Patola
Patola

Reputation: 673

Slow perofromance when drawing transformationd bitmap on canvas in Android

I am trying to further process a Camera2 image. Because the cameras in devices have different rotations and flipped based on back and front camera, I use transforms to properly rotate it. transformationMatrix is that matrix for the front camera that has 270 rotation.

Then from that transformed camera image, I want to copy a scrolling window to another bitmap. I want to retain that bitmap/state and draw a line before drawing finalBitmapWithScanner on the phone screen.

Is there a way to do this more efficiently and fast? The second line takes 200ms to complete which is the main issue here.

   Canvas canvas = new Canvas(tempBitmap);
   canvas.drawBitmap(cameraBitmap, transformationMatrix, paint); // <=  200ms 
   Rect src = new Rect((int) lastXPos, 0, (int) mXPos, mViewHeight);
   Canvas canvas2 = new Canvas(finalBitmap);
   canvas2.drawBitmap(tempBitmap, src, src, paint);
   Canvas canvas3 = new Canvas(finalBitmapWithScanner);
   canvas3.drawBitmap(finalBitmap, 0, 0, paint);
   canvas3.drawLine(mXPos, 0, mXPos, mViewHeight/2, scrollerPaint);


    transformationMatrix.reset();
    transformationMatrix.setRotate(270, imageHeight, 0);
    transformationMatrix.postTranslate(-imageHeight, 0);
    transformationMatrix.postScale(scaleFactor, scaleFactor);
    transformationMatrix.postScale(-1f, 1f, mViewWidth / 2f, mViewHeight / 2f);

Upvotes: 1

Views: 208

Answers (1)

sina akbari
sina akbari

Reputation: 736

There are bunch of ways you can try to achieve fast rendering:

  1. You can pass parameters "paint" an null.
  2. also you can use function CreateScaledBitmap and notice you have to set scale and size before rendering as see in below:

As you can see in documentation enter link description here; you have to resize and rescale your bitmap before rendering so you can use code below for your BitmapFactory.Options :

mBitmapOptions.inScaled = true;
mBitmapOptions.inDensity = srcWidth;
mBitmapOptions.inTargetDensity =  dstWidth;

// will load & resize the image to be 1/inSampleSize dimensions
    mCurrentBitmap = BitmapFactory.decodeResources(getResources(), 
         mImageIDs, mBitmapOptions);
  1. use canvas.restore() after draw func.

Upvotes: 1

Related Questions