默認情況下,不同的設備在其圖片上使用不同的方向。這可能很煩人。你需要做的是找到這個方向,並相應地翻轉圖像。
下面是一些代碼,我在我的應用程序中使用: 注:我敢肯定,這是不完美的。如果任何人有關於如何改善這一點的建議,請隨時評論/編輯!
private int getOrientation(String pathToYourImage) throws IOException{
ExifInterface exif = new ExifInterface(path);
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
if (rotation == ExifInterface.ORIENTATION_ROTATE_90){
return 90;
} else if (rotation == ExifInterface.ORIENTATION_ROTATE_180){
return 180;
} else if (rotation == ExifInterface.ORIENTATION_ROTATE_270){
return 270;
}
return 0;
}
事遂所願
if (orientation > 0){
originalImage = rotateBitmap(originalImage, orientation);
}
private Bitmap rotateBitmap(Bitmap srcBitmap, int angle){
Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotatedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true);
srcBitmap.recycle();
return rotatedBitmap;
}
這可能無法完全滿足您的需求,但它運作良好,對我來說足夠。詢問你是否還有其他問題!
我想用戶正在拍照並且您沒有檢測到設備方向。我不編碼android,但你必須做的是:檢測設備的方向。解析收到的數據並旋轉與方向對應的圖像 – wonderwhy 2014-09-05 16:16:58