java图片分割

java 分割图片

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class cutPicUtils{

  public static void main(String[] args) throws IOException {
    File file = new File("img1.png");//本地图片位置  --这是相对路径 在本项目根文件下
    BufferedImage image = (BufferedImage) ImageIO.read(file);
    BufferedImage cut1 = cut(0, 0, 100, 100, image);
    ImageIO.write(cut1, "png", new File("cut1.png"));//分割后的图片位置
  }

  public static BufferedImage cut(int x, int y, int wight, int hight, BufferedImage img) {
    int[] simgRgb = new int[wight * hight];
    img.getRGB(x, y, wight, hight, simgRgb, 0, wight);
    BufferedImage newImage = new BufferedImage(wight, hight, BufferedImage.TYPE_INT_ARGB);
    newImage.setRGB(0, 0, wight, hight, simgRgb, 0, wight);
    return newImage;
  }
  
}

这是一个对图片进行多次分割的代码

public static void MycutPic(String filePath) throws IOException {
    File file = new File(filePath);//本地图片
    BufferedImage image = (BufferedImage) ImageIO.read(file);
    int count = 0;
    for (int i = 0; i < 4; i++) {
      for (int k = 0; k < 4; k++) {
        count++;
        int x1 = i * 75;
        int y1 = k * 75;
        
        BufferedImage cut1 = cut(x1, y1, 75, 75, image);
        File file1 = new File("cut" + count + ".png");
        ImageIO.write(cut1, "png", file1);
        count++;
        
        if (i == 3 || k == 3) {
          continue;
        }
        
        int x2 = (i * 75) + 50;
        int y2 = (k * 75) + 50;
        
        BufferedImage cut2 = cut(x2, y2, 75, 75, image);
        File file2 = new File("cut" + count + ".png");
        ImageIO.write(cut2, "png", file2);
        count++;
        
        int x3 = (i * 75) + 25;
        int y3 = (k * 75) + 25;
        
        BufferedImage cut3 = cut(x3, y3, 75, 75, image);
        File file3 = new File("cut" + count + ".png");
        ImageIO.write(cut3, "png", file3);
      }
    }
    
  }

原图找不到了,这是原图做了灰度处理后的图片
在这里插入图片描述
这是分割效果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值