Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更好地替代.getPixel()?_Java_Android_Imaging - Fatal编程技术网

Java 更好地替代.getPixel()?

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

我在我的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;
}
公共位图转换为阈值(位图任意位图)
{
int width=anythingBmap.getWidth();
int height=anythingBmap.getHeight();
int阈值=120;

对于(int x=0;x使用public void
getPixels(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));