java合并等大小图成大图

本文介绍了一个Java程序,用于将多张尺寸相同的图片合并为一张大图片。提供了两种合并方式:合并两张图片及按行列批量合并多张图片。适用于批量处理图片场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

合并一组等大的图片文件成一个大图片
 
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.awt.image.BufferedImage;
  4. import javax.imageio.ImageIO;
  5. public class MergePics {    
  6.     public void mergeTwoFiles(String fn1, String fn2, String fnOut)
  7.         throws IOException {
  8.         File file1 = new File(fn1);
  9.         BufferedImage image1 = ImageIO.read(file1);
  10.         int width = image1.getWidth();
  11.         int height = image1.getHeight();
  12.         int[] imageArray1 = new int[width * height];
  13.         imageArray1 = image1.getRGB(00, width, height, imageArray1,
  14.                 0, width);
  15.         File file2 = new File(fn2);
  16.         BufferedImage image2 = ImageIO.read(file2);
  17.         int[] imageArray2 = new int[width * height];
  18.         imageArray2 = image2.getRGB(00, width, height, imageArray2,
  19.                 0, width);
  20.         BufferedImage imageNew = new BufferedImage(width * 2, height,
  21.                 BufferedImage.TYPE_INT_RGB);
  22.         imageNew.setRGB(00, width, height, imageArray1, 0, width);
  23.         imageNew.setRGB(width, 0, width, height, imageArray2, 0, width);
  24.         File outFile = new File(fnOut);
  25.         ImageIO.write(imageNew, "png", outFile);
  26.     }
  27.     public boolean mergeFiles(String[] fns, int num, String fnOut)
  28.         throws IOException {
  29.         if (fns.length < num || num <= 0) {
  30.             return false;
  31.         }
  32.         BufferedImage image = ImageIO.read(new File(fns[0]));
  33.         int w = image.getWidth();
  34.         int h = image.getHeight();
  35.         int numH = (int)Math.ceil(fns.length / num);
  36.         int numW = num;
  37.         BufferedImage imageNew = new BufferedImage(numW * w, numH * h,
  38.                 BufferedImage.TYPE_INT_RGB);
  39.         int[] imageArray = new int[w * h];
  40.         imageArray = image.getRGB(00, w, h, imageArray, 0, w);
  41.         imageNew.setRGB(00, w, h, imageArray, 0, w);
  42.         for (int i = 0, nW = 0, nH = 0; i < fns.length; i++, nW++) {
  43.             if (i == 0continue;
  44.             if (nW == num) {
  45.                 nW = 0;
  46.                 nH++;
  47.                 System.out.println("");
  48.             }
  49.             BufferedImage img = ImageIO.read(new File(fns[i]));
  50.             imageArray = img.getRGB(00, w, h, imageArray, 0, w);
  51.             imageNew.setRGB(nW * w, nH * h, w, h, imageArray, 0, w);
  52.             System.out.print("(" + nH + ", " + nW + ") ");
  53.         }
  54.         File outFile = new File(fnOut);
  55.         ImageIO.write(imageNew, "png", outFile);
  56.         return true;
  57.     }
  58.     
  59.     public void testMergeTwoFiles() {
  60.         String fn1 = "d:" + File.separator + "1" + File.separator +
  61.           "g1.png";
  62.         String fn2 = "d:" + File.separator + "1" + File.separator +
  63.           "g2.png";
  64.         String fnOut = "d:" + File.separator + "1" + File.separator + "2.png";
  65.         try {
  66.             this.mergeTwoFiles(fn1, fn2, fnOut);
  67.         } catch (IOException e) {
  68.             e.printStackTrace();
  69.         }       
  70.     }
  71.     
  72.     public void testMergeFiles() {
  73.         String fnprefix = "d:" + File.separator + "1" + File.separator;
  74.         String[] fns = new String[16];
  75.         for (int j = 0; j < fns.length; j++) {
  76.             fns[j] = fnprefix + "pic" + j + ".png";
  77.             System.out.println(j);
  78.         }
  79.         String fnOut = fnprefix + "out.png";
  80.         try {
  81.             this.mergeFiles(fns, 4, fnOut);
  82.         } catch (IOException e) {
  83.             e.printStackTrace();
  84.         }
  85.     }
  86.     /**
  87.      * @param args
  88.      */
  89.     public static void main(String[] args) {
  90.         MergePics mp = new MergePics();
  91.         mp.testMergeFiles();
  92.     }
  93. }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值