关于后置摄像头拍照后照片方向的总结

12 篇文章 0 订阅

开发android Camera的时候,发现一个问题,就是拍照后图片的方向是错的。如何避免他?可以读取照片的exif信息,exif信息存储了包括时间,照片方向等照片的全部信息。具体步骤如下。

 ExifInterface exif = null;
        String TAG_ORIENTATION=null;
        try {
            exif = new ExifInterface(getPhotoPath());
            TAG_ORIENTATION=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
            Log.d(TAG,"exif方向:  "+TAG_ORIENTATION);
        } catch (IOException e) {
            e.printStackTrace();  
        }


getPhotoPath() 方法是获得图片的路径,比如/sdcard/1.jpg

我用四个方向进行拍照。经过打印发现,打印的结果分别是6,8,3,1

经过源码查询发现,这几个值是系统定义的方向分别对应应ExifInterface.ORIENTATION_ROTATE_90,ExifInterface.ORIENTATION_ROTATE_270,ExifInterface.ORIENTATION_ROTATE_180和0度

竖屏模式下,逆时针旋转90度,exif打印结果是1,就是0度,就是摄像头的正方向,由此可见android手机的摄像头是横屏方向安装上的

因此我们可以根据exif信息旋转bitmap。

try {   
           ExifInterface exifInterface = new ExifInterface(file.getPath());   
           int result = exifInterface.getAttributeInt(   
                   ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);   
           int rotate = 0;   
           switch(result) {   
           case ExifInterface.ORIENTATION_ROTATE_90:   
               rotate = 90;   
               break;   
           case ExifInterface.ORIENTATION_ROTATE_180:   
               rotate = 180;   
               break;   
           case ExifInterface.ORIENTATION_ROTATE_270:   
               rotate = 270;   
               break;   
           default:   
               break;   
           }   
      catch (IOException e) {   
           e.printStackTrace();   
       } 
下面是旋转bitmap的方法
public static Bitmap rotate(Bitmap b, int degrees,String filePath, int reqWidth, int reqHeight) {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();
            m.setRotate(degrees,
                    (float) b.getWidth() / 2, (float) b.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(
                        b, 0, 0, b.getWidth(), b.getHeight(), m, true);
                if (b != b2) {
                    b.recycle();  
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
                // 建议大家如何出现了内存不足异常,最好return 原始的bitmap对象。.
                return  getSampledBitmap(filePath, reqWidth, reqHeight);
            }
        }
        return b;
    }


public static Bitmap getSampledBitmap(String filePath, int reqWidth, int reqHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(filePath, options);

        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = (int) FloatMath.floor(((float) height / reqHeight) + 0.5f); //Math.round((float)height / (float)reqHeight);
            } else {
                inSampleSize = (int) FloatMath.floor(((float) width / reqWidth) + 0.5f); //Math.round((float)width / (float)reqWidth);
            }
        }

        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值