Android Bitmap删除白边

时间:2013-09-26 08:58:31

标签: android bitmap

我有一个关于Android中位图的问题:我有一个带有白色边距[尺寸未知]的位图。是否可以创建一个删除了所有白色边距的新位图(矩形)?

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

据说不知道绘画的地方。

这样做的好方法是什么? 谢谢!

3 个答案:

答案 0 :(得分:5)

谢谢@Maxim Efimov& @StackOverflowException

以防万一有人需要一个代码片段来解决这类问题:

此方法返回一个删除了边距的剪切较小的Bitmap。首先将像素传递给int数组,然后使用数组比Bitmap.getPixel方法快一点

只需调用指示源位图和背景颜色的方法。

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

答案 1 :(得分:2)

使用Bitmap.createBitmap(source, x, y, width, height)因此,了解白色边距大小,您可以按照自己的意愿行事。

答案 2 :(得分:1)

我的解决方案:

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

速度不是很快,因为小米Redmi 3S的1280 x 576 px位图执行需要2965ms。 如果可以在修剪之前缩小图像:

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