捕获的照片方向在 android 中发生变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11026615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Captured Photo orientation is changing in android
提问by Santhosh
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?
我点击一个按钮打开相机应用程序。并在下一个活动中显示捕获的照片。但是拍摄的照片正在旋转 90 度。当我在捕获图像后在视图中显示图像时,它的方向始终是横向。为什么在人像模式下拍摄照片时,照片不会以人像方式显示?
onClick of a button :
单击按钮:
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);
Inside onActvityresult:
在活动结果中:
bmp = BitmapFactory.decodeFile(APP_DIR+"/latest.png");
startActivity(new Intent(this, DisplayActivity.class));
Displaying captured photo:
显示拍摄的照片:
photoViewRelativeLayout.setBackgroundDrawable(new BitmapDrawable(getResources(), CaptureActivity.bmp));
回答by Shabbir Panjesha
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.
我在三星手机上遇到了同样的问题。显然三星手机设置了 EXIF 方向标签,而不是旋转单个像素。使用 BitmapFactory 读取位图不支持这个标签。我发现这个问题的解决方案是在 onActivityResult 中使用 ExifInterface活动的方法。它检查与从相机捕获的图像的 URI 相关联的方向。
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);
回答by Syeda
Please use Glide instead. It fixes the orientation itself and is irrespective of the mobile you're using. Here\s a sample
请改用 Glide。它自行固定方向,与您使用的手机无关。这里\一个示例
Glide.with(_c).load(horizontalList.get(position).imagePath).into(holder.img_injury);
回答by Bob Richardson
I got it somewhat working. When I take the photo in Landscape mode everything works fine as before. If I take the photo in portrait mode, I need to turn the camera upside down and the pic looks good. If you notice I changed your code a bit by adding the "ExifInterface" in front of each orientation. I used the following code:
我得到它有点工作。当我以横向模式拍摄照片时,一切都像以前一样正常。如果我以人像模式拍摄照片,我需要将相机倒置,照片看起来不错。如果您注意到我通过在每个方向前面添加“ExifInterface”来稍微更改您的代码。我使用了以下代码:
try {
ExifInterface exif = new ExifInterface(filename);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate-=90;break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate-=90;break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate-=90;break;
}
Log.d("Fragment", "EXIF info for file " + filename + ": " + rotate);
} catch (IOException e) {
Log.d("Fragment", "Could not get EXIF info for file " + filename + ": " + e);
}
回答by Dheeresh Singh
you can just simpaly rotate it
你可以简单地旋转它
get the orietation of the image taken using this code in onActvityresult ....
在 onActvityresult 中获取使用此代码拍摄的图像的方向 ....
File imageFile = new File(imageUri.toString());
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch(orientation) {
case ORIENTATION_ROTATE_270:
rotate-=90;break;
case ORIENTATION_ROTATE_180:
rotate-=90;break
case ORIENTATION_ROTATE_90:
rotate-=90;break
}
回答by jai khambhayta
call this method with path of bitmap and bitmap
使用位图和位图的路径调用此方法
getRotateImage(path, bm)
Add getRotateImage method as static in any util class and use it.
在任何 util 类中将 getRotateImage 方法添加为静态方法并使用它。
public static Bitmap getRotateImage(String photoPath, Bitmap bitmap) throws IOException {
ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap rotatedBitmap = null;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(bitmap, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = bitmap;
}
return rotatedBitmap;
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}