Skip to content

Commit 4f9d41e

Browse files
committed
修复切图脚本
1 parent eefe9e0 commit 4f9d41e

File tree

1 file changed

+51
-59
lines changed

1 file changed

+51
-59
lines changed

Cutter.java

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import java.io.*;
21
import javax.imageio.ImageIO;
32
import java.awt.image.BufferedImage;
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.logging.Logger;
46

57
public class Cutter {
68
private static boolean readPixel(BufferedImage image, int x, int y) {
@@ -17,70 +19,60 @@ private static boolean readPixel(BufferedImage image, int x, int y) {
1719
}
1820
}
1921

20-
public static void main(String[] args) throws IOException {
21-
for (var fileName : args) {
22-
var file = new File(fileName);
23-
var image = ImageIO.read(file);
24-
if (image == null) {
25-
System.out.println("Bad file: " + fileName);
26-
continue;
27-
}
28-
System.out.println("Process image: " + fileName);
22+
private static void writeFile(BufferedImage image, File outputDir, String name, int x, int y, int w, int h) throws IOException {
23+
if (w <= 0 || h <= 0) {
24+
return;
25+
}
26+
var leftTop = image.getSubimage(x, y, w, h);
27+
ImageIO.write(leftTop, "png", new File(outputDir, name));
28+
}
2929

30-
var leftStart = 0;
31-
while (true) {
32-
var color = image.getRGB(0, leftStart) & 0xFFFFFF;
33-
if (readPixel(image, 0, leftStart)) {
34-
break;
35-
}
36-
leftStart++;
37-
}
30+
public static void cut(File file, Logger logger) throws IOException {
31+
var image = ImageIO.read(file);
32+
if (image == null) {
33+
logger.severe("Bad file: " + file);
34+
return;
35+
}
36+
logger.info("Process image: " + file);
3837

39-
var leftLength = 0;
40-
while (true) {
41-
if (!readPixel(image, 0, leftStart + leftLength)) {
42-
break;
43-
}
44-
leftLength++;
45-
}
38+
var leftStart = 0;
39+
while (!readPixel(image, 0, leftStart)) {
40+
leftStart++;
41+
}
4642

47-
var topStart = 0;
48-
while (true) {
49-
if (readPixel(image, topStart, 0)) {
50-
break;
51-
}
52-
topStart++;
53-
}
43+
var leftLength = 0;
44+
while (readPixel(image, 0, leftStart + leftLength)) {
45+
leftLength++;
46+
}
5447

55-
var topLength = 0;
56-
while (true) {
57-
if (!readPixel(image, topStart + topLength, 0)) {
58-
break;
59-
}
60-
topLength++;
61-
}
48+
var topStart = 0;
49+
while (!readPixel(image, topStart, 0)) {
50+
topStart++;
51+
}
6252

63-
var outputDir = new File(fileName + "_cutted");
64-
outputDir.mkdir();
53+
var topLength = 0;
54+
while (readPixel(image, topStart + topLength, 0)) {
55+
topLength++;
56+
}
57+
58+
var outputDir = new File(file + "_cutted");
59+
outputDir.mkdir();
6560

66-
var leftTop = image.getSubimage(1, 1, topStart - 1, leftStart - 1);
67-
ImageIO.write(leftTop, "png", new File(outputDir, "left_top.png"));
68-
var centerTop = image.getSubimage(topStart, 1, topLength, leftStart - 1);
69-
ImageIO.write(centerTop, "png", new File(outputDir, "center_top.png"));
70-
var rightTop = image.getSubimage(topStart + topLength, 1, image.getWidth() - topStart - topLength - 1, leftStart - 1);
71-
ImageIO.write(rightTop, "png", new File(outputDir, "right_top.png"));
72-
var leftCenter = image.getSubimage(1, leftStart, topStart - 1, leftLength);
73-
ImageIO.write(leftCenter, "png", new File(outputDir, "left_center.png"));
74-
var centerCenter = image.getSubimage(topStart, leftStart, topLength, leftLength);
75-
ImageIO.write(centerCenter, "png", new File(outputDir, "center_center.png"));
76-
var rightCenter = image.getSubimage(topStart + topLength, leftStart, image.getWidth() - topStart - topLength - 1, leftLength);
77-
ImageIO.write(rightCenter, "png", new File(outputDir, "right_center.png"));
78-
var leftBottom = image.getSubimage(1, leftStart + leftLength, topStart - 1, image.getHeight() - leftStart - leftLength - 1);
79-
ImageIO.write(leftBottom, "png", new File(outputDir, "left_bottom.png"));
80-
var centerBottom = image.getSubimage(topStart, leftStart + leftLength, topLength, image.getHeight() - leftStart - leftLength - 1);
81-
ImageIO.write(centerBottom, "png", new File(outputDir, "center_bottom.png"));
82-
var rightBottom = image.getSubimage(topStart + topLength, leftStart + leftLength, image.getWidth() - topStart - topLength - 1, image.getHeight() - leftStart - leftLength - 1);
83-
ImageIO.write(rightBottom, "png", new File(outputDir, "right_bottom.png"));
61+
writeFile(image, outputDir, "left_top.png", 1, 1, topStart - 1, leftStart - 1);
62+
63+
writeFile(image, outputDir, "center_top.png", topStart, 1, topLength, leftStart - 1);
64+
writeFile(image, outputDir, "right_top.png", topStart + topLength, 1, image.getWidth() - topStart - topLength - 1, leftStart - 1);
65+
writeFile(image, outputDir, "left_center.png", 1, leftStart, topStart - 1, leftLength);
66+
writeFile(image, outputDir, "center_center.png", topStart, leftStart, topLength, leftLength);
67+
writeFile(image, outputDir, "right_center.png", topStart + topLength, leftStart, image.getWidth() - topStart - topLength - 1, leftLength);
68+
writeFile(image, outputDir, "left_bottom.png", 1, leftStart + leftLength, topStart - 1, image.getHeight() - leftStart - leftLength - 1);
69+
writeFile(image, outputDir, "center_bottom.png", topStart, leftStart + leftLength, topLength, image.getHeight() - leftStart - leftLength - 1);
70+
writeFile(image, outputDir, "right_bottom.png", topStart + topLength, leftStart + leftLength, image.getWidth() - topStart - topLength - 1, image.getHeight() - leftStart - leftLength - 1);
71+
}
72+
73+
public static void main(String[] args) throws IOException {
74+
for (var fileName : args) {
75+
cut(new File(fileName), Logger.getGlobal());
8476
}
8577
}
8678
}

0 commit comments

Comments
 (0)