[英]Photos loaded onto canvas taken from a camera in portrait mode are landscape
[英]How to find out if the image from the gallery was taken in portrait or landscape from camera
portrait
還是landscape
我有從下面的代碼獲得的路徑
/storage/emulated/0/DCIM/Camera/IMG_20181110_211757.jpg
然后我正在使用ExifInterface查找方向:
public static void getCameraPhotoOrientation(Activity activity, String imagePath) {
String path =getPath(activity,imagePath);
try {
ExifInterface ei = new ExifInterface(path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
Timber.d("Type-1");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Timber.d("Type-2");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Timber.d("Type-3");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
Timber.d("Type-4");
break;
}
}catch (IOException ex){
ex.printStackTrace();
}
}
我總是以ExifInterface.ORIENTATION_UNDEFINED
作為ExifInterface.ORIENTATION_UNDEFINED
如何解決這個問題
我的圖庫中有一個從相機拍攝的圖像
開發人員無法保證任何特定圖像都來自相機應用程序,除非開發人員是該相機應用程序的作者。
我試圖找出圖像是縱向還是橫向
不需要圖像是縱向或橫向。 例如,相機應用可以將其照片裁剪為圓形或正方形。
我有從下面的代碼獲得的路徑
由於許多設備上的許多Uri
值,該代碼將失敗。 如果要將ExifInterface
用作Uri
,請使用getContentResolver().openInputStream()
獲取有關Uri
標識的內容的InputStream
,然后將該InputStream
傳遞給此ExifInterface
構造函數 。
我總是以ExifInterface.ORIENTATION_UNDEFINED作為方向
圖像不必具有EXIF標頭,更不用說ExifInterface.TAG_ORIENTATION
。
如果該標簽不存在,則可以檢查圖像的寬度和高度並進行猜測:
對於正方形圖像,這將失敗,而對於裁剪后的圖像,這將失敗(例如,照片是人像拍攝,但用戶將其裁剪為現在的寬度大於高度)。
最好的解決方案是更改應用程序,而不管圖像是縱向還是橫向。 下一個最佳解決方案是詢問用戶他們要如何處理圖像,也許您可以使用上述算法在此處設置默認選項。
通過更改如下所示的路徑訪問過程來嘗試
File imFile = new File(imagePath);
ExifInterface ei = new ExifInterface(imFile.getAbsolutePath());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.