[英]Android - ImageView Orientation changes depending on device
我制作了一個自定義相機應用程序,並且想要在ImageView上顯示圖片。 我在3種不同的手機(HTC Desire Z,Galaxy S4和Galaxy S2)上啟動了我的程序。 在HTC上,ImageView中會考慮圖片的方向,但Galaxy不會。
我的程序是這樣的:
->我用自定義相機應用程序拍照
->根據電話方向(OrientationEventListener)設置圖片的EXIF標志:
switch(getOrientation(rot)){
case VERTICAL_ENDROIT : params.setRotation(90); break;
case HORIZONTAL_ENDROIT : params.setRotation(0); break;
case VERTICAL_ENVERS : params.setRotation(270); break;
case HORIZONTAL_ENVERS : params.setRotation(180); break;
}
camera.setParameters(params);
此功能在所有電話上都運行良好,它們都已檢測到並分配了正確的方向。
->我將其保存為JPG格式
->我將圖片發送到ImageView
Bitmap bm2 = BitmapFactory.decodeFile(photoItem.getPathPhoto(),options);
imageView.setImageBitmap(bm2);
對於Galaxy,ImageView上的Orientation始終相同,對應於params.setRotation(0)。 HTC沒問題。
所以我試圖在將圖片發送到ImageView之前查看圖片的exif標志:
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
奇怪的是,對於HTC, 方向始終為0,而對於Samsung 方向則獲得正確的值(1 3 6 8)。
結論:對於Galaxy Phone,EXIF方向值可以,但是ImageView上的方向不好。
使用HTC Phones,EXIF方向值是false,但是方向是可以的。 (wtf?)
感謝您的回答:)
PS:我不想使用矩陣或圖像處理的東西,因為我的CPU /內存資源非常低
try {
final File f = new File(directory, name);
ExifInterface ei = new ExifInterface(f.getAbsolutePath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
try {
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
b = RotateBitmap(b, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
b = RotateBitmap(b, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
b = RotateBitmap(b, 270);
break;
// etc.
}
} catch (OutOfMemoryError err) {
} catch (Exception e) {
}
return b;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
public static Bitmap RotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
source = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
source.getHeight(), matrix, true);
if (source != null) {
b = null;
}
return source;
}
此代碼將執行您所需的功能。
試試下面的代碼
try {
File f = new File(imagePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
null, options);
bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mat, true);
ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100,
outstudentstreamOutputStream);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
Log.w("TAG", "-- Error in setting image");
} catch (OutOfMemoryError oom) {
Log.w("TAG", "-- OOM Error in setting image");
}
好吧,我發現為什么方向與Galaxy和HTC不一樣。 HTC不使用EXIF標志來定位圖片。 圖片以正確的方向本機保存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.