不丢失像素的切图

图片切割

将一个原有图片根据传进来的横切数切成小图。

思路:

获取图片长宽算出切成多少块,然后判断是否丢失像素。

版本一:

Dingtalk_20211124164821.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
java复制代码File file = new File("btg.jpg"); // 项目目录下有名为btg.jpg的图片

FileInputStream fis = new FileInputStream(file);

BufferedImage image = ImageIO.read(fis); //把文件读到图片缓冲流中

int rows = 4; //定义图片要切分成多少块

int cols = 4;

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++) {
//初始化BufferedImage

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();

}

}

System.out.println("切分完成");

//保存小图片到文件中

for (int i = 0; i < imgs.length; i++) {
ImageIO.write(imgs[i], "jpg", new File("img" + i + ".jpg"));

}

System.out.println("小图片创建完成");

此时发现一个问题,每个图片都是固定大小。丢掉了一些像素,违背了我们的初衷

思路:

此时我们只能将最右边和最下边的宽高进行计算,右下角的宽高比较特殊,右下角呢个图片宽高=原图宽高-(算出的每格大小 * (列/行 - 1))

Dingtalk_20211124165912.jpg

具体实现:

方便前段使用将行列都从1开始计数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
java复制代码public static void main(String[] args) throws Exception {
File file = new File("xxx"); // 项目目录下有名为btg.jpg的图片

FileInputStream fis = new FileInputStream(file);

BufferedImage image = ImageIO.read(fis); //把文件读到图片缓冲流中

//定义图片要切分成多少块
int rows = 7;

// 算出每块高度是多少
int chunkHeight = image.getHeight()/rows;

// 算出多少列
int cols = image.getWidth()/chunkHeight;

// 算出每列多少宽度
int chunkWidth = image.getWidth()/cols;

int chunks = rows * cols;

int count = 0;

BufferedImage imgs[] = new BufferedImage[chunks];

for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {

// 判断是不是最后一块
if (j == cols) {
// 算出最后一块宽度是多少
int wight = image.getWidth() - (chunkWidth * (j - 1));
if (i == rows) {
// 算出最后一块宽高
int hight = image.getHeight() - (chunkHeight * (i - 1));
imgs[count] = new BufferedImage(wight, hight, image.getType());
Graphics2D gr = imgs[count++].createGraphics();
// image原图 sx2 - sx1 = 图片宽度 sy2 - sy1 = 图片高度
gr.drawImage(image, 0, 0, wight, hight,
image.getWidth(), chunkHeight * (i - 1),
chunkWidth * (j - 1) , image.getHeight(), null);
gr.dispose();
System.out.println("sx1==="+wight+"sy1===="+(chunkHeight * (i - 1))+
"sx2===="+image.getWidth()+ "sy2===="+image.getHeight());
}else {
// 最后一列
imgs[count] = new BufferedImage(wight, chunkHeight, image.getType());
Graphics2D gr = imgs[count++].createGraphics();
// image原图 sx2 - sx1 = 图片宽度 sy2 - sy1 = 图片高度
gr.drawImage(image, 0, 0, wight, chunkHeight,
chunkWidth * (j - 1), chunkHeight * (i - 1),
image.getWidth(), (chunkHeight * (i - 1)) + chunkHeight, null);
gr.dispose();
System.out.println("sx1==="+(chunkWidth * (j - 1))+"sy1===="+(chunkHeight * (i - 1)) +
"sx2===="+ image.getWidth()+"sy2===="+((chunkHeight * (i - 1)) + chunkHeight));
}
}else {
if (i == rows) {
int hight = image.getHeight() - (chunkHeight * (i - 1));
imgs[count] = new BufferedImage(chunkWidth, hight, image.getType());
Graphics2D gr = imgs[count++].createGraphics();
gr.drawImage(image, 0, 0, chunkWidth, hight,
chunkWidth * (j-1), chunkHeight * (i-1),
chunkWidth * (j-1)+chunkWidth, image.getHeight(), null);
gr.dispose();
System.out.println("sx1==="+(chunkWidth * (j-1)+chunkWidth)+"sy1===="+(chunkHeight * (i-1))+"sx2===="+chunkWidth * (j-1)+chunkWidth
+"sy2===="+image.getHeight());
}else {
imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());
Graphics2D gr = imgs[count++].createGraphics();
gr.drawImage(image, 0, 0, chunkWidth, chunkHeight,
chunkWidth * (j-1), chunkHeight * (i-1),
(chunkWidth * (j-1)) + chunkWidth, (chunkHeight * (i-1)) + chunkHeight, null);
gr.dispose();
System.out.println("sx1==="+(chunkWidth * (j-1))+
"sy1===="+(chunkHeight * (i-1))+
"sx2===="+(chunkWidth * (j-1) + chunkWidth)
+"sy2===="+(chunkHeight * (i-1) + chunkHeight));

}
}
ImageIO.write(imgs[count-1], "jpg", new File("D:\\bbb\\"+"img" + (count-1) + ".jpg"));
}
}
System.out.println("切分完成");

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%