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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
根据提供的引用内容,以下是使用Java对PNG素材进行分割的示例代码: ```java import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageSplitter { public static void main(String[] args) { try { // 读取PNG图片 BufferedImage image = ImageIO.read(new File("input.png")); // 获取图片宽度和高度 int width = image.getWidth(); int height = image.getHeight(); // 定义每个小图的宽度和高度 int smallWidth = 100; int smallHeight = 100; // 计算需要分割的小图数量 int rows = height / smallHeight; int cols = width / smallWidth; // 分割小图 for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { // 创建一个新的小图像 BufferedImage smallImage = new BufferedImage(smallWidth, smallHeight, BufferedImage.TYPE_INT_ARGB); // 将原始图像的一部分复制到小图像中 for (int i = 0; i < smallHeight; i++) { for (int j = 0; j < smallWidth; j++) { int rgb = image.getRGB(x * smallWidth + j, y * smallHeight + i); smallImage.setRGB(j, i, rgb); } } // 保存小图像为PNG文件 ImageIO.write(smallImage, "PNG", new File("output_" + y + "_" + x + ".png")); } } System.out.println("PNG素材分割完成!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 这段代码使用Java的ImageIO库读取PNG图片,并将其分割成指定大小的小图像。每个小图像的宽度和高度可以根据需要进行调整。分割后的小图像将保存为独立的PNG文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值