java 拼接文字和圖片
public static void test2() {
try {
// 讀取第一張圖片
// File fileOne = new File("D:\\qq.jpg");
// BufferedImage ImageOne = ImageIO.read(fileOne);
// int width = ImageOne.getWidth();//圖片寬度
// int height = ImageOne.getHeight();//圖片高度
// 對第二張圖片做相同的處理
File fileTwo = new File("D:\\qq.jpg");
BufferedImage ImageTwo = ImageIO.read(fileTwo);
int width = ImageTwo.getWidth();// 圖片寬度
int height = ImageTwo.getHeight();// 圖片高度
int height2 = 20;
BufferedImage ImageOne = new BufferedImage(width, height2,
BufferedImage.TYPE_4BYTE_ABGR);
// ImageOne.get
Graphics g = ImageOne.getGraphics();//
g.fillRect(0, 0, width, height);
g.setColor(Color.blue);
Font mFont = new Font("宋體", Font.PLAIN, 12);
g.setFont(mFont);
String text = "添加文字測試";
g.drawString(text, 0, 10);
// 從圖片中讀取RGB
int[] ImageArrayOne = new int[width * height2];
ImageArrayOne = ImageOne.getRGB(0, 0, width, height2,
ImageArrayOne, 0, width);
int[] ImageArrayTwo = new int[width * height];
ImageArrayTwo = ImageTwo.getRGB(0, 0, width, height, ImageArrayTwo,
0, width);
// 生成新圖片
BufferedImage ImageNew = new BufferedImage(width, height + height2,
BufferedImage.TYPE_INT_RGB);
ImageNew.setRGB(0, 0, width, height2, ImageArrayOne, 0, width);// 設置上半部分的RGB
// ImageNew.setRGB(width,0,width,height,ImageArrayTwo,0,width);//設置右半部分的RGB
ImageNew.setRGB(0, height2, width, height, ImageArrayTwo, 0, width);// 設置下半部分的RGB
File outFile = new File("d:\\qq2.jpg");
ImageIO.write(ImageNew, "jpg", outFile);// 寫圖片
} catch (Exception e) {
e.printStackTrace();
}
}