通过删除java中的透明像素将图像裁剪为最小尺寸 [英] Crop image to smallest size by removing transparent pixels in java

查看:331
本文介绍了通过删除java中的透明像素将图像裁剪为最小尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张精灵表,每张图片都以32x32格式为中心。实际图像不是32x32,而是略小。我想做的是拍摄一个单元格并裁剪透明像素,使图像尽可能小。

I have a sprite sheet which has each image centered in a 32x32 cell. The actual images are not 32x32, but slightly smaller. What I'd like to do is take a cell and crop the transparent pixels so the image is as small as it can be.

我如何用Java做到这一点( JDK 6)?

How would I do that in Java (JDK 6)?

以下是我目前如何将图块分解为单元格的示例:

Here is an example of how I'm currently breaking up the tile sheet into cells:

BufferedImage tilesheet = ImageIO.read(getClass().getResourceAsStream("/sheet.png");
for (int i = 0; i < 15; i++) {
  Image img = tilesheet.getSubimage(i * 32, 0, 32, 32);
  // crop here..
}

我目前的想法是测试中心的每个像素,看看它是否透明,但我想知道是否会更快/更干净的方式。

My current idea was to test each pixel from the center working my way out to see if it is transparent, but I was wondering if there would be a faster/cleaner way of doing this.

推荐答案

我认为这正是你应该做的,循环遍历像素数组,检查然后丢弃。虽然例如当你有一个星形时它不会调整图像的大小,因此要注意这一点。

I think this is exactly what you should do, loop through the array of pixels, check for alpha and then discard. Although when you for example would have a star shape it will not resize the image to be smaller be aware of this.

这篇关于通过删除java中的透明像素将图像裁剪为最小尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆