weirdgyn
weirdgyn

Reputation: 926

Android fast bitmap drawing

I need to optimize this portion of code which consume about 70ms (on my Samsung Galaxy Tab S2 - Android 7.0):

private static final int IR_FRAME_WIDTH = 160;
private static final int IR_FRAME_HEIGHT = 120;

...

final int[] bmp_data = NormalizeBmp(image_data, IR_FRAME_WIDTH, IR_FRAME_HEIGHT, Polarity.WhiteHot);

Bitmap bitmap = Bitmap.createBitmap(IR_FRAME_WIDTH, IR_FRAME_HEIGHT, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, 0, null);

final Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);

for (int y = 0; y < IR_FRAME_HEIGHT; y++)
    for(int x = 0; x < IR_FRAME_WIDTH; x++) {
        final int _gray = bmp_data[(y * IR_FRAME_WIDTH) + x];

        paint.setColor(Color.rgb(_gray, _gray, _gray));
        canvas.drawPoint(x, y, paint);
    }

I'm not used to native code so I would prefer not-to use it if there's a standard code way to improve this performances. I think the code is pretty self-explaining NormalizeBmp create an array of 160x120 integers containing the gray tone to use. I can easily change NormalizeBmp to produce a different color output.

Upvotes: 0

Views: 134

Answers (1)

Rahul Shukla
Rahul Shukla

Reputation: 1292

Instead of drawing pixel by pixel over the canvas, what you can do is to create a bitmap directly from the data returned by NormalizeBmp(). All you need is to modify the returned int array to pack the colors in RGB format. Since you are using the same color to set your Paint() in RGB, this would be pretty straightforward. Here's a simple way to pack the color:

retVal[i] = (0xFF << 24) | (retVal[i] & 0xFF)<< 16 | (retVal[i] & 0xFF) << 8 | retVal[i]

and then use the modified array to draw a bitmap using Bitmap.Create(int[] colors, width, height, bitmap_format).

Upvotes: 1

Related Questions