Crop image to smallest size by removing transparent pixels in java

别等时光非礼了梦想. 提交于 2019-12-02 05:12:37

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.

There's a trivial solution – to scan every pixel. The algorithm bellow has constant performance 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);
}

But this is much more effective:

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

This algorithm follows the idea from pepan's answer (see above) and is 2 to 4 times more effective. The difference is: it never scans any pixel twice and tries to contract search range on each stage.

Method's performance in worst case is O(w•h–a•b)

This code works for me. The algorithm is simple, it iterates from left/top/right/bottom of the picture and finds the very first pixel in the column/row which is not transparent. It then remembers the new corner of the trimmed picture and finally it returns the sub image of the original image.

There are things which could be improved.

  1. The algorithm expects, there is the alpha byte in the data. It will fail on an index out of array exception if there is not.

  2. The algorithm expects, there is at least one non-transparent pixel in the picture. It will fail if the picture is completely transparent.

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

    }

If your sheet already has transparent pixels, the BufferedImage returned by getSubimage() will, too. The default Graphics2D composite rule is AlphaComposite.SRC_OVER, which should suffice for drawImage().

If the sub-images have a distinct background color, use a LookupOp with a four-component LookupTable that sets the alpha component to zero for colors that match the background.

I'd traverse the pixel raster only as a last resort.

Addendum: Extra transparent pixels may interfere with collision detection, etc. Cropping them will require working with a WritableRaster directly. Rather than working from the center out, I'd start with the borders, using a pair of getPixels()/setPixels() methods that can modify a row or column at a time. If a whole row or column has zero alpha, mark it for elimination when you later get a sub-image.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!