从新 Google 照片应用中选择照片的方向不正确
Selecting photo from new Google Photos app has incorrect orientation
我正在尝试使用 Android 上的意图选择一张照片。一切正常,照片正在从任何照片应用程序(相机、图库、屏幕截图等。即使是从新的照片应用程序中选择)中检索;除了在 Google 张照片上在线备份的那些。
获取位图时,纵向拍摄的照片将以横向方式检索。我有获取照片方向的代码,因此我可以相应地进行翻译,但在新的 Google 照片应用程序上选择在线照片时,方向始终为 0。当返回 0 时,关于如何获取这些照片的方向有什么想法吗?下面的代码 - 谢谢。
开始意图
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, IMPORT_PHOTO_RESULT);
意向结果
Uri selectedImageUri = data.getData();
imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
//fix rotation
int orientation = -1;
Cursor cursor = MediaStore.Images.Media.query(getContentResolver(), selectedImageUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION });
try {
if (cursor.moveToFirst()) {
orientation = cursor.getInt(0);
}
} finally {
cursor.close();
}
imageBitmap = Utils.rotateImageWithRotation(orientation, imageBitmap);
//get stream to display and send
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
// get byte array here
byte[] bytearray = stream.toByteArray();
sendImage(bytearray, null);
旋转图片
public static Bitmap rotateImageWithRotation(int rotationInDegrees, Bitmap mBitmap) {
Matrix matrix = new Matrix();
if (rotationInDegrees != 0f) {
matrix.preRotate(rotationInDegrees);
}
Bitmap adjustedBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
return adjustedBitmap;
}
你可以试试这个:
Bitmap bitmap = null;
try {
Bitmap tmpBmp;
Uri uri = params[0]; // remove uri with uri from intent
int angle = 0;
Matrix m = new Matrix();
BitmapFactory.Options tempOption = new BitmapFactory.Options();
tempOption.inSampleSize = 2;
AssetFileDescriptor fileDescriptor = getContentResolver().openAssetFileDescriptor(uri, "r");
tmpBmp = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, tempOption);
ExifInterface ei = new ExifInterface(uri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
angle = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
angle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
angle = 270;
break;
}
m.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
if (Runtime.getRuntime().totalMemory() > 64 * 1024 * 1024) {
options.inPreferredConfig = Bitmap.Config.RGB_565;
}
bitmap = Bitmap.createBitmap(tmpBmp, 0, 0, tempOption.outWidth, tempOption.outHeight, m, true);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
return bitmap;
我放弃了,转而使用这个神奇的库。感谢您的工作!
我正在尝试使用 Android 上的意图选择一张照片。一切正常,照片正在从任何照片应用程序(相机、图库、屏幕截图等。即使是从新的照片应用程序中选择)中检索;除了在 Google 张照片上在线备份的那些。
获取位图时,纵向拍摄的照片将以横向方式检索。我有获取照片方向的代码,因此我可以相应地进行翻译,但在新的 Google 照片应用程序上选择在线照片时,方向始终为 0。当返回 0 时,关于如何获取这些照片的方向有什么想法吗?下面的代码 - 谢谢。
开始意图
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, IMPORT_PHOTO_RESULT);
意向结果
Uri selectedImageUri = data.getData();
imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
//fix rotation
int orientation = -1;
Cursor cursor = MediaStore.Images.Media.query(getContentResolver(), selectedImageUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION });
try {
if (cursor.moveToFirst()) {
orientation = cursor.getInt(0);
}
} finally {
cursor.close();
}
imageBitmap = Utils.rotateImageWithRotation(orientation, imageBitmap);
//get stream to display and send
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
// get byte array here
byte[] bytearray = stream.toByteArray();
sendImage(bytearray, null);
旋转图片
public static Bitmap rotateImageWithRotation(int rotationInDegrees, Bitmap mBitmap) {
Matrix matrix = new Matrix();
if (rotationInDegrees != 0f) {
matrix.preRotate(rotationInDegrees);
}
Bitmap adjustedBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
return adjustedBitmap;
}
你可以试试这个:
Bitmap bitmap = null;
try {
Bitmap tmpBmp;
Uri uri = params[0]; // remove uri with uri from intent
int angle = 0;
Matrix m = new Matrix();
BitmapFactory.Options tempOption = new BitmapFactory.Options();
tempOption.inSampleSize = 2;
AssetFileDescriptor fileDescriptor = getContentResolver().openAssetFileDescriptor(uri, "r");
tmpBmp = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, tempOption);
ExifInterface ei = new ExifInterface(uri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
angle = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
angle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
angle = 270;
break;
}
m.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
if (Runtime.getRuntime().totalMemory() > 64 * 1024 * 1024) {
options.inPreferredConfig = Bitmap.Config.RGB_565;
}
bitmap = Bitmap.createBitmap(tmpBmp, 0, 0, tempOption.outWidth, tempOption.outHeight, m, true);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
return bitmap;
我放弃了,转而使用这个神奇的库。感谢您的工作!