Android crop picture size control picture size

Source: Internet
Author: User

Use Bitmapfactory to get a screen-sized picture and your own picture cropping tool

 PackageCom.lin.image; Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;  Public classImagescaleactivityextendsActivityImplementsview.onclicklistener{/**Called when the activity is first created.*/     PrivateButton selectimagebtn; PrivateButton cutimagebtn; PrivateImageView ImageView; Private Static Final intImage_select=1; Private Static Final intimage_cut=2; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                  Setcontentview (R.layout.main); SELECTIMAGEBTN=(Button) Findviewbyid (R.ID.SELECTIMAGEBTN); CUTIMAGEBTN=(Button) Findviewbyid (R.ID.CATIMAGEBTN); ImageView=(ImageView) Findviewbyid (R.id.imageview); Cutimagebtn.setonclicklistener ( This); Selectimagebtn.setonclicklistener ( This); } @Override Public voidOnClick (View v) {<span style= "White-space:pre" > </span>//capture images that fit the screen size        if(v==selectimagebtn) {Intent Intent=NewIntent (Intent.action_pick, Android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);         Startactivityforresult (Intent, image_select); }//typically used for avatars such as the need to set the specified size        Else if(v==cutimagebtn) {Intent Intent=getimageclipintent ();         Startactivityforresult (Intent, image_cut); }} @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) {         //TODO auto-generated Method Stub        if(resultcode==RESULT_OK) {             if(requestcode==image_select) {Uri Imagefileuri=Intent.getdata (); intdw=Getwindowmanager (). Getdefaultdisplay (). GetWidth (); intDh=getwindowmanager (). Getdefaultdisplay (). GetHeight ()/2; //screen width and general height as the maximum size of the picture display            Try{bitmapfactory.options Factory=Newbitmapfactory.options (); Factory.injustdecodebounds=true;//allow query pictures to not allocate memory for picture pixels when TrueBitmap bmp=Bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (Imagefileuri),NULL, Factory); intHratio= (int) Math.ceil (factory.outheight/(float) DH);//the picture is several times the height                intWratio= (int) Math.ceil (factory.outwidth/(float) (DW);//the picture is several times the widthSystem.out.println ("Hratio:" +hratio+ "Wratio:" +wratio); //reduced to 1/ratio size and 1/ratio^2 pixels                if(hratio>1| | Wratio>1){                     if(hratio>wratio) {Factory.insamplesize=Hratio; }                     Elsefactory.insamplesize=Wratio; } factory.injustdecodebounds=false; BMP=Bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (Imagefileuri),NULL, Factory);             Imageview.setimagebitmap (BMP); }Catch(Exception ex) {}}Else if(requestcode==image_cut) {Bitmap BMP=intent.getparcelableextra ("Data");             Imageview.setimagebitmap (BMP); }         }              }         /*** Get the picture after cut*/          Public StaticIntent getimageclipintent () {Intent Intent=NewIntent (Intent.action_get_content,NULL); Intent.settype ("Image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 1);//Crop Frame ScaleIntent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", 80);//Output picture sizeIntent.putextra ("Outputy", 80); Intent.putextra ("Return-data",true); returnIntent; }           }



Excerpt from Dikeboy1234 's column

Android crop picture size control picture size

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.