Crop image to smallest size by removing transparent pixels in java

后端 未结 6 968
走了就别回头了
走了就别回头了 2021-01-21 15:04

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 tr

6条回答
  •  太阳男子
    2021-01-21 15:38

    [Hi I tried the following. In the images file idle1.png is the image with a big transparent box while testing.png is the same image with minimum bounding box

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

    where the get functions are

    public int getY1(WritableRaster raster) { //top of character

        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]
    

    Thank you for your help regards Michael.Look at Idle1.png and the minimum bounding box idle = testing.png]images here

提交回复
热议问题