Android calls the System camera program and selects images from the image library. After returning the image, it calls the system cropping tool.

Source: Internet
Author: User

Call System photo

Intent I = new intent (mediastore. action_image_capture );

File myimagedir = new file (temp_take_photo_file_path );

// Create an image storage directory
If (! Myimagedir. exists ())
{
Mylog. D (this, "create the path:" + temp_take_photo_file_path );
Myimagedir. mkdirs ();
}

// Name by Time
Imagfile = file. createtempfile ("" + system. currenttimemillis (), ". jpg", myimagedir );

Tmpuri = URI. fromfile (imagfile );

I. putextra (mediastore. extra_output, tmpuri );

Startactivityforresult (I, take_photo_request_code );

Select an image from the image library

Intent innerintent = new intent (intent. action_get_content); // "android. Intent. Action. get_content"

Innerintent. settype ("image/*"); // view type

// Stringimage_unspecified = "image/*"; the detailed types are in COM. Google. Android. mms. contenttype.

Intent wrapperintent = intent. createchooser (innerintent, null );

Act. startactivityforresult (wrapperintent, select_photo_request_code );

Receive and call the system cropping Tool

@ Override
Protected void onactivityresult (INT requestcode, int resultcode,
Intent intent ){
If (requestcode = mediahelper. take_photo_request_code | requestcode = mediahelper. select_photo_request_code ){

If (resultcode = result_ OK ){
Uri uri = NULL;
If (requestcode = mediahelper. select_photo_request_code ){
Uri = intent. getdata ();
} Else if (requestcode = mediahelper. take_photo_request_code ){
Uri = mediahelper. tmpuri;
}

If (Uri! = NULL ){

Final intent intent1 = new intent ("com. Android. Camera. Action. Crop ");
Intent1.setdataandtype (Uri, "image /*");
Intent1.putextra ("crop", "true ");
Intent1.putextra ("aspectx", 1 );
Intent1.putextra ("aspecty", 1 );
Intent1.putextra ("outputx", 132 );
Intent1.putextra ("outputy", 132 );
Intent1.putextra ("Return-Data", true );
Startactivityforresult (intent1, mediahelper. cut_photo_request_code );

}
}

Else if (requestcode = mediahelper. cut_photo_request_code ){
If (resultcode = result_ OK & intent! = NULL ){
Bm = intent. getparcelableextra ("data ");

}

}

}

When cropping an image, some images cannot be cropped according to a specified proportion. After checking the source code, you can see that the system's cropping image performs face recognition on the image by default, when a face is identified, it will be processed according to aspectx and aspecty as 1. If you want to set the custom cropping ratio, you need to set nofacedetection to true.

That is, iintent. putextra ("nofacedetection", true); cancels the face recognition function.

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.