繁体   English   中英

使用exif旋转图像

[英]Using exif to rotate an image

我正在关注此链接Android获取相机位图的方向? 并在需要时向后旋转-90度以旋转我的图像,但对我不起作用,我收到此错误

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

但是像下面这样检查它看起来像是指向正确的地方

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

//最后的0是我检查旋转

我想知道这是否是特定于构建版本的,例如从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;
}

非常感谢

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

这是行不通的。 如果在Uri上调用getPath() ,则说明这样做是错误的,因为这对于大多数Uri值(尤其是具有content方案的Uri值)毫无意义。

由于SDK的ExifInterface仅适用于本地文件,因此您将需要其他EXIF代码。 为此使用了AOSP中的一些代码 该版本的ExifInterface可以与InputStream一起使用,因此您可以为Uri标识的内容获取一个新的InputStream ,并将其传递给替代的ExifInterface

暂无
暂无

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

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