我正在创建一个应用程序,打开图片库,照片将显示在另一个活动中,从图片库中选择该照片。我的问题是,我在肖像模式拍摄的照片将在显示后旋转。但我在景观模式下拍摄的照片将被正确显示。
这就是为什么,我必须检查一个图像是在肖像模式还是在景观模式下使用摄像头在android,这样我就可以旋转的肖像拍摄的照片。有人能帮我怎么做吗?
N.B.:照片的宽度和高度在拍摄的肖像和风景中都是一样的。。
发布于 2013-12-22 11:45:09
您可以使用矩阵检查图像的旋转,并相应地旋转。
此代码在onActivityResult->
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = false;
bmOptions.inPurgeable = true;
Bitmap cameraBitmap = BitmapFactory.decodeFile(filePath);//get file path from intent when you take iamge.
ByteArrayOutputStream bos = new ByteArrayOutputStream();
cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
ExifInterface exif = new ExifInterface(filePath);
float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
System.out.println(rotation);
float rotationInDegrees = exifToDegrees(rotation);
System.out.println(rotationInDegrees);
Matrix matrix = new Matrix();
matrix.postRotate(rotationInDegrees);
Bitmap scaledBitmap = Bitmap.createBitmap(cameraBitmap);
Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
FileOutputStream fos=new FileOutputStream(filePath);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();OnActivityResult代码在这里结束。
下面的函数用于获得旋转:-
private static float exifToDegrees(float exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}https://stackoverflow.com/questions/20728684
复制相似问题