17

フロントエンドカメラとバックエンドカメラの両方から写真を撮るポートレートモードのカメラアプリがあります.問題は、キャプチャされた画像が間違った方法で回転しているようなものです...

プレビューのために、次のコードを使用しました....

    Camera.Parameters parameters = camera.getParameters();
        android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(defaultCameraId, info);
        int rotation = this.getWindowManager().getDefaultDisplay()
                .getRotation();
        if (Integer.parseInt(Build.VERSION.SDK) >= 8) {

            int degrees = 0;
            switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
            }
            int result;
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                result = (info.orientation + degrees) % 360;
                result = (360 - result) % 360; // compensate the mirror
            } else { // back-facing
                result = (info.orientation - degrees + 360) % 360;
            }

            camera.setDisplayOrientation(result);

        } else {
            parameters.set("orientation", "portrait");
        }

        camera.setParameters(parameters);

しかし、キャプチャされた画像は間違った方法で回転されます.私も使用してキャプチャされた画像を回転させようとしました.ネクサスmatrix.postRotate(bitmap)などの一部のデバイスでは機能しません..EXIFも試しました.しかし、ここではファイルパスの代わりにURLを持っています.That同様に機能しません。誰でも私を助けることができますか?

4

5 に答える 5

7

以下のコードを参照できます。

ExifInterface exif = new ExifInterface(SourceFileName);     //Since API Level 5

String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);

また、このリンクを参照してくださいhttps://stackoverflow.com/a/6124375/1441666

于 2012-10-25T12:29:51.773 に答える
4

これを使用して、Uri

                    String filePath = mImageUri.getPath();
                if (filePath != null) {
                    rotation = -1;
                        ExifInterface exif = new ExifInterface(filePath); // Since
                                                                            // API
                                                                            // Level
                                                                            // 5
                        rotation = Integer.parseInt(exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                        // //Log.v("roation",
                        // exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                    }

                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
            }

そしてノートの回転として「3=180、6=90、8=270」

于 2012-11-04T16:34:12.450 に答える
3

このコードスニペットを試してください

try {

    ExifInterface exif = new ExifInterface(filePath);
    orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    //Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
    Log.v("log", "ort is "+orientation);

    } catch (IOException e)
    {
        e.printStackTrace();
    }

次に、取得した方向に従ってマトリックスを回転させます

if (orientation==6)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
else if (orientation==8)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(270);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}

else if (orientation==3)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(180);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true;
}
于 2012-11-05T07:39:03.890 に答える
3

これには次のコードを使用しています。

ExifInterface exif = new ExifInterface(getUriPath(uri));
int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

getUriPath:

public String getUriPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null,
            null);
    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(projection[0]);
        cursor.moveToFirst();
        String filePath = cursor.getString(column_index);
        cursor.close();
        return filePath;
    } else
        return null;
}
于 2012-11-03T17:49:48.967 に答える