創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
本文實(shí)例講述了java實(shí)現(xiàn)合并圖片的方法。分享給大家供大家參考,具體如下:
package com.test; import java.io.File; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; public class ImageCombineTest { public static void main(String args[]) { try { // 讀取第一張圖片 File fileOne = new File("/Users/coolcloud/Pictures/Art/lena-2.jpg"); BufferedImage ImageOne = ImageIO.read(fileOne); int width = ImageOne.getWidth(); // 圖片寬度 int height = ImageOne.getHeight(); // 圖片高度 // 從圖片中讀取RGB int[] ImageArrayOne = new int[width * height]; ImageArrayOne = ImageOne.getRGB(0, 0, width, height, ImageArrayOne, 0, width); // 對(duì)第二張圖片做相同的處理 File fileTwo = new File("/Users/coolcloud/Pictures/Art/lena-2.jpg"); BufferedImage ImageTwo = ImageIO.read(fileTwo); int[] ImageArrayTwo = new int[width * height]; ImageArrayTwo = ImageTwo.getRGB(0, 0, width, height, ImageArrayTwo, 0, width); // 生成新圖片 // BufferedImage ImageNew = new BufferedImage(width * 2, height, // BufferedImage.TYPE_INT_RGB); BufferedImage ImageNew = new BufferedImage(width*2, height*2, BufferedImage.TYPE_INT_RGB); ImageNew.setRGB(0, 0, width, height, ImageArrayOne, 0, width); // 設(shè)置左半部分的RGB // ImageNew.setRGB(width, 0, width, height, ImageArrayTwo, 0, width);// 設(shè)置右半部分的RGB // ImageNew.setRGB(0, height, width, ImageOne.getHeight()+ImageTwo.getHeight(), ImageArrayTwo, 0, width);// 設(shè)置右半部分的RGB ImageNew.setRGB(0, height, width, height, ImageArrayTwo, 0, width); // 設(shè)置右半部分的RGB File outFile = new File("/Users/coolcloud/Pictures/generatepic.jpg"); ImageIO.write(ImageNew, "png", outFile); // 寫圖片 } catch (Exception e) { e.printStackTrace(); } } }