我在我的Android应用程序中使用这段代码将位图转换为纯黑色和白色,它工作如下:
public Bitmap ConvertToThreshold(Bitmap anythingBmap)
{
int width = anythingBmap.getWidth();
int height = anythingBmap.getHeight();
int threshold = 120;
for(int x=0;x<width;x++){
for(int y=0;y<height;y++){
int pixel = anythingBmap.getPixel(x, y);
int gray = Color.red(pixel);
if(gray < threshold){
anythingBmap.setPixel(x, y, 0xFF000000);
} else{
anythingBmap.setPixel(x, y, 0xFFFFFFFF);
}
}
}
return anythingBmap;
}如果.getPixel()非常慢,那么这个问题需要很长时间来处理。有更快的方法吗?
谢谢
发布于 2014-05-28 05:26:39
使用公共空getPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)。这将同时返回所有像素。
发布于 2014-05-28 06:11:49
一个更好的方法是创建一个用于像素处理的int[]缓冲区。之后,只需将数组复制到位图中即可。您需要使用的方法:
public void copyPixelsFromBuffer (Buffer src)
public void copyPixelsToBuffer (Buffer dst)
private static IntBuffer makeBuffer(int[] src, int n) {
IntBuffer dst = IntBuffer.allocate(n);
for (int i = 0; i < n; i++) {
dst.put(src[i]);
}
dst.rewind();
return dst;
}样本代码:
final int N = mWidth * mHeight;
mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
int[] data8888 = new int[N];
mBitmap.copyPixelsFromBuffer(makeBuffer(data8888, N));https://stackoverflow.com/questions/23903377
复制相似问题