Java 更好地替代.getPixel()?
我在我的Android应用程序中使用此代码将位图转换为纯黑白,它可以工作:Java 更好地替代.getPixel()?,java,android,imaging,Java,Android,Imaging,我在我的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(in
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;
}
公共位图转换为阈值(位图任意位图)
{
int width=anythingBmap.getWidth();
int height=anythingBmap.getHeight();
int阈值=120;
对于(int x=0;x使用public voidgetPixels(int[]像素,int偏移,int跨步,int x,int y,int width,int height)
。这将一次返回所有像素。更好的方法是为像素处理创建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;
}
在这种方法中如何处理单个像素?它是一个平坦的矩阵。因此,第n行第m列的像素位于数组位置n*width+m
final int N = mWidth * mHeight;
mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
int[] data8888 = new int[N];
mBitmap.copyPixelsFromBuffer(makeBuffer(data8888, N));