我有一张精灵表,每张图片都以32x32格式为中心。实际图像不是32x32,而是略小。我想做的是拍摄一个单元格并裁剪透明像素,使图像尽可能小。
我如何用Java(JDK 6)做到这一点?
以下是我目前如何将瓷砖表拆分为单元格的示例:
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..
}
我目前的想法是测试中心的每个像素,看看它是否透明,但我想知道是否会有更快/更清晰的方法。
答案 0 :(得分:5)
有一个简单的解决方案 - 扫描每个像素。下面的算法具有恒定的性能O(w•h)
。
private static BufferedImage trimImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
int top = height / 2;
int bottom = top;
int left = width / 2 ;
int right = left;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (image.getRGB(x, y) != 0){
top = Math.min(top, x);
bottom = Math.max(bottom, x);
left = Math.min(left, x);
right = Math.max(right, x);
}
}
}
return image.getSubimage(left, top, right - left, bottom - top);
}
但这更有效:
private static BufferedImage trimImage(BufferedImage image) {
WritableRaster raster = image.getAlphaRaster();
int width = raster.getWidth();
int height = raster.getHeight();
int left = 0;
int top = 0;
int right = width - 1;
int bottom = height - 1;
int minRight = width - 1;
int minBottom = height - 1;
top:
for (;top < bottom; top++){
for (int x = 0; x < width; x++){
if (raster.getSample(x, top, 0) != 0){
minRight = x;
minBottom = top;
break top;
}
}
}
left:
for (;left < minRight; left++){
for (int y = height - 1; y > top; y--){
if (raster.getSample(left, y, 0) != 0){
minBottom = y;
break left;
}
}
}
bottom:
for (;bottom > minBottom; bottom--){
for (int x = width - 1; x >= left; x--){
if (raster.getSample(x, bottom, 0) != 0){
minRight = x;
break bottom;
}
}
}
right:
for (;right > minRight; right--){
for (int y = bottom; y >= top; y--){
if (raster.getSample(right, y, 0) != 0){
break right;
}
}
}
return image.getSubimage(left, top, right - left + 1, bottom - top + 1);
}
此算法遵循 pepan 的答案(见上文)的想法,效率提高2到4倍。不同之处在于:它从不扫描任何像素两次,并尝试在每个阶段收缩搜索范围。
最差情况下,方法的效果为O(w•h–a•b)
答案 1 :(得分:3)
此代码适用于我。算法很简单,它从图片的左/上/右/下迭代,找到列/行中第一个不透明的像素。然后它会记住修剪过的图片的新角落,最后它会返回原始图像的子图像。
有些事情可以改进。
算法需要,数据中有alpha字节。如果没有,那么它将在数组异常的索引上失败。
算法预计,图片中至少有一个非透明像素。如果图片完全透明,它将失败。
private static BufferedImage trimImage(BufferedImage img) {
final byte[] pixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
int width = img.getWidth();
int height = img.getHeight();
int x0, y0, x1, y1; // the new corners of the trimmed image
int i, j; // i - horizontal iterator; j - vertical iterator
leftLoop:
for (i = 0; i < width; i++) {
for (j = 0; j < height; j++) {
if (pixels[(j*width+i)*4] != 0) { // alpha is the very first byte and then every fourth one
break leftLoop;
}
}
}
x0 = i;
topLoop:
for (j = 0; j < height; j++) {
for (i = 0; i < width; i++) {
if (pixels[(j*width+i)*4] != 0) {
break topLoop;
}
}
}
y0 = j;
rightLoop:
for (i = width-1; i >= 0; i--) {
for (j = 0; j < height; j++) {
if (pixels[(j*width+i)*4] != 0) {
break rightLoop;
}
}
}
x1 = i+1;
bottomLoop:
for (j = height-1; j >= 0; j--) {
for (i = 0; i < width; i++) {
if (pixels[(j*width+i)*4] != 0) {
break bottomLoop;
}
}
}
y1 = j+1;
return img.getSubimage(x0, y0, x1-x0, y1-y0);
}
答案 2 :(得分:2)
我认为这正是你应该做的,循环遍历像素数组,检查alpha然后丢弃。虽然例如当你有一个星形时,它不会将图像的大小调整为小一点。
答案 3 :(得分:1)
[嗨,我尝试了以下方法。在图像文件中,idle1.png是具有较大透明框的图像,而testing.png是具有最小边界框的同一图像
'BufferedImage tempImg = (ImageIO.read(new File(fileNPath)));
WritableRaster tempRaster = tempImg.getAlphaRaster();
int x1 = getX1(tempRaster);
int y1 = getY1(tempRaster);
int x2 = getX2(tempRaster);
int y2 = getY2(tempRaster);
System.out.println("x1:"+x1+" y1:"+y1+" x2:"+x2+" y2:"+y2);
BufferedImage temp = tempImg.getSubimage(x1, y1, x2 - x1, y2 - y1);
//for idle1.png
String filePath = fileChooser.getCurrentDirectory() + "\\"+"testing.png";
System.out.println("filePath:"+filePath);
ImageIO.write(temp,"png",new File(filePath));
get函数所在的地方
public int getY1(WritableRaster raster){ //字符顶部
for (int y = 0; y < raster.getHeight(); y++) {
for (int x = 0; x < raster.getWidth(); x++) {
if (raster.getSample(x, y,0) != 0) {
if(y>0) {
return y - 1;
}else{
return y;
}
}
}
}
return 0;
}
public int getY2(WritableRaster raster) {
//ground plane of character
for (int y = raster.getHeight()-1; y > 0; y--) {
for (int x = 0; x < raster.getWidth(); x++) {
if (raster.getSample(x, y,0) != 0) {
return y + 1;
}
}
}
return 0;
}
public int getX1(WritableRaster raster) {
//left side of character
for (int x = 0; x < raster.getWidth(); x++) {
for (int y = 0; y < raster.getHeight(); y++) {
if (raster.getSample(x, y,0) != 0) {
if(x > 0){
return x - 1;
}else{
return x;
}
}
}
}
return 0;
}
public int getX2(WritableRaster raster) {
//right side of character
for (int x = raster.getWidth()-1; x > 0; x--) {
for (int y = 0; y < raster.getHeight(); y++) {
if (raster.getSample(x, y,0) != 0) {
return x + 1;
}
}
}
return 0;
}'[Look at Idle1.png and the minimum bounding box idle = testing.png][1]
感谢您对Michael的帮助。请看Idle1.png和最小边界框idle = testing.png] images here
答案 4 :(得分:0)
如果您的工作表已有透明像素,则BufferedImage
返回的getSubimage()
也会显示。默认Graphics2D
composite rule为AlphaComposite.SRC_OVER
,应该足够drawImage()
。
如果子图像具有不同的背景颜色,请使用带有四分量LookupTable
的{{3}},将与背景匹配的颜色的alpha分量设置为零。
我只是作为最后的手段遍历像素栅格。
附录:额外的透明像素可能会干扰碰撞检测等。裁剪它们需要直接使用LookupOp
。我不是从中心开始工作,而是从边界开始,使用一对getPixels()
/ setPixels()
方法,一次可以修改行或列。如果整行或列的alpha值为零,请在稍后获取子图像时将其标记为消除。
答案 5 :(得分:0)
上面代码的简单修复。我使用了RGB的中位数,并修复了x和y的min()函数:
private static BufferedImage trim(BufferedImage img) {
int width = img.getWidth();
int height = img.getHeight();
int top = height / 2;
int bottom = top;
int left = width / 2 ;
int right = left;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (isFg(img.getRGB(x, y))){
top = Math.min(top, y);
bottom = Math.max(bottom, y);
left = Math.min(left, x);
right = Math.max(right, x);
}
}
}
return img.getSubimage(left, top, right - left, bottom - top);
}
private static boolean isFg(int v) {
Color c = new Color(v);
return(isColor((c.getRed() + c.getGreen() + c.getBlue())/2));
}
private static boolean isColor(int c) {
return c > 0 && c < 255;
}