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

          项目开发中,须要用一个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;
	}





原文地址:https://www.cnblogs.com/brucemengbm/p/7357852.html