捕获的照片方向在 android 中发生了变化 [英] Captured Photo orientation is changing in android
问题描述
我正在通过单击按钮打开相机应用程序.并在下一个活动中显示拍摄的照片.但拍摄的照片旋转了 90 度.当我在捕获图像后在视图中显示图像时,它的方向始终是横向的.为什么在人像模式下拍摄的照片没有以人像模式显示?
I'am opening camera app on click of a button. And displaying the captured photo in next activity. But the captured photo is rotating by 90 degrees. When I display the image in a view after I capture it, it's orientation is always landscape. Why is the photo not being shown in portrait as is when the photo is taken in portrait mode?
点击按钮:
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(APP_DIR+"/latest.png")));
startActivityForResult(i, CAPTURE_PHOTO_CONSTANT);
onActvityresult 内部:
Inside onActvityresult:
bmp = BitmapFactory.decodeFile(APP_DIR+"/latest.png");
startActivity(new Intent(this, DisplayActivity.class));
显示拍摄的照片:
photoViewRelativeLayout.setBackgroundDrawable(new BitmapDrawable(getResources(), CaptureActivity.bmp));
推荐答案
我在三星手机上遇到过同样的问题.显然三星手机设置了 EXIF 方向标签,而不是旋转单个像素.使用 BitmapFactory 读取位图不会支持这个标签.我发现这个问题的解决方案是在活动的 onActivityResult 方法中使用 ExifInterface.它检查与从相机捕获的图像的 URI 关联的方向.
I had the same problem mostly with the Samsung handsets.Apparently Samsung phones set the EXIF orientation tag, rather than rotating individual pixels.Reading the Bitmap using BitmapFactory does not support this tag.What i found the solution to this problem was using ExifInterface in onActivityResult method of the activity.Which checks for orientation associated with URI of the captured image from the camera.
int rotate = 0;
try {
getContentResolver().notifyChange(imageUri, null);
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(Common.TAG, "Exif orientation: " + orientation);
} catch (Exception e) {
e.printStackTrace();
}
/****** Image rotation ****/
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true);
这篇关于捕获的照片方向在 android 中发生了变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!