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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章