Kotlin BufferedImage trim

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fun trim(image: BufferedImage): BufferedImage {
val width = image.width
val height = image.height
var top = height / 2
var bottom = top
var left = width / 2
var right = left
for (x in 0 until width) {
for (y in 0 until height) {
if (image.getRGB(x, y) != 0) {
top = min(top, y)
bottom = max(bottom, y)
left = min(left, x)
right = max(right, x)
}
}
}
val w = right - left
val h = bottom - top
if (w > 0 && h > 0)
return image.getSubimage(left, top, w, h)
return image
}
分享到