Java横向、纵向合并图片

文章出处:点击打开链接   http://cxr1217.iteye.com/blog/1638681


进行图片对比时候想把两张有差异的图片放到一起,方便人工查看下,在网上搜了一些,有纵向合并的。

将纵向合并的自己调整了下,源码如下:

 

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.awt.image.BufferedImage;  
  3. import javax.imageio.ImageIO;  
  4.   
  5. public class TestPic {  
  6.     public static void main(String[] args) {  
  7.         xPic();  
  8.     }  
  9.       
  10.     public static void xPic(){//横向处理图片  
  11.         try {  
  12.             /* 1 读取第一张图片*/   
  13.             File fileOne = new File("E:\\1.png");  
  14.             BufferedImage imageFirst = ImageIO.read(fileOne);  
  15.             int width = imageFirst.getWidth();// 图片宽度  
  16.             int height = imageFirst.getHeight();// 图片高度  
  17.             int[] imageArrayFirst = new int[width * height];// 从图片中读取RGB  
  18.             imageArrayFirst = imageFirst.getRGB(00, width, height, imageArrayFirst, 0, width);  
  19.   
  20.             /* 1 对第二张图片做相同的处理 */  
  21.             File fileTwo = new File("E:\\2.png");  
  22.             BufferedImage imageSecond = ImageIO.read(fileTwo);  
  23.             int[] imageArraySecond = new int[width * height];  
  24.             imageArraySecond = imageSecond.getRGB(00, width, height, imageArraySecond, 0, width);  
  25.               
  26.             // 生成新图片   
  27.             BufferedImage imageResult = new BufferedImage(width * 2 , height,BufferedImage.TYPE_INT_RGB);  
  28.             imageResult.setRGB(00, width, height, imageArrayFirst, 0, width);// 设置左半部分的RGB  
  29.             imageResult.setRGB(width, 0, width, height, imageArraySecond, 0, width);// 设置右半部分的RGB  
  30.             File outFile = new File("D:\\out.jpg");  
  31.             ImageIO.write(imageResult, "jpg", outFile);// 写图片  
  32.         } catch (Exception e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.     }  
  36.       
  37.     public static void yPic(){//纵向处理图片  
  38.         try {  
  39.             /* 1 读取第一张图片*/   
  40.             File fileOne = new File("D:\\1.GIF");  
  41.             BufferedImage imageFirst = ImageIO.read(fileOne);  
  42.             int width = imageFirst.getWidth();// 图片宽度  
  43.             int height = imageFirst.getHeight();// 图片高度  
  44.             int[] imageArrayFirst = new int[width * height];// 从图片中读取RGB  
  45.             imageArrayFirst = imageFirst.getRGB(00, width, height, imageArrayFirst, 0, width);  
  46.   
  47.             /* 1 对第二张图片做相同的处理 */  
  48.             File fileTwo = new File("D:\\1.GIF");  
  49.             BufferedImage imageSecond = ImageIO.read(fileTwo);  
  50.             int[] imageArraySecond = new int[width * height];  
  51.             imageArraySecond = imageSecond.getRGB(00, width, height, imageArraySecond, 0, width);  
  52.               
  53.             // 生成新图片   
  54.             BufferedImage imageResult = new BufferedImage(width, height * 2,BufferedImage.TYPE_INT_RGB);  
  55.             imageResult.setRGB(00, width, height, imageArrayFirst, 0, width);// 设置左半部分的RGB  
  56.             imageResult.setRGB(0, height, width, height, imageArraySecond, 0, width);// 设置右半部分的RGB  
  57.             File outFile = new File("D:\\out.jpg");  
  58.             ImageIO.write(imageResult, "jpg", outFile);// 写图片  
  59.         } catch (Exception e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.     }  
  63.   
  64. }  
 

直接运行即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值