Method 1. Using Bitmap.createbitmap
Bitmap Adjustphotorotation (Bitmap BM,Final intOrientationdegree) {Matrix m=NewMatrix (); 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); returnBM1; } Catch(OutOfMemoryError ex) {ex.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } return NULL; }
Method 2. Using Canvas.drawbitmap
Bitmap Adjustphotorotation (Bitmap BM,Final intOrientationdegree) { Try { intwidth =bm.getwidth (); intHeight =bm.getheight (); Matrix Matrix=NewMatrix (); Matrix.setrotate (Orientationdegree, (float) WIDTH/2, (float) HEIGHT/2); floatTargetx = 0; floattargety = 0; if(Orientationdegree = = | | orientationdegree = = 270) { if(Width >height) {Targetx= (float) HEIGHT/2-(float) WIDTH/2; Targety= 0-Targetx; } Else{targety= (float) WIDTH/2-(float) HEIGHT/2; Targetx= 0-targety; }} matrix.posttranslate (Targetx, targety); Bitmap BM1=Bitmap.createbitmap (Bm.getheight (), Bm.getwidth (), Bitmap.Config.ARGB_8888); Paint Paint=NewPaint (); Canvas Canvas=NewCanvas (BM1); Canvas.drawbitmap (BM, matrix, paint); returnBM1; } Catch(OutOfMemoryError e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } }
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