zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    WebApi 接口参数不再困惑:传参详解
    dataType和contentType的区别
    WaitHandles 的数目必须少于或等于 64 个--任意线程信号量监视
    跨线程调用DataGridView控件
    (转)调整.NET控件WebBrowser的默认浏览器内核版本
    Winform中checklistbox控件的常用方法
    csuoj 残缺的棋盘
    csuoj 你经历过绝望吗?两次! bfs + 优先队列
    csuoj barricade
    csuoj 集训队分组
  • 原文地址:https://www.cnblogs.com/zuferj115/p/8462512.html
Copyright © 2011-2022 走看看