Java实现图片切割或图片裁剪

该博客展示了如何使用Java对图像进行处理,包括将一副图片均匀分割成9个相同大小的小图,并详细解释了代码实现过程。此外,还介绍了如何根据指定的四个点坐标裁剪图片,输出裁剪后的图片。整个过程涵盖了图像读取、图形操作和文件输出等步骤。
摘要由CSDN通过智能技术生成
public class Test {

    public static void main(String[] agrs) throws IOException {
        String originalImg = "C:\\Users\\dell\\Pictures\\1.jpg";
        // 读入大图
        File file = new File(originalImg);
        FileInputStream fis = new FileInputStream(file);
        System.out.println(file.exists());
        BufferedImage image = ImageIO.read(fis);

        // 分割成3*3(9)个小图
        int rows = 3;
        int cols = 3;
        int chunks = rows * cols;

        // 计算每个小图的宽度和高度
        int chunkWidth = image.getWidth() / cols;
        int chunkHeight = image.getHeight() / rows;
        System.out.println("图片的宽度为:" + chunkWidth * rows + "图片的高度为:" + chunkHeight * cols);//230,278
        //大图中的一部分
        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);
                System.out.println("源矩阵第一个角的坐标" + chunkWidth * y + "+" + chunkHeight * x + "源矩阵第二个角的坐标" + chunkWidth * (y + 1) + "+" + chunkHeight * (x + 1));
                gr.dispose();
                /*
                 源矩阵第一个角的坐标0+0源矩阵第二个角的坐标77+92
                 源矩阵第一个角的坐标77+0源矩阵第二个角的坐标154+92
                 源矩阵第一个角的坐标154+0源矩阵第二个角的坐标231+92
                 源矩阵第一个角的坐标0+92源矩阵第二个角的坐标77+184
                 源矩阵第一个角的坐标77+92源矩阵第二个角的坐标154+184
                 源矩阵第一个角的坐标154+92源矩阵第二个角的坐标231+184
                 源矩阵第一个角的坐标0+184源矩阵第二个角的坐标77+276
                 源矩阵第一个角的坐标77+184源矩阵第二个角的坐标154+276
                 源矩阵第一个角的坐标154+184源矩阵第二个角的坐标231+276
                */
            }
        }
        // 输出小图
        for (int i = 0; i < imgs.length; i++) {
            //ImageIO.write(imgs[i], "jpg", new File("C:\\img\\split\\img" + i + ".jpg"));
            ImageIO.write(imgs[i], "jpg", new File("C:\\Users\\dell\\Desktop\\ceshi\\" + i + ".jpg"));
            System.out.println(i);
        }

        System.out.println("完成分割!");
    }

以上为将一副图片均匀分割为九个大小相同的图片,具体的使用已在代码中做了注释.

下面则是对一幅图片进行裁剪,最后输出一张被裁剪后的图片

public class Test {

    public static void main(String[] agrs) throws IOException {
        String originalImg = "C:\\Users\\dell\\Pictures\\1.jpg";
        // 读入大图
        File file = new File(originalImg);
        FileInputStream fis = new FileInputStream(file);
        System.out.println(file.exists());
        BufferedImage image = ImageIO.read(fis);
        /*
         * 假设我需要切割的四点坐标为a(20,30)b(200,40)c(30,200)d(200,210)
         * 起始坐标为(最小的x,最小的y)
         * 此时的实际切割坐标应为由a为起始坐标,width为(x坐标最大的点的x - a的x坐标),height为(y坐标最大的点的y - a的y坐标)
         * 此时的width为180,height为180
         */
        //切割图片
        BufferedImage bf = new BufferedImage(180, 180, image.getType());
        Graphics2D graphics2D = bf.createGraphics();
        graphics2D.drawImage(image, 20, 30, 180, 180, 0, 0, 180, 180, null);
        graphics2D.dispose();
        //输出图片
        Random random = new Random();
        int j = random.nextInt(1000);
        ImageIO.write(bf, "jpg", new File("C:\\Users\\dell\\Desktop\\ceshi\\qiege\\" + j + ".jpg"));
        System.out.println("完成切割");
    }

 

  • 8
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值