简体   繁体   English

是否可以从android中的位图中删除透明像素

[英]Is it possible to remove transparent pixels from bitmap in android

在我的应用程序中,如果图像未填充 imageView,我正在截屏,然后将透明像素添加到位图中。是否可以从位图中删除透明像素或在没有透明像素的情况下截屏。提前致谢。

This method is a lot faster:这种方法要快得多:

static Bitmap trim(Bitmap source) {
    int firstX = 0, firstY = 0;
    int lastX = source.getWidth();
    int lastY = source.getHeight();
    int[] pixels = new int[source.getWidth() * source.getHeight()];
    source.getPixels(pixels, 0, source.getWidth(), 0, 0, source.getWidth(), source.getHeight());
    loop:
    for (int x = 0; x < source.getWidth(); x++) {
        for (int y = 0; y < source.getHeight(); y++) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                firstX = x;
                break loop;
            }
        }
    }
    loop:
    for (int y = 0; y < source.getHeight(); y++) {
        for (int x = firstX; x < source.getWidth(); x++) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                firstY = y;
                break loop;
            }
        }
    }
    loop:
    for (int x = source.getWidth() - 1; x >= firstX; x--) {
        for (int y = source.getHeight() - 1; y >= firstY; y--) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                lastX = x;
                break loop;
            }
        }
    }
    loop:
    for (int y = source.getHeight() - 1; y >= firstY; y--) {
        for (int x = source.getWidth() - 1; x >= firstX; x--) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                lastY = y;
                break loop;
            }
        }
    }
    return Bitmap.createBitmap(source, firstX, firstY, lastX - firstX, lastY - firstY);
}

I have done this way and it works great.我已经这样做了,而且效果很好。

public static Bitmap createTrimmedBitmap(Bitmap bmp) {
    int imgHeight = bmp.getHeight();
    int imgWidth  = bmp.getWidth();
    int smallX=0,largeX=imgWidth,smallY=0,largeY=imgHeight;
    int left=imgWidth,right=imgWidth,top=imgHeight,bottom=imgHeight;
    for(int i=0;i<imgWidth;i++)
    {
        for(int j=0;j<imgHeight;j++)
        {
            if(bmp.getPixel(i, j) != Color.TRANSPARENT){
                if((i-smallX)<left){
                    left=(i-smallX);
                }
                if((largeX-i)<right)
                {
                    right=(largeX-i);
                }
                if((j-smallY)<top)
                {
                    top=(j-smallY);
                }
                if((largeY-j)<bottom)
                {
                    bottom=(largeY-j);
                }
            }
        }
    }
    Log.d(TAG, "left:" + left + " right:" + right + " top:" + top + " bottom:" + bottom);
    bmp=Bitmap.createBitmap(bmp,left,top,imgWidth-left-right, imgHeight-top-bottom);

    return bmp;

}
Bitmap imageWithBG = Bitmap.createBitmap(image.getWidth(), image.getHeight(),image.getConfig()); // Create another image the same size imageWithBG.eraseColor(Color.WHITE); // set its background to white, or whatever color you want Canvas canvas = new Canvas(imageWithBG); // create a canvas to draw on the new image canvas.drawBitmap(image, 0f, 0f, null); // draw old image on the background image.recycle(); // clear out old image

To trim crop transparent borders of a image in Android you ca use this arrange.要在 Android 中修剪图像的透明边框,您可以使用此排列。 Work faster because don't need read all pixels, it just slice the bitmap, more details: CropTrimTransparentImage工作更快,因为不需要读取所有像素,它只是对位图进行切片,更多细节: CropTrimTransparentImage

public Bitmap crop (Bitmap bitmap){

    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    int[] empty = new int[width];
    int[] buffer = new int[width];
    Arrays.fill(empty,0);

    int top = 0;
    int left = 0;
    int botton = height;
    int right = width;

    for (int y = 0; y < height; y++) {
        bitmap.getPixels(buffer, 0, width, 0, y, width, 1);
        if (!Arrays.equals(empty, buffer)) {
            top = y;
            break;
        }
    }

    for (int y = height - 1; y > top; y--) {
        bitmap.getPixels(buffer, 0, width, 0, y, width, 1);
        if (!Arrays.equals(empty, buffer)) {
            botton = y;
            break;
        }
    }


    int bufferSize = botton -top +1;
    empty = new int[bufferSize];
    buffer = new int[bufferSize];
    Arrays.fill(empty,0);

    for (int x = 0; x < width; x++) {
        bitmap.getPixels(buffer, 0, 1, x, top + 1, 1, bufferSize);
        if (!Arrays.equals(empty, buffer)) {
            left = x;
            break;
        }
    }

    for (int x = width - 1; x > left; x--) {
        bitmap.getPixels(buffer, 0, 1, x, top + 1, 1, bufferSize);
        if (!Arrays.equals(empty, buffer)) {
            right = x;
            break;
        }
    }

    Bitmap cropedBitmap = Bitmap.createBitmap(bitmap, left, top, right-left, botton-top);
    return cropedBitmap;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM