拍照旋转图片


public class ImageUtil {

	public static final String TAG = ImageUtil.class.getSimpleName();

	/**
	 * 判断图片是否需要翻转(正方向的不用翻转)
	 * 
	 * @param fileName
	 * @return
	 */
	public static boolean needRotate(Context context, String fileName) {
		try {
			ExifInterface exif = new ExifInterface(fileName);
			int orientation = exif.getAttributeInt(
					ExifInterface.TAG_ORIENTATION,
					ExifInterface.ORIENTATION_UNDEFINED);
			LogUtil.d(TAG, "orientation is " + orientation);
			switch (orientation) {
			case ExifInterface.ORIENTATION_ROTATE_90:
				return true;
			case ExifInterface.ORIENTATION_ROTATE_270:
				return true;
			case ExifInterface.ORIENTATION_ROTATE_180:
				return true;
			}

			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 获取翻转到正方向的的图片
	 * 
	 * @param fileName
	 * @param maxWidth
	 * @param maxHeight
	 * @return
	 */
	public static void rotatedBitmap(Context context, String fileName,
			String dstFile) {
		try {
			ExifInterface exif = new ExifInterface(fileName);
			int orientation = exif.getAttributeInt(
					ExifInterface.TAG_ORIENTATION,
					ExifInterface.ORIENTATION_UNDEFINED);
			LogUtil.d(TAG, "orientation is " + orientation);
			Matrix matrix = null;

			switch (orientation) {
			case ExifInterface.ORIENTATION_ROTATE_90:
				matrix = new Matrix();
				matrix.postRotate(90);
				break;
			case ExifInterface.ORIENTATION_ROTATE_270:
				matrix = new Matrix();
				matrix.postRotate(270);
				break;
			case ExifInterface.ORIENTATION_ROTATE_180:
				matrix = new Matrix();
				matrix.postRotate(180);
				break;
			}

			Bitmap bmp = getSmallBitmap(context,
					Uri.fromFile(new File(fileName)));
			if (matrix != null) {
				Bitmap bmp2 = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
						bmp.getHeight(), matrix, true);
				bmp.recycle();

				FileOutputStream fos = new FileOutputStream(dstFile);
				bmp2.compress(Bitmap.CompressFormat.JPEG, 100, fos);
				fos.close();
				bmp2.recycle();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static boolean compress(Context context, Uri src, File dst, int rate) {
		try {
			Bitmap bmp = getSmallBitmap(context, src);
			FileOutputStream fos = new FileOutputStream(dst);
			bmp.compress(Bitmap.CompressFormat.JPEG, rate, fos);
			bmp.recycle();
			bmp = null;
			fos.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {

		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			final int heightRatio = Math.round((float) height
					/ (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);
			inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}
		return inSampleSize;

	}

	public static Bitmap getSmallBitmap(Context context, Uri uri) {
		try {
			final BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;
			InputStream is = context.getContentResolver().openInputStream(uri);
			BitmapFactory.decodeStream(is, null, options);
			is.close();

			// Calculate inSampleSize
			options.inSampleSize = calculateInSampleSize(options, 720, 1280);

			// Decode bitmap with inSampleSize set
			options.inJustDecodeBounds = false;

			is = context.getContentResolver().openInputStream(uri);
			Bitmap bmp = BitmapFactory.decodeStream(is, null, options);
			is.close();
			return bmp;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

猜你喜欢

转载自bwlcool.iteye.com/blog/1904273