zoukankan      html  css  js  c++  java
  • 【图片分割器】Java完成图片分隔

    刷朋友圈,看到很多人发分隔后的9宫格,出现一个大图挺好看,所以自己用java写了一个图片分隔器。

    可以自定义分隔数。

    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    /**
     * @Description 图片分隔-发微信朋友圈
     * @Author cheng2839
     * @Date 2018年10月04日
     * @Version v1.0
     */
    public class MyTest028 {
    
        private static final String IMAGE_FILE_PATH = "E:\timg.jpg";
    
        //横向分隔个数
        private static final int SEP_X_NUM = 3;
        //纵向分隔个数
        private static final int SEP_Y_NUM = 3;
    
        public static void main(String[] args) {
            try {
                sepImage();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        public static void sepImage() throws IOException {
            File file = new File(IMAGE_FILE_PATH);
            if (!file.exists() || !file.isFile()) {
                throw new RuntimeException("file not exists or un-file:" + IMAGE_FILE_PATH);
            }
            BufferedImage image = ImageIO.read(file);
            int totalWidth = image.getWidth();
            int totalHeight = image.getHeight();
            int width = totalWidth / (SEP_X_NUM <=0?1:SEP_X_NUM);
            int height = totalHeight / (SEP_Y_NUM <=0?1:SEP_Y_NUM);
    
    
            File dirFile = new File(file.getParent(), file.getName().substring(0, file.getName().lastIndexOf(".")));
            if (!dirFile.exists()) {
                dirFile.mkdir();
            }
    
            for (int y = 0, j = 1; y <= totalHeight-height; y+=height, j++) {
                for (int x = 0, i = 1; x <= totalWidth-width; x+=width, i++) {
                    File targetFile = new File(dirFile, j+"_"+i+".jpg");
                    BufferedImage targetImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                    Graphics g = targetImage.getGraphics();
                    System.out.println(targetFile.getPath() + ":    x="+x+",y="+y+"; width="+width+", height="+height+"; totalWidth"+totalWidth+",totalHeight="+totalHeight);
                    g.drawImage(image.getSubimage(x, y, width, height), 0, 0, null);
                    ImageIO.write(targetImage, "JPG", targetFile);
                }
            }
        }
    
    }
    ____________________________特此,勉励____________________________
    本文作者cheng2839
    本文链接https://www.cnblogs.com/cheng2839
    关于博主:评论和私信会在第一时间回复。
    版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
    声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
  • 相关阅读:
    Activity 与ListActivity的区别
    自定义标题栏
    解决小程序无法触发SESSION问题
    js数组与字符串经常用的几种简单的方法
    python程序与进制的执行过程
    常问的 web前端 问题
    如何判断页面滑到了屏幕最底部
    http请求状态保持的四种方法
    vue知识点归纳与总结(笔记)
    Nginx常用命令及使用场景
  • 原文地址:https://www.cnblogs.com/cheng2839/p/14183390.html
Copyright © 2011-2022 走看看