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();
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/106943/viewspace-1027829/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/106943/viewspace-1027829/