Android:使用ExifInterface处理照片角度

问题描述:部分手机(例如韩国的某星手机)拍出来的照片,在浏览器显示的时候会翻转九十度,一个站着的人变成了躺着。但是在手机上还没见过因为这个角度而显示异常的情况,也就是说,一个照片的角度如果是90,那么不一定在手机上会横着显示。

ExifInterface接口可以查看照片的属性,利用这个接口可以将照片的角度反转到正常角度。

代码:

/**	 
	 * @param photoPath
	 *            照片的路径
	 * @throws IOException
	 *             new ExifInterface(photoPath)抛出的异常
	 */
	public Bitmap fixPhoto(String photoPath) throws IOException {

		Bitmap bitmap = null;
		ExifInterface ei = new ExifInterface(photoPath);
		int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
				ExifInterface.ORIENTATION_NORMAL);
		Matrix matrix = new Matrix();
		switch (orientation) {
		case ExifInterface.ORIENTATION_ROTATE_90:
			matrix.postRotate(90);
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
					bitmap.getHeight(), matrix, true);
			break;
		case ExifInterface.ORIENTATION_ROTATE_180:
			matrix.postRotate(180);
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
					bitmap.getHeight(), matrix, true);
			break;
		case ExifInterface.ORIENTATION_ROTATE_270:
			matrix.postRotate(270);
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
					bitmap.getHeight(), matrix, true);
			break;
		default:
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
					bitmap.getHeight(), matrix, true);
			break;
		}

		return bitmap;

	}

这样处理后的图片就可以正常角度显示了。


补充:图片稍微压缩以下再保存成文件。另外,图片属性的修改和要锁保存等等都要写在子线程中,因此,相对完整的代码如下

	public class FixPhotoTask extends AsyncTask<String, Void, String> {
		private Bitmap fixedPhoto;

		@Override
		protected String doInBackground(String... params) {
			try {
				fixedPhoto = fixPhoto(params[0], params[1]);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			System.out.println("图片处理成功,保存位置为:" + result);
		}

	}

	/**
	 * 
	 * @param photoPath
	 *            照片的路径
	 * @throws IOException
	 *             new ExifInterface(photoPath)抛出的异常
	 */
	public Bitmap fixPhoto(String photoPath, String outputPath)
			throws IOException {

		Bitmap bitmap = null;
		ExifInterface ei = new ExifInterface(photoPath);
		int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
				ExifInterface.ORIENTATION_NORMAL);
		Matrix matrix = new Matrix();
		switch (orientation) {
		case ExifInterface.ORIENTATION_ROTATE_90:
			matrix.postRotate(90);
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
					bitmap.getHeight(), matrix, true);
			break;
		case ExifInterface.ORIENTATION_ROTATE_180:
			matrix.postRotate(180);
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
					bitmap.getHeight(), matrix, true);
			break;
		case ExifInterface.ORIENTATION_ROTATE_270:
			matrix.postRotate(270);
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
					bitmap.getHeight(), matrix, true);
			break;
		default:
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
					bitmap.getHeight(), matrix, true);
			break;
		}
		/*
		 * 图片压缩和保存的逻辑
		 */
		File outputFile = new File(outputPath);
		FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
		return bitmap;
	}



发布了66 篇原创文章 · 获赞 16 · 访问量 14万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章