簡體   English   中英

為什么三星android設備中的旋轉不起作用?

[英]Why the rotation in samsung android device is not working?

我試圖制作一個允許用戶從畫廊中選擇圖像/拍照並將結果設置為位圖的應用。 當我測試該應用程序時,我發現三星設備的旋轉存在問題。

經過一會兒的搜索,我發現旋轉不是由google定義的,但是制造商本身和三星似乎有一些不同的設置。 另外,有人建議使用另一種方法來檢查旋轉。

如何解決問題? (注意:不僅是拍攝的圖片,而且圖庫中的圖片都有相同的旋轉問題)

這是從提供的文件路徑中獲取位圖的代碼:

  private Bitmap getBitmap(String path) {

        Uri uri = getImageUri(path);
        InputStream in = null;
        try {
            in = mContentResolver.openInputStream(uri);

            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

            BitmapFactory.decodeStream(in, null, o);
            in.close();

            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            in = mContentResolver.openInputStream(uri);
            Bitmap b = BitmapFactory.decodeStream(in, null, o2);
            in.close();

            return b;
        } catch (FileNotFoundException e) {
            Log.e(TAG, "file " + path + " not found");
        } catch (IOException e) {
            Log.e(TAG, "file " + path + " not found");
        }
        return null;
    }

感謝您的幫助

我認為您正在尋找的是從圖像讀取exif旋轉並相應地旋轉它。 我知道三星設備存在問題,圖像無法正確顯示,但是您可以像這樣進行糾正:

首先,您必須從圖像中讀取Exif旋轉:

ExifInterface exif = new ExifInterface(pathToFile);  
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); 

有了這些信息,您就可以校正圖像的旋轉,不幸的是,這要復雜一些,它涉及使用矩陣旋轉位圖。 您可以這樣創建矩陣:

Matrix matrix = new Matrix();
switch (rotation) {
    case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
        matrix.setScale(-1, 1);
        break;

    case ExifInterface.ORIENTATION_ROTATE_180:
        matrix.setRotate(180);
        break;

    case ExifInterface.ORIENTATION_FLIP_VERTICAL:
        matrix.setRotate(180);
        matrix.postScale(-1, 1);
        break;

    case ExifInterface.ORIENTATION_TRANSPOSE:
        matrix.setRotate(90);
        matrix.postScale(-1, 1);
        break;

    case ExifInterface.ORIENTATION_ROTATE_90:
        matrix.setRotate(90);
        break;

    case ExifInterface.ORIENTATION_TRANSVERSE:
        matrix.setRotate(-90);
        matrix.postScale(-1, 1);
        break;

    case ExifInterface.ORIENTATION_ROTATE_270:
        matrix.setRotate(-90);
        break;

    case ExifInterface.ORIENTATION_NORMAL:        
    default:
        break;
}

最后,您可以創建正確旋轉的位圖:

int height = bitmap.getHeight();
int width = bitmap.getWidth();
Bitmap correctlyRotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

為了避免OutOfMemory異常,您應在創建正確旋轉的位圖后回收舊的未正確旋轉的位圖,如下所示:

bitmap.recycle();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM