Zac*_*uel 3 android android-gallery android-camera
我正在使用图库选取器从图库中选取图像.照相机以纵向模式拍摄的照片在照片库中显示为笔直.但是当我导入照片时,我将照片旋转(横向).只有画廊显示这张照片是直的.如何管理这个问题?我希望所有照片都是它的实际方向.提前致谢
private void addImageFromGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
GALLERY_CODE);
}
Run Code Online (Sandbox Code Playgroud)
Zac*_*uel 11
得到了答案.方向以EXIF格式保存.我们必须读取每个图像的数据的Orientation标签.
public static float rotationForImage(Context context, Uri uri) {
if (uri.getScheme().equals("content")) {
String[] projection = { Images.ImageColumns.ORIENTATION };
Cursor c = context.getContentResolver().query(
uri, projection, null, null, null);
if (c.moveToFirst()) {
return c.getInt(0);
}
} else if (uri.getScheme().equals("file")) {
try {
ExifInterface exif = new ExifInterface(uri.getPath());
int rotation = (int)exifOrientationToDegrees(
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL));
return rotation;
} catch (IOException e) {
Log.e(TAG, "Error checking exif", e);
}
}
return 0f;
}
private static float exifOrientationToDegrees(int 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;
}
}
Run Code Online (Sandbox Code Playgroud)
旋转值可用于校正照片的方向,如下所示:
Matrix matrix = new Matrix();
float rotation = PhotoTaker.rotationForImage(context, uri);
if (rotation != 0f) {
matrix.preRotate(rotation);
}
Bitmap resizedBitmap = Bitmap.createBitmap(
sourceBitmap, 0, 0, width, height, matrix, true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1689 次 |
| 最近记录: |