Android Bitmap Common Several operations: zoom, crop, rotate, offset

Source: Internet
Author: User

Android Bitmap Related actions

Several common operations: zoom, crop, rotate, offset

Many operations require the matrix to support the matrix, which processes the bitmap and calculates the location of each pixel to display the bitmap.
There is a 3x3 matrix in the matrix for image processing:

mscale_x mskew_x  mtrans_xmskew_y  mscale_y mtrans_ympersp_0 mpersp_1 mpersp_2

The specific purpose can be guessed according to the variable name:
Zoom x Offset x Shift x
Offset y scale y shift y
Pivot 0 Pivot 1 Perspective 2

Matrix operations have Set,pre and Post;set to directly set the values in the matrix; the pre is similar to the matrix left multiply; The post is similar to the right multiplication in the matrix.

Once the original bitmap is calculated, a bitmap is regenerated

Code snippet:

    /*** Stretch according to a given width and height *@paramOriginal Origin *@paramNewwidth The width of the new chart *@paramnewheight height of the new chart *@returnNew Bitmap*/    PrivateBitmap Scalebitmap (Bitmap origin,intNewwidth,intnewheight) {        if(Origin = =NULL) {            return NULL; }        intHeight =origin.getheight (); intwidth =origin.getwidth (); floatScaleWidth = ((float) newwidth)/width; floatScaleHeight = ((float) newheight)/height; Matrix Matrix=NewMatrix (); Matrix.postscale (ScaleWidth, scaleheight);//use post-multiplyBitmap NEWBM = Bitmap.createbitmap (origin, 0, 0, width, height, matrix,false); if(!origin.isrecycled ())        {origin.recycle (); }        returnNEWBM; }    /*** Proportionally scaled picture * *@paramOriginal Origin *@paramratio ratio *@returnthe new bitmap*/    PrivateBitmap Scalebitmap (Bitmap origin,floatratio) {        if(Origin = =NULL) {            return NULL; }        intwidth =origin.getwidth (); intHeight =origin.getheight (); Matrix Matrix=NewMatrix ();        Matrix.prescale (ratio, ratio); Bitmap NEWBM= Bitmap.createbitmap (origin, 0, 0, width, height, matrix,false); if(Newbm.equals (Origin)) {returnNEWBM;        } origin.recycle (); returnNEWBM; }    /*** Crop * *@paramBitmap Original *@returnCropped image*/    PrivateBitmap Cropbitmap (Bitmap Bitmap) {intW = bitmap.getwidth ();//get the picture wide and high        inth =bitmap.getheight (); intCropwidth = w >= h? H:w;//edge length of square area taken after croppingCropwidth/= 2; intCropheight = (int) (cropwidth/1.2); returnBitmap.createbitmap (Bitmap, W/3, 0, Cropwidth, Cropheight,NULL,false); }    /*** Select Transform * *@paramOriginal Origin *@paramAlpha Rotation angle, can be negative *@returnthe rotated picture*/    PrivateBitmap Rotatebitmap (Bitmap origin,floatAlpha) {        if(Origin = =NULL) {            return NULL; }        intwidth =origin.getwidth (); intHeight =origin.getheight (); Matrix Matrix=NewMatrix ();        Matrix.setrotate (Alpha); //rotate around in situBitmap NEWBM = Bitmap.createbitmap (origin, 0, 0, width, height, matrix,false); if(Newbm.equals (Origin)) {returnNEWBM;        } origin.recycle (); returnNEWBM; }    /*** Offset Effect *@paramOriginal Origin *@returnbitmap after the offset*/    PrivateBitmap Skewbitmap (Bitmap origin) {if(Origin = =NULL) {            return NULL; }        intwidth =origin.getwidth (); intHeight =origin.getheight (); Matrix Matrix=NewMatrix (); Matrix.postskew (-0.6f, -0.3f); Bitmap NEWBM= Bitmap.createbitmap (origin, 0, 0, width, height, matrix,false); if(Newbm.equals (Origin)) {returnNEWBM;        } origin.recycle (); returnNEWBM; }

The operation definition of the button:

@Override Public voidOnClick (View v) {Bitmap ORIGINBM=Bitmapfactory.decoderesource (Getresources (), r.drawable.littleboygreen_x128); Switch(V.getid ()) { CaseR.ID.BTN1: {//scale by SizeEffecttextview.settext (R.string.scale); Bitmap NBM= Scalebitmap (ORIGINBM, 100, 72);                Effectview.setimagebitmap (NBM);  Break; }             CaseR.ID.BTN2: {//Scaled proportionally , each click will be differentEffecttextview.settext (R.string.scale_ratio); if(ratio < 3) {ratio+ = 0.05f; } Else{ratio= 0.1f; } Bitmap NBM=Scalebitmap (ORIGINBM, ratio);                Effectview.setimagebitmap (NBM);  Break; }             CaseR.ID.BTN3: {//croppingEffecttextview.settext ("Cut the head"); Bitmap Cropbitmap=Cropbitmap (ORIGINBM);                Effectview.setimagebitmap (CROPBITMAP);  Break; }             CaseR.ID.BTN4: {//clockwise rotation effect; update rotation angle per click                if(Alpha < 345) {Alpha+ = 15; } Else{Alpha= 0; } effecttextview.settext (Rotation); Bitmap Rotatebitmap=Rotatebitmap (ORIGINBM, Alpha);                Effectview.setimagebitmap (ROTATEBITMAP);  Break; }             CaseR.id.btn5: {//counter-clockwise rotation effect; update rotation angle per click                if(Beta > 15) {Beta-= 15; } Else{Beta= 360; } effecttextview.settext (Rotation); Bitmap Rotatebitmap=rotatebitmap (ORIGINBM, beta);                Effectview.setimagebitmap (ROTATEBITMAP);  Break; }             CaseR.ID.BTN6: {//Offset effect; offset in methodBitmap SKEWBM =Skewbitmap (ORIGINBM);                Effectview.setimagebitmap (SKEWBM);  Break; }        }    }

Problems encountered

New Matrix (); Matrix.prescale (ratio, ratio); // when Ratio=1, the following NEWBM will be equivalent to Origin false ); if (! origin.isrecycled ()) {origin.recycle ();}

Log as follows, when Ratio=1, the new bitmap and the old bitmap the same address


11-27 05:27:16.086 16723-16723/? D/rust:originbitmap = [email protected]
11-27 05:27:16.086 16723-16723/? D/rust:newbitmap = [email protected]

Android Bitmap Common Several operations: zoom, crop, rotate, offset

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.