Solve the camera photo is not clear, photo rotation problem!

Source: Internet
Author: User
Tags crop image

Private URI Uri;

Photo

Intent Getimagebycamera = new Intent ("Android.media.action.IMAGE_CAPTURE");

Contentvalues values = new Contentvalues ();
Values.put (MediaStore.Images.Media.DISPLAY_NAME, String.valueof (System.currenttimemillis ()) + ". jpg");
Values.put (MediaStore.Images.Media.MIME_TYPE, "image/png");
URI = Getcontentresolver (). Insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Getimagebycamera.putextra (MediaStore.Images.Media.ORIENTATION, 0);
Getimagebycamera.putextra (Mediastore.extra_output, URI);

Startactivityforresult (Getimagebycamera,request_camera);

Get pictures of photos

if (Requestcode = = Request_camera) {//Camera get Photos
String Path = Tool.getpathfromuri (context, URI);
int orientation = tool.readpicturedegree (path);//Get rotation angle
APPLOG.D ("Dingdongkai", "orientation===" +orientation);
if (Math.Abs (orientation) > 0) {
Bitmap bit = Getbitmapfromuri (URI);
Bitmap = Tool.rotaingimageview (orientation, bit);//Rotate Picture
}
Startphotozoom (Uri.parse (MediaStore.Images.Media.insertImage (Getcontentresolver (), Bitmap, null,null));

Get picture path based on URI

public static String Getpathfromuri (Context Mcontext,uri Contenturi) {
APPLOG.D ("Dingdongkai", "contenturi===" +contenturi.tostring ());
String[] proj = {MediaStore.Images.Media.DATA};
Cursorloader loader = new Cursorloader (Mcontext, Contenturi, proj, NULL, NULL, NULL);
cursor cursor = Loader.loadinbackground ();
int column_index = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);
Cursor.movetofirst ();
Return cursor.getstring (Column_index);
}

Get the rotation angle of the picture
public static 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;
}

/**
* Crop Image Method implementation
*
* @param URI
*/
public void Startphotozoom (Uri uri) {
Set crop properties
Intent Intent = new Intent ("Com.android.camera.action.CROP");
Intent.setdataandtype (URI, "image/*");
Intent.putextra ("Crop", "true");
Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
Intent.putextra ("Outputx", 320);
Intent.putextra ("Outputy", 320);
Intent.putextra ("Return-data", true);
Startactivityforresult (Intent, 6);
}

Solve the camera photo is not clear, photo rotation problem!

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.