三星手机拍照后显示图片角度旋转问题的解决方法

来源:互联网 发布:开淘宝用什么电脑配置 编辑:程序博客网 时间:2024/04/29 13:26

          项目开发中,需要用一个ImageView显示拍照的图片。在使用三星手机进行测试的时候发现图片角度发生了旋转,经资料查询,这是因为三星手机拍照的图片旋转角度是90度,而其他手机是0度。这样思路就出来了:先查询被旋转了多少度,然后再旋转回来。ok,下面上代码。


首先是读取图片被旋转的角度。

/** * 读取照片exif信息中的旋转角度 * @param path  照片路径 * @return 角度 */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;}

再接着是将图片旋转回0度。

public Bitmap toturn(Bitmap img, String path) {Matrix matrix = new Matrix();matrix.postRotate(readPictureDegree(path)); int width = img.getWidth();int height = img.getHeight();img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);return img;}





1 0
原创粉丝点击