从三星手机中获取图片颠倒的问题

问题所在

最近做了一个需求,从手机相册中获取图片并进行显示,做完测试都没有问题,后来突然发现三星的手机取出的图片是左转了90度的。wtf~~。(文章转自自己的博客,这里的最近其实是16年5月)

下面这个是不正常的(状态栏请无视,随便拿了个公司的测试机)


从三星手机中获取图片颠倒的问题_第1张图片

正常应该是这样的


从三星手机中获取图片颠倒的问题_第2张图片

解决方案

我们需要用到一个叫做 ExifInterface的类,google官方对其的描述是:

This is a class for reading and writing Exif tags in a JPEG file or a RAW image file.

Supported formats are: JPEG, DNG, CR2, NEF, NRW, ARW, RW2, ORF and RAF.

Attribute mutation is supported for JPEG image files.

根据描述这是一个用来操作图片exiftag信息的类

解决的具体办法

不是所有的图片都需要处理,先检查

talk is cheap,show you my code

     public 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;
 } 

这是一个检查图片的旋转度数的方法(三星我遇到的都是90),所以,如果你收到返回为90.那就应该要进行处理。

既然你知道有问题了,是不是要处理呢?

talk is cheap,show you my code

public Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
       Bitmap returnBm = null;
       // 根据旋转角度,生成旋转矩阵
       Matrix matrix = new Matrix();
       matrix.postRotate(degree);
       try {
           // 将原始图片按照旋转矩阵进行旋转,并得到新的图片
           returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                   bm.getHeight(), matrix, true);
       } catch (OutOfMemoryError e) {
       }
       if (returnBm == null) {
           returnBm = bm;
       }
       if (bm != returnBm) {
           bm.recycle();
       }
       return returnBm;
   }

你需要把path转为bitmap,然后此方法是根据你传入的角度,进行旋转(检测获取到是多少,就传入多少就行)。

再提供一个path转bitmap的方法

 public static Bitmap getBitmapCompress720P(String pathName) {
        int targetWidth = 720;
        int targetHeight = 1080;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bitmap;
        
        float imgWidth = options.outWidth;
        float imgHeight = options.outHeight;
        
        int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);
        int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);
        options.inSampleSize = 1;
        
        if (widthRatio > 1 || widthRatio > 1) {
            if (widthRatio > heightRatio) {
                options.inSampleSize = widthRatio;
            } else {
                options.inSampleSize = heightRatio;
            }
        }
        
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(pathName, options);
        return bitmap;
       }

这个方法是path转bitmap。对图片进行了缩放,因为bitmap在set imageview的时候,其实你取出来的会很大,多半会失败,反正要压缩,不如这里压缩,上面方法是设置的720P的压缩,你可以直接改,也可以进行扩展。

注意事项

一般来说,需要从相册取图片的需求都伴随着上传,如果你不能直接bitmap上传,那可能还需要转回到path存到本地,这里是一个比较耗时的过程,建议使用异步,并且,在上传时去做转存。

你可能感兴趣的:(从三星手机中获取图片颠倒的问题)