• java分割、合并图片


    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    
    /**
     * Created by liwj
     * date:2018/2/23
     * comment:
     */
    public class ImageSplit {
    
        /**
         * 切割图片
         *
         * @throws Exception
         */
        private static void splitImage() throws Exception {
            String originalImg = "C:\Users\liwj\Desktop\tidb\image\ori.jpg";
            File file = new File(originalImg);
            FileInputStream fis = new FileInputStream(file);
            BufferedImage image = ImageIO.read(fis);
    
            int rows = 2;
            int cols = 2;
            int chunks = rows * cols;
    
            int chunkWidth = image.getWidth() / cols;
            int chunkHeight = image.getHeight() / rows;
    
            int count = 0;
            BufferedImage[] imgs = new BufferedImage[chunks];
            for (int x = 0; x < rows; x++) {
                for (int y = 0; y < cols; y++) {
                    imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());
    
                    Graphics2D gr = imgs[count++].createGraphics();
                    gr.drawImage(image, 0, 0, chunkWidth, chunkHeight,
                            chunkWidth * y, chunkHeight * x,
                            chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);
                    gr.dispose();
                }
            }
            for (int i = 0; i < imgs.length; i++) {
                ImageIO.write(imgs[i], "jpg", new File("C:\Users\liwj\Desktop\tidb\image\" + i + ".jpg"));
            }
        }
    
        /**
         * 合并图片
         *
         * @throws Exception
         */
        private static void mergeImage() throws Exception {
            int rows = 2;
            int cols = 2;
            int chunks = rows * cols;
    
            int chunkWidth, chunkHeight;
            int type;
    
            File[] imgFiles = new File[chunks];
            for (int i = 0; i < chunks; i++) {
                imgFiles[i] = new File("C:\Users\liwj\Desktop\tidb\image\" + i + ".jpg");
            }
    
            BufferedImage[] buffImages = new BufferedImage[chunks];
            for (int i = 0; i < chunks; i++) {
                buffImages[i] = ImageIO.read(imgFiles[i]);
            }
            type = buffImages[0].getType();
            chunkWidth = buffImages[0].getWidth();
            chunkHeight = buffImages[0].getHeight();
    
            BufferedImage finalImg = new BufferedImage(chunkWidth * cols, chunkHeight * rows, type);
    
            int num = 0;
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    finalImg.createGraphics().drawImage(buffImages[num], chunkWidth * j, chunkHeight * i, null);
                    num++;
                }
            }
    
            ImageIO.write(finalImg, "jpeg", new File("C:\Users\liwj\Desktop\tidb\image\finalImg.jpg"));
        }
    
        public static void main(String[] args) {
            try {
                splitImage();
                mergeImage();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    Rabbitmq
    Python() with语法糖
    Python()-类的专有方法之双下划线方法
    git配置踩过的坑
    https 协议 和 ssh 协议在使用上的差别
    Definite dimensionality
    java中定义和声明的区别
    数据库中表的数据逻辑删除的方案
    StringBuffer、StringBuilder和String的区别?
    常量池中的String与堆中的String
  • 原文地址:https://www.cnblogs.com/zuferj115/p/8462512.html
Copyright © 2020-2023  润新知