1 packageImagePackage;2
3 importjava.awt.AlphaComposite;4 importjava.awt.Graphics2D;5 importjava.awt.image.BufferedImage;6 importjava.io.File;7 importjava.io.IOException;8 importjava.net.HttpURLConnection;9 importjava.net.URL;10
11 importjavax.imageio.ImageIO;12
13 /**
14
15 * 该类实现了图片的合并功能,可以选择水平合并或者垂直合并。16 * 当然此例只是针对两个图片的合并,如果想要实现多个图片的合并,只需要自己实现方法 BufferedImage17 * mergeImage(BufferedImage[] imgs, boolean isHorizontal)即可;18 * 而且这个方法更加具有通用性,但是时间原因不实现了,方法和两张图片实现是一样的19 */
20
21 public classImageMerge {22
23 /**
24 *@paramfileUrl25 * 文件绝对路径或相对路径26 *@return读取到的缓存图像27 *@throwsIOException28 * 路径错误或者不存在该文件时抛出IO异常29 */
30 public staticBufferedImage getBufferedImage(String fileUrl)31 throwsIOException {32 File f = newFile(fileUrl);33 returnImageIO.read(f);34 }35
36
37 /**
38 * 远程图片转BufferedImage39 *@paramdestUrl 远程图片地址40 *@return
41 */
42 public staticBufferedImage getBufferedImageDestUrl(String destUrl) {43 HttpURLConnection conn = null;44 BufferedImage image = null;45 try{46 URL url = newURL(destUrl);47 conn =(HttpURLConnection) url.openConnection();48 if (conn.getResponseCode() == 200) {49 image =ImageIO.read(conn.getInputStream());50 returnimage;51 }52 } catch(Exception e) {53 e.printStackTrace();54 } finally{55 conn.disconnect();56 }57 returnimage;58 }59
60 /**
61 * 输出图片62 *63 *@parambuffImg64 * 图像拼接叠加之后的BufferedImage对象65 *@paramsavePath66 * 图像拼接叠加之后的保存路径67 */
68 public static voidgenerateSaveFile(BufferedImage buffImg, String savePath) {69 int temp = savePath.lastIndexOf(".") + 1;70 try{71 File outFile = newFile(savePath);72 if(!outFile.exists()){73 outFile.createNewFile();74 }75 ImageIO.write(buffImg, savePath.substring(temp), outFile);76 System.out.println("ImageIO write...");77 } catch(IOException e) {78 e.printStackTrace();79 }80 }81
82 /**
83 *84 * @Title: 构造图片85 * @Description: 生成水印并返回java.awt.image.BufferedImage86 *@parambuffImg87 * 源文件(BufferedImage)88 *@paramwaterFile89 * 水印文件(BufferedImage)90 *@paramx91 * 距离右下角的X偏移量92 *@paramy93 * 距离右下角的Y偏移量94 *@paramalpha95 * 透明度, 选择值从0.0~1.0: 完全透明~完全不透明96 *@returnBufferedImage97 *@throwsIOException98 */
99 public static BufferedImage overlyingImage(BufferedImage buffImg, BufferedImage waterImg, int x, int y, float alpha) throwsIOException {100
101 //创建Graphics2D对象,用在底图对象上绘图
102 Graphics2D g2d =buffImg.createGraphics();103 int waterImgWidth = waterImg.getWidth();//获取层图的宽度
104 int waterImgHeight = waterImg.getHeight();//获取层图的高度105 //在图形和图像中实现混合和透明效果
106 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));107 //绘制
108 g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);109 g2d.dispose();//释放图形上下文使用的系统资源
110 returnbuffImg;111 }112
113
114 /**
115 * 待合并的两张图必须满足这样的前提,如果水平方向合并,则高度必须相等;如果是垂直方向合并,宽度必须相等。116 * mergeImage方法不做判断,自己判断。117 *118 *@paramimg1119 * 待合并的第一张图120 *@paramimg2121 * 带合并的第二张图122 *@paramisHorizontal123 * 为true时表示水平方向合并,为false时表示垂直方向合并124 *@return返回合并后的BufferedImage对象125 *@throwsIOException126 */
127 public staticBufferedImage mergeImage(BufferedImage img1,128 BufferedImage img2, boolean isHorizontal) throwsIOException {129 int w1 =img1.getWidth();130 int h1 =img1.getHeight();131 int w2 =img2.getWidth();132 int h2 =img2.getHeight();133
134 //从图片中读取RGB
135 int[] ImageArrayOne = new int[w1 *h1];136 ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); //逐行扫描图像中各个像素的RGB到数组中
137 int[] ImageArrayTwo = new int[w2 *h2];138 ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);139
140 //生成新图片
141 BufferedImage DestImage = null;142 if (isHorizontal) { //水平方向合并
143 DestImage = new BufferedImage(w1+w2, h1, BufferedImage.TYPE_INT_RGB);144 DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); //设置上半部分或左半部分的RGB
145 DestImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2);146 } else { //垂直方向合并
147 DestImage = new BufferedImage(w1, h1 +h2, BufferedImage.TYPE_INT_RGB);148 DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); //设置上半部分或左半部分的RGB
149 DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); //设置下半部分的RGB
150 }151
152 returnDestImage;153 }154
155 /**
156 * Java 测试图片叠加方法157 */
158 public static voidoverlyingImageTest() {159
160 String sourceFilePath = "D://test//test1.jpg";161 String waterFilePath = "D://test//test2.jpg";162 String saveFilePath = "D://test//overlyingImageNew.jpg";163 try{164 BufferedImage bufferImage1 =getBufferedImage(sourceFilePath);165 BufferedImage bufferImage2 =getBufferedImage(waterFilePath);166
167 //构建叠加层
168 BufferedImage buffImg = overlyingImage(bufferImage1, bufferImage2, 0, 0, 1.0f);169 //输出水印图片
170 generateSaveFile(buffImg, saveFilePath);171 } catch(IOException e) {172 e.printStackTrace();173 }174
175 }176
177
178 /**
179 * Java 测试图片合并方法180 */
181 public static voidimageMargeTest() {182 //读取待合并的文件
183 BufferedImage bi1 = null;184 BufferedImage bi2 = null;185 //调用mergeImage方法获得合并后的图像
186 BufferedImage destImg = null;187 System.out.println("下面是垂直合并的情况:");188 String saveFilePath = "D://test//new1.jpg";189 String divingPath = "D://test//new2.jpg";190 String margeImagePath = "D://test//margeNew.jpg";191 try{192 bi1 =getBufferedImage(saveFilePath);193 bi2 =getBufferedImage(divingPath);194 //调用mergeImage方法获得合并后的图像
195 destImg = mergeImage(bi1, bi2, false);196 } catch(IOException e) {197 e.printStackTrace();198 }199 //保存图像
200 generateSaveFile(destImg, margeImagePath);201 System.out.println("垂直合并完毕!");202 }203
204 public static voidmain(String[] args) {205 //测试图片的叠加
206 overlyingImageTest();207 //测试图片的垂直合并
208 imageMargeTest();209 }210
211 }