我正在拍摄照片并将其存储到SD Card
,然后将其从SD Card
查看到ImageView
,但是会轮流播放......
我正在Portrait mode
中捕获它,但是在Landscape mode
...
我有什么遗漏的东西吗?
在这里找到/**
* Displaying captured image/video on the screen
* */
private void previewMedia(boolean isImage) {
// Checking whether captured media is image or video
if (isImage) {
imgPreview.setVisibility(View.VISIBLE);
final Bitmap bitmap = BitmapFactory.decodeFile(filePath);
Bitmap orientedBitmap = ExifUtil.rotateBitmap(filePath, bitmap);
imgPreview.setImageBitmap(orientedBitmap);
} else {
imgPreview.setVisibility(View.GONE);
}
}
但仍然在ImageView中显示旋转的图像...
答案 0 :(得分:4)
如果图像(照片)是由您制作的程序拍摄的,则必须使用正确的旋转值设置Parameters.setRotation。
这取决于相机驱动,在保存之前旋转图像或将旋转值保存到exif TAG_ORIENTATION。
因此,如果TAG_ORIENTATION为空或零,则图像的方向正确,否则您必须根据TAG_ORIENTATION中的值旋转图像。
CODE
从EXIF获取方向:
ExifInterface exif = null;
try {
exif = new ExifInterface(path);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
旋转位图:
Bitmap bmRotated = rotateBitmap(bitmap, orientation);
旋转位图的方法:
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
}
catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
答案 1 :(得分:1)
您需要将EXIF
与 ORIENTATION_UNDEFINED 一起使用才能获得正确的方向。
ExifInterface exif = null;
try {
exif = new ExifInterface(path);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
旋转位图:
Bitmap bmRotated = rotateBitmap(bitmap, orientation);