7

Android 2.2 を搭載した HTC Desire でアプリをテストしています。そして、それは私が望んでいるように正確に機能します。Sherlock パッケージを使用して、古いデバイスでも新しいデバイスでも同じスタイルを実現しています。

AVD は最新の Android を使用するように設定されており、見た目も問題ありません。次に、Samsung Galaxy S2 に配置しました。カメラとギャラリーの画像を操作すると、回転が正しくありません。サムスンの何か(カメラアプリ、アンドロイド自体)がEXIFをチェックしないか、チェックし、私の画像の向きが間違っているようです。縦向きの画像は横向きでロードされ、横向きの画像は縦向きでロードされます。

  1. 画像をそのまま読み込むには、何らかの形で EXIF をチェックして無視する必要があると思いますか?
  2. より大きな問題は、同様の問題を起こす他のデバイス (一部の HTC、一部の HUAWEI など) があるかどうかを知る方法ですか? 4つの画面サイズグループがあることを除いて、すべてのAndroidデバイスが同じように動作すると思いました...

Tnx。

4

1 に答える 1

4

コードがないと、何が起こっているのかわかりません。

私が見つけた最も簡単な方法は、EXIF 情報を読み取り、画像の回転が必要かどうかを確認することです。Android の ExifInterface クラスの詳細については、http://developer.android.com/intl/es/reference/android/media/ExifInterface.html を参照してください

とはいえ、ここにいくつかのサンプルコードがあります:

/** An URI and a imageView */
public void setBitmap(ImageView mImageView, String imageURI){
    // Get the original bitmap dimensions
    BitmapFactory.Options options = new BitmapFactory.Options();            
    Bitmap bitmap = BitmapFactory.decodeFile(imageURI, options);
    float rotation = rotationForImage(getActivity(), Uri.fromFile(new File(imageURI)));

    if(rotation!=0){
        //New rotation matrix
        Matrix matrix = new Matrix();
        matrix.preRotate(rotation);
        mImageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, reqHeight, reqWidth, matrix, true));
    } else {
        //No need to rotate
        mImageView.setImageBitmap(BitmapFactory.decodeFile(imageURI, options));
    }
}


/** Returns how much we have to rotate */
public static float rotationForImage(Context context, Uri uri) {
        try{
            if (uri.getScheme().equals("content")) {
                //From the media gallery
                String[] projection = { Images.ImageColumns.ORIENTATION };
                Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
                    if (c.moveToFirst()) {
                        return c.getInt(0);
                    }               
            } else if (uri.getScheme().equals("file")) {
                 //From a file saved by the camera
                    ExifInterface exif = new ExifInterface(uri.getPath());
                    int rotation = (int) exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
                    return rotation;
            }
            return 0;

        } catch (IOException e) {
            Log.e(TAG, "Error checking exif", e);
            return 0;
        }
}

/** Get rotation in degrees */
private static float exifOrientationToDegrees(int exifOrientation) {
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            return 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            return 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            return 270;
        }
        return 0;
}

エラーが発生した場合は、rotationForImage 関数で「EXIF のチェック中にエラーが発生しました」というログが表示されます。

于 2012-10-08T23:13:01.680 に答える