Cropped photos of Android development

Source: Internet
Author: User

Please respect other people's labor results, reproduced please specify the source: Cropped photos of Android development


1. Select photos from albums to crop

Select a photo from the album and crop it:

/** * Select photos from albums to crop */private void Cropfromgallery () {    //TODO auto-generated method stub        Intent intent=new Intent () ;    Intent.setaction (Intent.action_pick);//pick an item from the data    intent.settype ("image/*");//select    from all pictures Intent.putextra ("Crop", "true");//set to crop    Intent.putextra ("Aspectx", 1);//Cut the width ratio    Intent.putextra ("Aspecty" , 1);//cutting the high proportion of    Intent.putextra ("Outputx", 600);//cutting width    Intent.putextra ("Outputy", 600);//Cutting height    Intent.putextra ("scale", true);//Supports scaling    Intent.putextra ("Return-data", false);    Intent.putextra (Mediastore.extra_output, Imageuri);//outputs the cropped result to the specified URI    intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ());//Cut the format of the picture    Intent.putextra ("Nofacedetection", true);//No Face Detection    Startactivityforresult (Intent, select_pic);   }


display the cut photos in the Imagaview on:

Case Select_pic:    if (RESULTCODE==RESULT_OK) {        try {            Bitmap bitmap=bitmapfactory.decodestream ( Getcontentresolver ().                    Openinputstream (Imageuri));//load a picture of the Imageuri object into memory            imgshow.setimagebitmap (bitmap);        } catch ( FileNotFoundException e) {            //TODO auto-generated catch block            e.printstacktrace ();        }    }    Break

Program run:



2. Take photos from the camera to crop

Control the camera to take pictures and save the photos to the specified location:

/** * Take photos from camera to crop */private void Cropfromtake () {    //TODO auto-generated method stub    Intent intent=new Intent (); 
   intent.setaction (mediastore.action_image_capture);//Set ACTION to take photo    Intent.putextra (Mediastore.extra_ OUTPUT, Imageuri);//Save the captured photo to the specified URI    startactivityforresult (Intent, take_pic);}

crop the already-lined photos and display them in ImageView on:

Case Take_pic:    if (RESULTCODE==RESULT_OK) {        Cropimageuri (Imageuri, N, +, crop_pic);    }    Break


/** * Crop the photo corresponding to the specified URI * @param imageuri:uri corresponding photo * @param outputx: Crop width * @param outputy: Crop high * @param requestcode: Request Code */private void Cropimageuri (Uri imageuri, int outputx, int outputy, int requestcode) {    Intent Intent = new Intent ("COM.ANDROID.C Amera.action.CROP ");    Intent.setdataandtype (Imageuri, "image/*");    Intent.putextra ("Crop", "true");    Intent.putextra ("Aspectx", 1);    Intent.putextra ("Aspecty", 1);    Intent.putextra ("Outputx", OUTPUTX);    Intent.putextra ("Outputy", outputy);    Intent.putextra ("scale", true);    Intent.putextra (Mediastore.extra_output, Imageuri);    Intent.putextra ("Return-data", false);    Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ());    Intent.putextra ("Nofacedetection", true); No Face Detection    startactivityforresult (Intent, requestcode);}

Program run:



3. Complete Project Code:
Package Com.jph.cp;import Java.io.file;import Java.io.filenotfoundexception;import Android.support.v7.app.actionbaractivity;import Android.content.intent;import Android.graphics.Bitmap;import Android.graphics.bitmapfactory;import Android.net.uri;import Android.os.bundle;import android.os.Environment; Import Android.provider.mediastore;import android.view.view;import android.widget.imageview;/** * Select photos from the album to Crop, Take photo from camera to crop * @author JPH * date:2014.10.09 */public class Mainactivity extends Actionbaractivity {private final static in T select_pic=0x123; Private final static int take_pic=0x124; Private final static int crop_pic=0x125; Private Uri imageuri;private ImageView imgshow; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Initialize Imageuriimageuri=uri.fromfile ( New File (Environment.getexternalstoragedirectory (), "test.jpg")), imgshow= (ImageView) Findviewbyid (r.id.imgshow);} @Overrideprotected void OnactivityreSult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method Stubswitch (requestcode) {case SELECT_PI C:if (RESULTCODE==RESULT_OK) {try {Bitmap Bitmap=bitmapfactory.decodestream (Getcontentresolver (). OpenInputStream ( Imageuri));//load a picture of the Imageuri object into memory Imgshow.setimagebitmap (bitmap);} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Break;case Take_pic:if (RESULTCODE==RESULT_OK) {Cropimageuri (Imageuri, +, +, crop_pic);} Break;case Crop_pic:if (RESULTCODE==RESULT_OK) {try {Bitmap Bitmap=bitmapfactory.decodestream (GetContentResolver (). Openinputstream (Imageuri));//load a picture of the Imageuri object into memory Imgshow.setimagebitmap (bitmap);} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Break;default:break;} Super.onactivityresult (Requestcode, ResultCode, data);}  /** * Crop the photo corresponding to the specified URI * @param imageuri:uri corresponding photo * @param outputx: Crop width * @param outputy: Crop high * @param requestcode: Request Code */private void Cropimageuri (Uri imageuri, int OUTPUTX, int outputy, int requestcode) {Intent Intent = new Intent ("Com.android.camera.action.CROP");    Intent.setdataandtype (Imageuri, "image/*");    Intent.putextra ("Crop", "true");    Intent.putextra ("Aspectx", 1);    Intent.putextra ("Aspecty", 1);    Intent.putextra ("Outputx", OUTPUTX);    Intent.putextra ("Outputy", outputy);    Intent.putextra ("scale", true);    Intent.putextra (Mediastore.extra_output, Imageuri);    Intent.putextra ("Return-data", false);    Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection", true); No Face Detection Startactivityforresult (intent, requestcode);} public void Croppic (view view) {switch (View.getid ()) {case r.id.btncropfromgallery://Select a photo from the album to Crop Cropfromgallery (); Break;case r.id.btncropfromtake://take photos from the camera to crop cropfromtake (); break;default:break;}} /** * Take photos from camera to crop */private void Cropfromtake () {//TODO auto-generated method Stubintent intent=new Intent (); Intent.setac tion (mediastore.action_image_capture);//Set action to take picture Intent.putextra (Mediastore.extra_output, Imageuri);// Save the captured photos to the designated Uristartactivityforresult (intent, take_pic);} /** * Select photos from albums to crop */private void Cropfromgallery () {//TODO auto-generated method Stubintent intent=new Intent (); intent.se Taction (Intent.action_pick);//pick an item from the Dataintent.settype ("image/*");//select from all pictures Intent.putextra ("crop "," true ");//set to Crop Intent.putextra (" Aspectx ", 1),//Cut the width of the Intent.putextra (" Aspecty ", 1);//Cut the high proportion of Intent.putextra (" Outputx ", 600);//Cut width Intent.putextra (" outputy ", 600);//Cut height Intent.putextra (" scale ", true);//Support Zoom Intent.putextra ("Return-data", false); Intent.putextra (Mediastore.extra_output, Imageuri);//outputs the cropped result to the specified Uriintent.putextra (" OutputFormat ", Bitmap.CompressFormat.JPEG.toString ());//Cut the format of the picture Intent.putextra (" Nofacedetection ", true);    No face Detectionstartactivityforresult (intent, select_pic); }}


Cropped photos of Android development

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.