在 Android 中定位从相机拍摄的图像

Orientate Image Taken from camera in Android

我正在创建 android 相机应用。这是使用 camera API.
的自定义相机 每当我用相机拍照时,它总是以 landscap 模式保存。
我正在使用这种方法来设置相机方向。

 public static void setCameraDisplayOrientation(Activity activity,
                                               int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    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);
}

这是负责存储图像的代码。

 PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
            Log.d(TAG, "Error creating media file, check storage permissions: ");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
     } 
    };

此代码成功保存图像但始终处于横向模式。我缺少什么 我是 android 的新人。我想保存预览中显示的图像。

更新 2(根据回答)

       PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {

        Camera.CameraInfo info = new Camera.CameraInfo();

        int rotationAngle = getCorrectCameraOrientation (MainActivity.this, info);
   Toast.makeText(MainActivity.this, "Angle "+ rotationAngle, Toast.LENGTH_SHORT).show();
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

        try {
            writeFile (data, pictureFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        processImage (pictureFile, rotationAngle, 1);
    }
   };

我正在以这种方式使用代码,但这仍然不起作用同样的问题图像始终处于横向模式。

尝试

1.首先,添加下面定义的方法(writeFile、processImage和getCorrectCameraOrientation)(步骤3之后)

2. 拍摄照片后计算相机方向并更新 onPictureTaken**

@Override
public void onPictureTaken (final byte[] data, final Camera camera) {

    int rotationAngle = getCorrectCameraOrientation (this, info);

3. 创建文件并使用 rotationAngle 更新照片角度

File file = new File (folder, fileName);

try {
    file.createNewFile ();
}
catch (IOException e) {
    e.printStackTrace ();
}

writeFile (data, file);
processImage (file, rotationAngle, compressRatio);

writeFile

public static void writeFile (byte[] data, File file) throws IOException {

    BufferedOutputStream bos = null;

    try {
        FileOutputStream fos = new FileOutputStream (file);
        bos = new BufferedOutputStream (fos);
        bos.write (data);
    }
    finally {
        if (bos != null) {
            try {
                bos.flush ();
                bos.close ();
            }
            catch (Exception e) {
            }
        }
    }
}

processImage

public static void processImage (File file, int rotationAngle, int compressionRatio) {

    BufferedOutputStream bos = null;

    try {

        Bitmap bmp = BitmapFactory.decodeFile (file.getPath ());

        Matrix matrix = new Matrix ();
        matrix.postRotate (rotationAngle);

        bmp = Bitmap.createBitmap (bmp, 0, 0, bmp.getWidth (), bmp.getHeight (), matrix, true);

        FileOutputStream fos = new FileOutputStream (file);
        bmp.compress (Bitmap.CompressFormat.PNG, compressionRatio, fos);
    }
    catch (IOException e) {
        e.printStackTrace ();
    }
    catch (OutOfMemoryError t) {
        t.printStackTrace ();
    }
    catch (Throwable t) {
        t.printStackTrace ();
    }
    finally {
        if (bos != null) {
            try {
                bos.flush ();
                bos.close ();
            }
            catch (Exception e) {
            }
        }
    }
}

getCorrectCameraOrientation

    public static int getCorrectCameraOrientation (Activity activity, Camera.CameraInfo info) {

    int rotation = activity.getWindowManager ().getDefaultDisplay ().getRotation ();
    int degrees = 0;

    if (hasValidRotation (rotation)) {
        degrees = rotation * 90;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;
    }
    else {
        result = (info.orientation - degrees + 360) % 360;
    }

    return result;
}