干货来了,JAVA代码实现图片分割、合并工具类

发表于:2021-5-13 09:22

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:洛阳泰山    来源:CSDN

#
Java
分享:
  
  思路:
  将原图,竖向划分为10个等份,前两个等份作为1个参考图,后8份作为1张样本图,所以总共需要分割9张图出来(第一张占两份),然后将第一张参考图和后面8张样本图合并成8个样本结果即可。
  实现
  新建普通java 项目,Java单类实现代码,复制到java项目中,用idea编辑器 主方法运行。(引入的Class 都是JDK中自有的)
   
  import javax.imageio.ImageIO;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
   
  public class ImageUtil {
   
      public static BufferedImage cutIO(int x, int y, int width, int height, BufferedImage img) {
          int[] imgRgb = new int[width * height];
          System.out.println(x+" "+y+" "+width+" "+height);
          img.getRGB(x, y, width, height, imgRgb, 0, width);
          BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
          newImage.setRGB(0, 0, width, height, imgRgb, 0, width);
          return newImage;
      }
   
      public static void cut(int x, int y, int width, int height, BufferedImage img, Integer num) {
          BufferedImage newImage = cutIO(x,y,width,height,img);
          newImage.setRGB(0, 0, width, height, new int[width * height], 0, width);
          try {
              ImageIO.write(newImage, "PNG", new File("C:\\Users\\tarzan\\Desktop\\image\\"+num+".png"));
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
   
   
      public static  void  exec(String IMG) throws IOException {
          BufferedImage image = ImageIO.read(new File(IMG));
          int w=image.getWidth();
          int d=w/10;
          int h=image.getHeight();
          BufferedImage first = cutIO(0,0,d*2,h,image);;
          for (int i = 1; i < 9; i++) {
              BufferedImage img= cutIO(d*(i+1),0,d,h,image);
              mergeImage(first,img,"C:\\Users\\tarzan\\Desktop\\image\\0_"+(i)+".png");
          }
      }
   
   
   
      public static void mergeImage(String imgPath1,String imgPath2,String margeImgPath){
          try {
              BufferedImage bi_1 = ImageIO.read(new File(imgPath1));
              BufferedImage bi_2 = ImageIO.read(new File(imgPath2));
              mergeImage(bi_1,bi_2,margeImgPath);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
   
      public static void mergeImage(BufferedImage bi_1,BufferedImage bi_2,String margeImgPath){
          try{
              //假设图片1 和图片2 高度相同,左右合成
              //new 一个新的图像
              int w_1 = bi_1.getWidth();
              int w_2 = bi_2.getWidth();
              int h= bi_1.getHeight();
              int w= w_1 + w_2;
              BufferedImage bi=new BufferedImage(w,h,BufferedImage.TYPE_4BYTE_ABGR);
              //像素一个一个复制过来
              for(int y=0; y<h; y++){
                  for(int x=0;x<w_1;x++){
                      bi.setRGB(x,y,bi_1.getRGB(x,y));
                  }
              }
              for(int y=0;y<h;y++){
                  for(int x=0;x<w_2;x++){
                      bi.setRGB(w_1+x,y,bi_2.getRGB(x,y));
                  }
              }
              //输出文件
              ImageIO.write(bi,"png",new File(margeImgPath));
          }catch(IOException ex){
              ex.printStackTrace();
          }
      }
   
   
      public static void main(String[] args) throws IOException {
          exec("C:\\Users\\tarzan\\Desktop\\image\\test.png");
            //  mergeImage("C:\\Users\\tarzan\\Desktop\\image\\1.png","C:\\Users\\tarzan\\Desktop\\image\\2.png","C:\\Users\\tarzan\\Desktop\\image\\1_2.png");
      }
   
   
  }
  执行结果:

      本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号