Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot scale or flip surfaceview matrix

I am making a camera application. I use a SurfaceView to display my camera preview.

        preview=(SurfaceView)findViewById(R.id.preview);
        previewHolder=preview.getHolder();
        previewHolder.addCallback(surfaceCallback);
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        Matrix m = preview.getMatrix();
         m.setScale(-1,1);

What I'm trying to do is to flip the SurfaceView matrix. I tried to use preScale() and postScale(), and I tried to add the last 2 lines to to Surfaceview.callback method like this:

   public void surfaceChanged(SurfaceHolder holder, int format,
                                   int width, int height) {                     
          initPreview(width, height);
          startPreview();
Matrix m = preview.getMatrix();
         m.setScale(-1,1);
                  }

but I get the same error everywhere (along with app crashing):

java.lang.IllegalStateException: Matrix can not be modified

What am I doing wrong? Is it possible to change scale off SurfaceView whatsoever? If not, is there some other way to flip camera preview?

like image 200
Highstaker Avatar asked Mar 19 '26 08:03

Highstaker


1 Answers

To clarify the answer posted by Madhusudan a few months ago, instead of creating a blank Matrix and then setting it equal to your previews Matrix, you have to do a deep copy of the Matrix using set().

It would look something like this:

Matrix m = new Matrix(preview.getMatrix());
m.postRotate(90.0f);
canvas.drawBitmap(bitmap, m, NULL);

Or something like this:

Matrix m = new Matrix();
m.set(preview.getMatrix());
m.postRotate(90.0f);
canvas.drawBitmap(bitmap, m, NULL);
like image 143
Lurc Avatar answered Mar 21 '26 22:03

Lurc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!