android-位图阈值更快

android-位图阈值更快,第1张

概述我正在尝试编写一种将采用位图并将其强制为严格的黑白图像(无灰色阴影)的方法.我首先将位图传递给使用colormatrix使其变为灰度的方法:publicBitmaptoGrayscale(BitmapbmpOriginal){intwidth,height;height=bmpOriginal.getHeight();width=bmpO

我正在尝试编写一种将采用位图并将其强制为严格的黑白图像(无灰色阴影)的方法.

我首先将位图传递给使用colormatrix使其变为灰度的方法:

public Bitmap toGrayscale(Bitmap bmpOriginal){            int wIDth, height;    height = bmpOriginal.getHeight();    wIDth = bmpOriginal.getWIDth();        Bitmap bmpGrayscale = Bitmap.createBitmap(wIDth, height, Bitmap.Config.RGB_565);    Canvas c = new Canvas(bmpGrayscale);    Paint paint = new Paint();    colorMatrix cm = new colorMatrix();    cm.setSaturation(0);    colorMatrixcolorFilter f = new colorMatrixcolorFilter(cm);    paint.setcolorFilter(f);    c.drawBitmap(bmpOriginal, 0, 0, paint);    return bmpGrayscale;}

效果很好,而且很快.

然后我将其传递给另一种方法,以将灰度图像强制为2色图像(黑白),此方法有效,但显然它正在遍历每个像素,并且需要很长时间:

public Bitmap toStrictBlackWhite(Bitmap bmp){        Bitmap imageOut = bmp;        int tempcolorRed;        for(int y=0; y<bmp.getHeight(); y++){            for(int x=0; x<bmp.getWIDth(); x++){                tempcolorRed = color.red(imageOut.getPixel(x,y));                Log.v(TAG, "color: "+tempcolorRed);                if(imageOut.getPixel(x,y) < 127){                    imageOut.setPixel(x, y, 0xffffff);                }                else{                    imageOut.setPixel(x, y, 0x000000);                }                           }        }         return imageOut;    }

有人知道更快,更有效的方法吗?

解决方法:

不要使用getPixel()和setPixel().

使用getPixels()将返回所有像素的多维数组.在此数组上本地执行 *** 作,然后使用setPixels()设置修改后的数组.这将明显更快.

总结

以上是内存溢出为你收集整理的android-位图阈值更快全部内容,希望文章能够帮你解决android-位图阈值更快所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1087158.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存