Method 1. Using Bitmap.createbitmap
Bitmap Adjustphotorotation (Bitmap BM, final int orientationdegree) {
Matrix M = new Matrix ();
M.setrotate (Orientationdegree, (float) bm.getwidth ()/2, (float) bm.getheight ()/2);
try {
Bitmap BM1 = bitmap.createbitmap (BM, 0, 0, bm.getwidth (), Bm.getheight (), M, true);
return BM1;
} catch (OutOfMemoryError ex) {
}
return null;
}
Method 2. Using Canvas.drawbitmap
Bitmapadjustphotorotation (Bitmap BM, final int orientationdegree) {
Matrix M = new Matrix ();
M.setrotate (Orientationdegree, (float) bm.getwidth ()/2, (float) bm.getheight ()/2);
float Targetx, targety;
if (Orientationdegree = = 90) {
Targetx = Bm.getheight ();
targety = 0;
} else {
Targetx = Bm.getheight ();
Targety = Bm.getwidth ();
}
Final float[] values = new float[9];
M.getvalues (values);
float x1 = values[matrix.mtrans_x];
float y1 = values[matrix.mtrans_y];
M.posttranslate (targetx-x1, targety-y1);
Bitmap bm1 = Bitmap.createbitmap (Bm.getheight (), Bm.getwidth (), Bitmap.Config.ARGB_8888);
Paint paint = new paint ();
Canvas canvas = new canvas (BM1);
Canvas.drawbitmap (BM, m, paint);
return BM1;
}
Performance test:
1. Mobile phone
cpu:mtk6575, 1G Hz
Mem:512mb
Os:andoid 2.3.7
2. Picture size 1632 * 1224
Results:
1. Method 1 in 280-350 milliseconds, Method 2 is around 110 milliseconds.
2. Method 2 is better than Method 1
Two ways to rotate bitmap Android