如何在Android中提高效率?

Trt*_*Trt 5 android

我有这段代码CameraPreview从a 获取a的位图TextureView并将其呈现在a上ImageView.

public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    // Invoked every time there's a new Camera preview frame

    bmp = mTextureView.getBitmap();
    bmp2 = bmp.copy(bmp.getConfig(),true);

    for(int x=0;x<bmp.getWidth();x++){
        for(int y=0;y<bmp.getHeight();y++){
            //Log.i("Pixel RGB (Int)", Integer.toString(bmp.getPixel(x,y)));
            if(bmp.getPixel(x,y) < -8388608){
                bmp2.setPixel(x,y,Color.WHITE);
            }else{
                bmp2.setPixel(x,y,Color.BLACK);
            }
        }
    }

    mImageView.setImageBitmap(bmp2);
}
Run Code Online (Sandbox Code Playgroud)

所以基本上我将在相机显示的任何内容上应用实时图像处理.现在它只是背面和白色像素.它现在有点慢,位图的宽度和高度只有~250像素.

这是推荐的做法吗?

Zie*_*ony 5

要有效地过滤位图,可以使用ColorMatrixColorFilter.例如,要使您的图像为黑白,请使用以下代码:

ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);

float m = 255f;
float t = -255*1.2f;
ColorMatrix threshold = new ColorMatrix(new float[] {
            m, 0, 0, 1, t,
            0, m, 0, 1, t,
            0, 0, m, 1, t,
            0, 0, 0, 1, 0
});

// Convert to grayscale, then scale and clamp
colorMatrix.postConcat(threshold);

ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
imageView.setColorFilter(filter);
Run Code Online (Sandbox Code Playgroud)

基本上你必须改变颜色范围,所以等于(颜色)和(颜色+ 1)的值是(0)和(1).这就是为什么我将颜色乘以255并进行移位.您可能希望使用这些参数来获得正确的结果.

查看幻灯片:http://chiuki.github.io/android-shaders-filters/#/16