[英]How to check whether an image is captured in portrait mode or landscape mode using camera in android?
I am creating an app which open the photo gallery and a photo will be displayed in another activity by selecting that photo from the gallery. 我正在创建一个打开照片库的应用,通过从图库中选择该照片,该照片将显示在另一个活动中。 My problem is that the photos which I captured in portrait mode will be rotated after display.
我的问题是,以人像模式拍摄的照片在显示后会被旋转。 But the photos which I captured in landscape mode will be displayed correctly.
但是我以横向模式拍摄的照片将正确显示。
That's why, I have to check whether an image is captured in portrait mode or landscape mode using camera in android so that I can rotate the portrait captured photos. 因此,我必须检查是否使用Android相机以纵向模式或横向模式捕获图像,以便旋转纵向捕获的照片。 Can anyone help me how to do that?
谁能帮我怎么做?
NB: The width and height are same both in portrait captured image and landscape captured image. 注意:肖像拍摄的图像和风景拍摄的图像的宽度和高度均相同。
You can always check the rotation of the image using Matrix and rotate it accordingly. 您始终可以使用Matrix检查图像的旋转并相应地旋转它。
This code goes in onActivityResult--> 这段代码放在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 Code Ends here. OnActivityResult代码在这里结束。
This function below is used to get rotation:- 下面的此函数用于旋转:-
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.