android front and back camera captured picture orientation issue, rotated in a wrong way

后端 未结 5 1285
一整个雨季
一整个雨季 2021-02-05 10:12

I have a camera app in portrait mode which takes pictures from both front and back end cameras.The issue is like the captured images are rotated in a wrong way...

For pr

相关标签:
5条回答
  • 2021-02-05 10:36

    try this code snippet

    try {
    
        ExifInterface exif = new ExifInterface(filePath);
        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        //Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
        Log.v("log", "ort is "+orientation);
    
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    

    and then rotate the matrix as per orientation you get

    if (orientation==6)
    {
        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    }
    else if (orientation==8)
    {
        Matrix matrix = new Matrix();
        matrix.postRotate(270);
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    }
    
    else if (orientation==3)
    {
        Matrix matrix = new Matrix();
        matrix.postRotate(180);
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true;
    }
    
    0 讨论(0)
  • 2021-02-05 10:52

    You can refer below code.

    ExifInterface exif = new ExifInterface(SourceFileName);     //Since API Level 5
    
    String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    

    And also refer this link https://stackoverflow.com/a/6124375/1441666

    0 讨论(0)
  • 2021-02-05 10:55

    I'm using the following code for this:

    ExifInterface exif = new ExifInterface(getUriPath(uri));
    int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
    

    getUriPath:

    public String getUriPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null,
                null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(projection[0]);
            cursor.moveToFirst();
            String filePath = cursor.getString(column_index);
            cursor.close();
            return filePath;
        } else
            return null;
    }
    
    0 讨论(0)
  • 2021-02-05 10:58

    You can use this to get orientation from a Uri

                        String filePath = mImageUri.getPath();
                    if (filePath != null) {
                        rotation = -1;
                            ExifInterface exif = new ExifInterface(filePath); // Since
                                                                                // API
                                                                                // Level
                                                                                // 5
                            rotation = Integer.parseInt(exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                            // //Log.v("roation",
                            // exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                        }
    
                    }
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show();
                    Log.e(e.getClass().getName(), e.getMessage(), e);
                }
    

    And as note rotation '3 = 180, 6 = 90, 8 = 270'

    0 讨论(0)
  • 2021-02-05 10:58

    the selected answer just gives the the possible rotation that may have been saved in EXIF header. in several cases camera doesn't set "ExifInterface.TAG_ORIENTATION" attribute in EXIFHeader so it will be 0(ExifInterface.ORIENTATION_UNDEFINED). and event if it is set it will be true in only one case/orientation when the picture is taken. you have to manually set rotation in camera parameters using setRotation() method. the documentation of setRotation() is very clear and also explains how to calculate rotation with consideration of device rotation and camera sensor orientation(usually landscape).

    so check out setRotation() method . thats what you need to change.

    0 讨论(0)
提交回复
热议问题