Rotate and Scale Images with Android’s Matrix

First, you’ll need to integrate OpenCV into your project. We won’t cover that here, but there are a number of good tutorials on that, as well as OpenCV’s own documentation.

Once you’ve got the camera displaying, you’ll probably see something like the image at left: the image is sideways and not in full frame.

How do we fix this? The answer lies, in part, in the CameraBridgeViewBase.java file that ships with OpenCV for Android. Specifically, we’ll use a function called deliverAndDrawFrame . This function takes the camera frame, converts it to a bitmap, and renders that bitmap to an Android canvas on the screen.

And while we can rotate and scale the canvas in this function, it’s a very expensive operation that will slow your app down significantly. What would be ideal is if we simply modified the matrix into which all of those pixels get drawn.

To do so, we’ll need to add some code to the CameraBridgeViewBase.java file. We’ll do so just above the deliverAndDrawFrame method. First, declare a matrix variable:

private final Matrix mMatrix = new Matrix();

We’ll then want a function to be able to update that matrix based on various events. You can stub that function out next:

private void updateMatrix() { }

When to Call the UpdateMatrix function

When will this function get called? We’ll want to do it in deliverAndDrawFrame , but we also want to make sure it’s set up right initially. To ensure that, we’ll add a couple of override methods just below your updateMatrix function:

methods for triggering update matrix on layout and measure in android

These call the updateMatrix function when the screen is laid out, and when there is a call to measure for screen dimension changes.

Now we can specify what to do inside our updateMatrix function.

Getting matrix and window dimensions, and determining scale

First, we’ll want to reset the matrix from whatever manipulations occurred in the previous frame. We’ll also want to get some basic measurements so we can position things properly.