Android 旋轉本地圖片並保存覆蓋原圖片

Android手機千變萬化,適配真是個大問題。最近在做本地圖片保存並上傳服務器時,發現某些手機(三星)上傳到服務器圖片橫向顯示。特意上網去查了下,解決了問題。解決思路很簡單:獲取到圖片之後看圖片是否有旋轉,並得到旋轉的角度,再轉回來。下面看實現邏輯:


第一步:獲取指定路徑指定大小的圖片(500*500)這裏暫時先規定500*500


	/**
	 * 
	 * 獲取指定路徑指定大小的圖片(500*500)
	 * 
	 * @param filePath
	 * @return Bitmap
	 * @exception 異常描述
	 * @see 需要參見的其它內容
	 * @since 從類的哪一個版本,此方法被添加進來。(可選)
	 * @deprecated該方法從類的那一個版本後,已經被其它方法替換。(可選)
	 */
	public static Bitmap getCompressBm(String filePath) {
		Bitmap bm = null;
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filePath, options);
		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, 500, 500);
		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		bm = BitmapFactory.decodeFile(filePath, options);
		return bm;
	}


第二步:獲取圖片旋轉角度


	/**
	 * 
	 * 獲取照片旋轉角度
	 * 
	 * @param rotate
	 * @return int
	 * @exception 異常描述
	 * @see 需要參見的其它內容
	 * @since 從類的哪一個版本,此方法被添加進來。(可選)
	 * @deprecated該方法從類的那一個版本後,已經被其它方法替換。(可選)
	 */
	public static int getCameraPhotoOrientation(String imagePath) {
		int rotate = 0;
		try {
			File imageFile = new File(imagePath);
			ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
			int orientation = exif.getAttributeInt(
					ExifInterface.TAG_ORIENTATION,
					ExifInterface.ORIENTATION_NORMAL);

			switch (orientation) {
			case ExifInterface.ORIENTATION_ROTATE_270:
				rotate = 270;
				break;
			case ExifInterface.ORIENTATION_ROTATE_180:
				rotate = 180;
				break;
			case ExifInterface.ORIENTATION_ROTATE_90:
				rotate = 90;
				break;
			}

			// Log.v(TAG, "Exif orientation: " + orientation);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rotate;
	}


第三步:旋轉圖片

/**
	 * 
	 * 旋轉圖片
	 * 
	 * @param bitmap
	 *            rotate
	 * @return 返回類型
	 * @exception 異常描述
	 * @see 需要參見的其它內容
	 * @since 從類的哪一個版本,此方法被添加進來。(可選)
	 * @deprecated該方法從類的那一個版本後,已經被其它方法替換。(可選)
	 */
	public static Bitmap rotateBitmap(Bitmap bitmap, int rotate) {
		if (bitmap == null)
			return null;

		int w = bitmap.getWidth();
		int h = bitmap.getHeight();

		// Setting post rotate to 90
		Matrix mtx = new Matrix();
		mtx.postRotate(rotate);
		return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
	}

第四步:保存圖片到指定路徑並覆蓋原圖片

/**
	 * @Description 保存圖片到指定路徑
	 * @param bitmap
	 *            要保存的圖片
	 * @param filePath
	 *            目標路徑
	 * @return 是否成功
	 */
	@SuppressWarnings("finally")
	public static boolean saveBmpToPath(final Bitmap bitmap, final String filePath) {
		if (bitmap == null || filePath == null) {
			return false;
		}
		boolean result = false; // 默認結果
		File file = new File(filePath);
		OutputStream outputStream = null; // 文件輸出流
		try {
			outputStream = new FileOutputStream(file);
			result = bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
					outputStream); // 將圖片壓縮爲JPEG格式寫到文件輸出流,100是最大的質量程度
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close(); // 關閉輸出流
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return result;
		}
	}

第五步:獲取到修改過的圖片進行上傳,這裏就不寫上傳了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章