简体   繁体   English

使用exif旋转图像

[英]Using exif to rotate an image

i was following this link Android get Orientation of a camera Bitmap? 我正在关注此链接Android获取相机位图的方向? And rotate back -90 degrees to rotate my image if/when necessary, but its not working for me, i get this error 并在需要时向后旋转-90度以旋转我的图像,但对我不起作用,我收到此错误

05-28 23:29:30.049 9735-9735/ss.sealstudios.com.socialstories E/JHEAD: can't open '/document/508' 05-28 23:29:30.049 9735-9735 / ss.sealstudios.com.socialstories E / JHEAD:无法打开'/ document / 508'

but checking it like below looks like there pointing in the right place 但是像下面这样检查它看起来像是指向正确的地方

05-28 23:29:30.049 9735-9735/ss.sealstudios.com.socialstories I/System.out: bitmap uri content://com.android.providers.downloads.documents/document/508 0 05-28 23:29:30.049 9735-9735 / ss.sealstudios.com.socialstories I / System.out:位图uri content://com.android.providers.downloads.documents/document/508 0

//the 0 on the end is me checking for the rotation //最后的0是我检查旋转

i was wondering if this was build version specific like getting the real path from a URI as running this code on my marshmallow device gives me an entirely different result (this result, ie: error codes, is from a kitkat device), but this has defeated me for some weeks now following all sorts of answers to no avail, can anyone weigh in and help me please, here's what I'm trying to do 我想知道这是否是特定于构建版本的,例如从URI获取真实路径,因为在我的棉花糖设备上运行此代码会给我一个完全不同的结果(此结果,即:错误代码来自kitkat设备),但是在无数种无济于事的答案之后,我击败了我几个星期,任何人都可以称重并帮助我,这就是我想要做的

   if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data 
   != null 
   && data.getData() != null) 
  {

        Uri uri = data.getData();


        try {
            BitmapFactory.Options bitmapOptions = new 
            BitmapFactory.Options();
            bitmapOptions.inSampleSize = (int) 4;
            InputStream inputStream = 
            getContentResolver().openInputStream(uri);
            Bitmap scaledBitmap = BitmapFactory.decodeStream(inputStream, 
            null, bitmapOptions);
            ExifInterface exif = new ExifInterface(uri.getPath());
            int rotation = 
            exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,  
            ExifInterface.ORIENTATION_NORMAL);
            int rotationInDegrees = exifToDegrees(rotation);
            System.out.println("bitmap uri " + uri + " " + rotation);
            Matrix matrix = new Matrix();

            if (rotation != 0f){
                matrix.preRotate(rotationInDegrees);
                Bitmap.createBitmap(scaledBitmap, 0, 
                0,scaledBitmap.getWidth(),scaledBitmap.getHeight(), 
                matrix, true);
                imageView.setImageBitmap(scaledBitmap);

            }else
                imageView.setImageBitmap(scaledBitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
private static int exifToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { 
    return 90; 
    }
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {   
    return 180; }
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  
    return 270; }
    return 0;
}

many thanks 非常感谢

ExifInterface exif = new ExifInterface(uri.getPath());

This will not work. 这是行不通的。 If you call getPath() on a Uri , you are doing it wrong, as that is meaningless for most Uri values, particularly those with a content scheme, as your has. 如果在Uri上调用getPath() ,则说明这样做是错误的,因为这对于大多数Uri值(尤其是具有content方案的Uri值)毫无意义。

Since the SDK's ExifInterface only works with local files, you will need other EXIF code. 由于SDK的ExifInterface仅适用于本地文件,因此您将需要其他EXIF代码。 I use some code from the AOSP for this . 为此使用了AOSP中的一些代码 That version of ExifInterface can work with an InputStream , so you can get a fresh InputStream for the content identified by the Uri and pass that to the alternative ExifInterface . 该版本的ExifInterface可以与InputStream一起使用,因此您可以为Uri标识的内容获取一个新的InputStream ,并将其传递给替代的ExifInterface

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM