Android位图删除白边

Android位图删除白边,android,bitmap,Android,Bitmap,我有一个关于Android中位图的问题:我有一个左右空白[大小未知]的位图。是否可以创建一个删除了所有白边的新位图(矩形) 据说不知画在哪里 这样做的好方法是什么? 谢谢 使用Bitmap.createBitmap(源、x、y、宽度、高度)这样知道白边距大小就可以随心所欲了。谢谢@Maxim Efimov&@StackOverflowException 以防有人需要这类问题的代码片段: 此方法返回删除边距的剪切较小位图。首先将像素传递给int数组,然后使用该数组比Bitmap.getPixel方

我有一个关于Android中位图的问题:我有一个左右空白[大小未知]的位图。是否可以创建一个删除了所有白边的新位图(矩形)

据说不知画在哪里

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

使用
Bitmap.createBitmap(源、x、y、宽度、高度)
这样知道白边距大小就可以随心所欲了。

谢谢@Maxim Efimov&@StackOverflowException

以防有人需要这类问题的代码片段:

此方法返回删除边距的剪切较小位图。首先将像素传递给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;
}
Bitmap bmp2=删除边距(bmp,Color.WHITE);
专用静态位图removeMargins2(位图bmp,int-color){
//TODO自动生成的方法存根
long dtMili=System.currentTimeMillis();
int MTop=0,MBot=0,MLeft=0,MRight=0;
布尔值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());
对于(int ii=0,contX=0,contY=0;ii=bmp.getWidth()){
contX=0;
contY++;
如果(contY>=bmp.getHeight()){
打破
}
}
}
对于(int hP=0;hP=0&&!found2;hP--){
//寻找MBot
对于(int wP=0;wP=0&&!found2;wP--){
//找怀特先生
对于(int hP=0;hP
我的解决方案:

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

实际上,我不知道边距的大小(例如canvas.drawText,字母数未知[用户输入],我正在寻找一种方法来检测和删除页边距。->创建新页边距bitmap@user1616685有一个有点复杂但可用的过程-获得一个带边距的位图,然后在每边的边界像素上迭代,直到没有找到白色像素-实际上,这是边距的结尾。这样做可以得到所有边距,然后就可以使用我上面描述的方法循环位图像素?我将尝试一下并对其进行注释here@user1616685是的,或者只是使用,int)因为它不需要额外的缓冲区,我认为你修剪了太多的1个像素,因为你只在下一次迭代时退出外循环。我认为您应该测试例如
wP>-1&&!found2
这台机器的性能如何?看起来批量获取所有像素可能会大大加快速度……我在一台设备上使用了它,对于500x700位图,我需要180毫秒,但它是一个1.2 GHz的处理器。您有更快的解决方案吗?你能给出一个片段吗?仿真器需要更长的1800毫秒。所以加快这个过程会很好
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);
    }
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);
    }