How can I make this more efficient in Android?

Trt Trt

I have this piece of code that takes the bitmap of a CameraPreview from a TextureView and renders it on 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);
}

So basically I will be applying real-time image-processing on whatever the camera shows. For now it just back and whites pixels. It is a bit slow now, and the bitmap has only a width and height of ~250 pixels.

Is this the recommended way of doing this ?

Zielony

To filter bitmaps efficiently you can use ColorMatrixColorFilter. For example to make your image black & white use this code:

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);

Basically you have to transform the color range so values equal to (color) and (color+1) are (0) and (1). That's why I'm multiplying color by 255 and shifting. You may want to play with these parameters to get the right result.

Check out the slides here: http://chiuki.github.io/android-shaders-filters/#/16

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make this loop more efficient?

android - SQL database, how can I make my code more efficient

How can I make a code more efficient and shorter?

How can I make this Python code more efficient

How can I make this C# code more efficient?

How can I make my pandas code more efficient?

How can I make my trie more efficient?

How can I make this PyTorch heatmap function faster and more efficient?

How can I make this search query more efficient?

How can I make a recursive search for longest node more efficient?

How can I make this more efficient path finding?

How can I make this more efficient? (Merging arrays in C)

How can I refactor this code snippet to make it more efficient?

How can I make my website more efficient?

How can I make my IsItAHoliday function more efficient?

How can I make this PHP function more efficient at scale?

How can i make this modification on Dijkstra Algorithm more efficient?

How can I make this algorithm more efficient for a puzzle?

How can I make large IN clauses more efficient in SQL Server?

How can I make this pl/sql cursor more efficient?

How can I make my VBA error handling more efficient

How can I use iteration to make this vbnet code more efficient?

How can I concatenate my code properly and make this more efficient?

How can I make this program more efficient, short, and less complex?

How can i make this script more efficient?(Python)

How can I make this R function more efficient?

How can I make this python code more efficient?

How can I optimize this VBA code to make it more efficient and faster?

Can I make this macro more efficient or faster?