简体   繁体   English

Android Bitmap删除白边

[英]Android Bitmap remove white margin

I've got a question regarding Bitmaps in Android: I 've got a Bitmap with white margins [size unknown] around. 我有一个关于Android中位图的问题:我有一个带有白色边距[大小未知]的位图。 Is it possible to create a new Bitmap with all the white margins removed (rectangular shape)? 是否可以创建一个删除了所有白色边距的新位图(矩形)?

Bitmap bmp = Bitmap.createBitmap(width, bmpheigth, Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    canvas.setBitmap(bmp);
    canvas.drawColor(Color.WHITE);
// draw here things!

It is asumed to be unknown where are things painted. 人们认为不知道绘画的地方。

What is a good way to do that? 有什么好办法呢? thanks! 谢谢!

Thanks @Maxim Efimov & @StackOverflowException 谢谢@Maxim Efimov和@StackOverflowException

Just in Case Someone will need a snippet for this kind of problems: 以防万一有人需要一个代码片段来解决这类问题:

this method returns a cut out smaller Bitmap with Margins removed. 此方法返回一个删除了边距的剪切较小的Bitmap。 passing the pixels to a int-array first and then working with the array is a bit faster than the Bitmap.getPixel method 首先将像素传递给int数组,然后使用数组比Bitmap.getPixel方法快一点

just call the method indicating Source Bitmap and Background color. 只需调用指示源位图和背景颜色的方法。

Bitmap bmp2 = removeMargins(bmp, Color.WHITE);


private static Bitmap removeMargins2(Bitmap bmp, int color) {
    // TODO Auto-generated method stub


    long dtMili = System.currentTimeMillis();
    int MTop = 0, MBot = 0, MLeft = 0, MRight = 0;
    boolean found1 = false, found2 = false;

    int[] bmpIn = new int[bmp.getWidth() * bmp.getHeight()];
    int[][] bmpInt = new int[bmp.getWidth()][bmp.getHeight()];

    bmp.getPixels(bmpIn, 0, bmp.getWidth(), 0, 0, bmp.getWidth(),
            bmp.getHeight());

    for (int ii = 0, contX = 0, contY = 0; ii < bmpIn.length; ii++) {
        bmpInt[contX][contY] = bmpIn[ii];
        contX++;
        if (contX >= bmp.getWidth()) {
            contX = 0;
            contY++;
            if (contY >= bmp.getHeight()) {
                break;
            }
        }
    }

    for (int hP = 0; hP < bmpInt[0].length && !found2; hP++) {
        // looking for MTop
        for (int wP = 0; wP < bmpInt.length && !found2; wP++) {
            if (bmpInt[wP][hP] != color) {
                Log.e("MTop 2", "Pixel found @" + hP);
                MTop = hP;
                found2 = true;
                break;
            }
        }
    }
    found2 = false;

    for (int hP = bmpInt[0].length - 1; hP >= 0 && !found2; hP--) {
        // looking for MBot
        for (int wP = 0; wP < bmpInt.length && !found2; wP++) {
            if (bmpInt[wP][hP] != color) {
                Log.e("MBot 2", "Pixel found @" + hP);
                MBot = bmp.getHeight() - hP;
                found2 = true;
                break;
            }
        }
    }
    found2 = false;

    for (int wP = 0; wP < bmpInt.length && !found2; wP++) {
        // looking for MLeft
        for (int hP = 0; hP < bmpInt[0].length && !found2; hP++) {
            if (bmpInt[wP][hP] != color) {
                Log.e("MLeft 2", "Pixel found @" + wP);
                MLeft = wP;
                found2 = true;
                break;
            }
        }
    }
    found2 = false;

    for (int wP = bmpInt.length - 1; wP >= 0 && !found2; wP--) {
        // looking for MRight
        for (int hP = 0; hP < bmpInt[0].length && !found2; hP++) {
            if (bmpInt[wP][hP] != color) {
                Log.e("MRight 2", "Pixel found @" + wP);
                MRight = bmp.getWidth() - wP;
                found2 = true;
                break;
            }
        }

    }
    found2 = false;

    int sizeY = bmp.getHeight() - MBot - MTop, sizeX = bmp.getWidth()
            - MRight - MLeft;

    Bitmap bmp2 = Bitmap.createBitmap(bmp, MLeft, MTop, sizeX, sizeY);
    dtMili = (System.currentTimeMillis() - dtMili);
    Log.e("Margin   2",
            "Time needed " + dtMili + "mSec\nh:" + bmp.getWidth() + "w:"
                    + bmp.getHeight() + "\narray x:" + bmpInt.length + "y:"
                    + bmpInt[0].length);
    return bmp2;
}

使用Bitmap.createBitmap(source, x, y, width, height)因此知道白色边距大小,你可以做你想要的。

My solution: 我的解决方案

private Bitmap trim(Bitmap bitmap, int trimColor){
        int minX = Integer.MAX_VALUE;
        int maxX = 0;
        int minY = Integer.MAX_VALUE;
        int maxY = 0;

        for(int x = 0; x < bitmap.getWidth(); x++){
            for(int y = 0; y < bitmap.getHeight(); y++){
                if(bitmap.getPixel(x, y) != trimColor){
                    if(x < minX){
                        minX = x;
                    }
                    if(x > maxX){
                        maxX = x;
                    }
                    if(y < minY){
                        minY = y;
                    }
                    if(y > maxY){
                        maxY = y;
                    }
                }
            }
        }
        return Bitmap.createBitmap(bitmap, minX, minY, maxX - minX + 1, maxY - minY + 1);
    }

It isn't very fast, for 1280 x 576 px bitmap execution took 2965ms on Xiaomi Redmi 3S. 它不是很快,因为1280 x 576 px位图执行在小米Redmi 3S上花了2965ms。 If it possible scale down image before triming: 如果可以在修剪之前缩小图像:

private Bitmap scaleDown(Bitmap bitmap, float maxImageSize, boolean filter) {
        float ratio = Math.min(maxImageSize / bitmap.getWidth(), maxImageSize / bitmap.getHeight());
        int width = Math.round(ratio * bitmap.getWidth());
        int height = Math.round(ratio * bitmap.getHeight());

        return Bitmap.createScaledBitmap(bitmap, width, height, filter);
    }

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

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