Android Two rotation bitmap method comparison

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.