Android calls system camera and crops

Source: Internet
Author: User

To do a program ape has a period of time, things have done a lot, today just opened a blog, I have done before the record down, today first write Android system down with the system camera and cut

Before making a call to the system camera has encountered a lot of problems, and now paste code, in the code to put a little

Btn.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
Intent Intent;
String fileName = System.currenttimemillis () + ". png";
Logotemppath = Logo_base_path + fileName;
Intent = new Intent (mediastore.action_image_capture);
Intent.putextra (Mediastore.extra_output, Uri.fromfile (New File (Logo_base_path, fileName));
Startactivityforresult (Intent, Photo_camera);
}
});

The above is to click on a button to call the system camera, here note a little:

String fileName = System.currenttimemillis () + ". png"; The name of the image is named because of the previous problem, that is, after the photo is cropped, the picture is the last shot, Now change the temporary file name to be generated at any time.

----------------------------------------------------------------------------

if (Requestcode = = Photo_camera) {//Photo capture
int angle = Readpicturedegree (Logotemppath);//Some mobile phones will rotate after the photo is taken, here is the angle to get the rotation of the photo, the content is posted in the back
if (angle! = 0)
Rotatephoto (angle);//After getting the rotation angle, rotate the picture back
File File = new file (Logotemppath);
Startphotozoom (file) (uri.fromfile);//crop the picture, paste it behind the code

}

The above is photographed after the Onactivityresult method to get the data and call the crop, the above recall when the photo has been stored in a fixed location, so here directly from the location (Logotemppath) reading data, not from the intent to get

The reason for this is that some mobile phones will be returned with empty data after taking pictures.

----------------------------------------------------------------------------

public void Startphotozoom (Uri uri) {
Intent Intent = new Intent ("Com.android.camera.action.CROP");
Intent.setdataandtype (URI, "image/*");
Intent.putextra ("Crop", "true");

The aspectx,aspecty here is the ratio of wide to high
Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
Outputx,outputy Online Search said is cut wide, in fact, personal feeling is not, specifically what I do not know
Intent.putextra ("Outputx", 200);
Intent.putextra ("Outputy", 200);
Intent.putextra ("Return-data", false);

Intent.putextra ("OutputFormat", Bitmap.CompressFormat.PNG.toString ());
Intent.putextra (Mediastore.extra_output, Uri.fromfile (New File (Logo_zoom_file_path)));

Startactivityforresult (Intent, Photo_result);
}

The above is a clipping method, note here a little Intent.putextra ("Return-data", false); it is not set to true, so you cannot get data from intent after cropping.

Have done before set to true, but some mobile phone after the picture occupied a large amount of memory, in the call crop will be reported to Oom, this is modified to not return data, but directly to save the data in their own settings location

There's no problem with oom after this change.

----------------------------------------------------------------------------

if (Requestcode = = Photo_result) {//after cropping
Bitmap Bitmap = Bitmapfactory.decodefile (Logo_zoom_file_path);
Gets the picture after cropping.

-----------------------------------------------------------------------------

public int Readpicturedegree (String path) {
int degree = 0;
try {
Exifinterface exifinterface = new Exifinterface (path);
int orientation = Exifinterface.getattributeint (exifinterface.tag_orientation, exifinterface.orientation_normal);
Switch (orientation) {
Case EXIFINTERFACE.ORIENTATION_ROTATE_90:
degree = 90;
Break
Case EXIFINTERFACE.ORIENTATION_ROTATE_180:
degree = 180;
Break
Case EXIFINTERFACE.ORIENTATION_ROTATE_270:
degree = 270;
Break
}
} catch (IOException e) {
E.printstacktrace ();
}
return degree;
}

Get the angle of the picture rotation after taking a photo

----------------------------------------------------------------------------

public void Rotatephoto (int angle) {

WindowManager wm = Getwindowmanager ();
int windowwidth = Wm.getdefaultdisplay (). GetWidth ();
int windowheight = Wm.getdefaultdisplay (). GetHeight ();

Bitmapfactory.options opts = new Bitmapfactory.options ();
Opts.injustdecodebounds = true;
Bitmapfactory.decodefile (Logotemppath, opts);
Opts.inpreferredconfig = Bitmap.Config.RGB_565;
int bitmapheight = Opts.outheight;
int bitmapwidth = Opts.outwidth;
if (Bitmapheight > WindowHeight | | bitmapwidth > WindowWidth) {
int ScaleX = Bitmapwidth/windowwidth;
int ScaleY = Bitmapheight/windowheight;
if (ScaleX > ScaleY)//scale in horizontal direction
Opts.insamplesize = ScaleX;
else//scale in vertical direction
Opts.insamplesize = ScaleY;
} else {//picture does not scale smaller than the phone screen
Opts.insamplesize = 1;
}
Opts.injustdecodebounds = false;
Matrix M = new Matrix ();
M.postrotate (angle);
Bitmap Bitmap = null;
try {
Bitmap = Bitmapfactory.decodefile (Logotemppath, opts);
if (bitmap = = null)
Return
Bitmap = Bitmap.createbitmap (bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight (), M, true);//Regenerate picture
SaveToFile (bitmap)///Save the newly generated picture at the specified location for later cropping, and the specific code will not post
} catch (OutOfMemoryError e) {
System.GC ();
} finally {
if (bitmap! = null)
Bitmap.recycle ();
}
}

The above is the rotation of the picture and then go back to create a new picture, save to the specified location

----------------------------------------------------------------------------

Finally, one more question I have recently encountered, the problem is described as follows:

After calling the camera, press the home button to exit the program, open the camera on the phone to take a few photos, exit the camera to re-enter the program, click the Photo Confirmation button, jump to the cutting interface when the interface is black, no photo display.

After an afternoon of troubleshooting, and finally found the reason, after the operation of this step, the previous call to save the image location and the name of the crop is no longer there is logotemppath in the return program is already empty, and this program's activity to re-oncreate method,

So the Logotemppath in the program is empty, the workaround:

@Override
protected void Onsaveinstancestate (Bundle outstate) {
Super.onsaveinstancestate (outstate);
Outstate.putstring ("Logotemppath", Logotemppath);
}

Save the previously set information and retrieve it the next time you enter the OnCreate method of the program.

----------------------------------------------------------------------------

The above is all the content, the first time to write a blog without experience, the shortcomings are also looking at the ^_^

Android calls system camera and crops

Related Article

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.