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

来源:互联网 发布:java的计算器源代码 编辑:程序博客网 时间:2024/04/28 15:23

开发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);    }




原创粉丝点击