0

I have a bitmap from camera , after resize , it change to horizontal , I need to rotate 90 degree , but most of the sample are use Matrix to rotate , but when I new Matrix , it said matrix is deprecated , than I try to use CANVAS , following this , first time to use it , trying to figure it out , but can not rotate it , app crash , help please

code

  resizePhoto = BitmapFactory.decodeFile(imageLocation,bmOption);

//  R o t a t e    B i t m a p   90 degree

    Canvas canvas = new Canvas(resizePhoto);
    canvas.rotate(90);
    canvas.drawBitmap(resizePhoto , null ,null);
8
  • matrix isn't deprecated as far as I know
    – Bhargav
    Mar 4, 2016 at 10:25
  • in drawBitmap(resizePhoto , null ,null); you are using null Matrix which causes the crash
    – pskink
    Mar 4, 2016 at 10:27
  • we are talking about android.graphics.Matrix, not about android.opengl.Matrix
    – pskink
    Mar 4, 2016 at 10:30
  • @pskink Thanks , but I try to create a matrix by useing Matrix matrix = new Matrix() , it say matrix is deprecated, or i shouldn't do this?
    – Awei Hsue
    Mar 4, 2016 at 10:30

2 Answers 2

1
Matrix matrix = new Matrix();
matrix.setRotate(angle, imageCenterX, imageCenterY);
yourCanvas.drawBitmap(yourBitmap, matrix, null);
4
  • thank you! I'm noob , I confuse android.graphics.Matrix, not about android.opengl.Matrix
    – Awei Hsue
    Mar 4, 2016 at 10:41
  • It happens. don't beat yourself up about it. we're all constantly learning!
    – Hazonko
    Mar 4, 2016 at 10:42
  • Make sure you use a different bitmap for the Canvas than the one you draw with. That will cause your crash.
    – Simon
    Mar 4, 2016 at 10:42
  • if the answer was sufficient, please accept it. Thanks.
    – Hazonko
    Mar 7, 2016 at 10:09
0

You might want to rotate using a matrix passed into Bitmap.createBitmap. It should be faster than using a Canvas.

Matrix matrix = new Matrix();
matrix.setRotate(angle);
Bitmap resizePhoto = BitmapFactory.decodeFile(imageLocation, bmOption);
Bitmap rotatedPhoto = Bitmap.createBitmap(resizePhoto, 0, 0, 
    resizePhoto.getWidth(), resizePhoto.getHeight(), matrix, true);
resizePhoto.recycle();

You might need to swap the getWidth() and getHeight() around for an exact 90 degree rotation. I forget.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.