Some Android phone photos are rotated after photo-solution

Source: Internet
Author: User

On some Android phones (such as MT788, Note2), photos taken with camera will be automatically rotated (90°, 180°, 270°), which is not expected. After careful analysis, because the photo attribute is stored in the rotation information, so to solve this problem, you can in the Onactivityresult method, get the photo data, read its rotation information, if not 0, indicating that the picture has been rotated, Then use Android.graphics.Matrix to rotate the photos back.

1, read the picture rotation properties

/** * 读取图片的旋转的角度 * * @param path *            图片绝对路径 * @return 图片的旋转角度 */privateintgetBitmapDegree(String path) {    intdegree = 0;    try{        // 从指定路径下读取图片,并获取其EXIF信息        ExifInterface exifInterface = newExifInterface(path);        // 获取图片的旋转信息        intorientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,                ExifInterface.ORIENTATION_NORMAL);        switch(orientation) {        caseExifInterface.ORIENTATION_ROTATE_90:            degree = 90;            break;        caseExifInterface.ORIENTATION_ROTATE_180:            degree = 180;            break;        caseExifInterface.ORIENTATION_ROTATE_270:            degree = 270;            break;        }    catch(IOException e) {        e.printStackTrace();    }    returndegree;}

2. Rotate the picture according to an angle

/** * 将图片按照某个角度进行旋转 * * @param bm *            需要旋转的图片 * @param degree *            旋转角度 * @return 旋转后的图片 */publicstaticBitmap rotateBitmapByDegree(Bitmap bm, int degree) {    Bitmap returnBm = null;     // 根据旋转角度,生成旋转矩阵    Matrix matrix = newMatrix();    matrix.postRotate(degree);    try{        // 将原始图片按照旋转矩阵进行旋转,并得到新的图片        returnBm = Bitmap.createBitmap(bm, 00, bm.getWidth(), bm.getHeight(), matrix, true);    catch (OutOfMemoryError e) {    }    if(returnBm == null) {        returnBm = bm;    }    if(bm != returnBm) {        bm.recycle();    }    returnreturnBm;}

OK, with the above two methods, it can be done!

Some Android phone photos are rotated after photo-solution

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.