简体   繁体   English

Android相机在纵向模式下保存图片

[英]Android Camera Saving Pictures in Portrait Mode

I'm trying to use a camera in my app and I want to be able to use it in landscape and portrait mode. 我正在尝试在我的应用程序中使用相机,我希望能够在横向和纵向模式下使用它。 I'm having no difficulty creating pictures in landscape mode, but I haven't found a good way to save pictures in portrait mode. 我在横向模式下创建图片没有任何困难,但我还没有找到一种在纵向模式下保存图片的好方法。

When I want to make a picture in portrait mode I need to set the displayorientation to portrait first, like this: 当我想在纵向模式下制作图片时,我需要先将displayorientation设置为portrait,如下所示:

switch (windowManager.getDefaultDisplay().getRotation()) {
    case android.view.Surface.ROTATION_0:
        mCamera.setDisplayOrientation(90);
        break;
    case android.view.Surface.ROTATION_90:
        mCamera.setDisplayOrientation(0);
        break;
    case android.view.Surface.ROTATION_180:
        mCamera.setDisplayOrientation(270);
        break;
    case android.view.Surface.ROTATION_270:
        mCamera.setDisplayOrientation(180);
        break;
}

But then the picture is still saved in landscape mode. 但随后图片仍以横向模式保存。 One solution I found was to change the rotation parameter of the camera like this: 我找到的一个解决方案是更改相机的旋转参数,如下所示:

public void onOrientationChanged(int orientation) {
    if (orientation == ORIENTATION_UNKNOWN) return;
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    orientation = (orientation + 45) / 90 * 90;
    int rotation = 0;
    if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
        rotation = (info.orientation - orientation + 360) % 360;
    } else {  // back-facing camera
        rotation = (info.orientation + orientation) % 360;
    }
    mParameters.setRotation(rotation);
}

The problem with this approach is that it may just set the orientation in the EXIF header and not actually rotate the picture (which is the case with the device I'm using). 这种方法的问题在于它可能只是在EXIF标题中设置方向而不是实际旋转图片(我正在使用的设备就是这种情况)。

Another approach was rotating the actual data after the picture is taken like this: 另一种方法是在拍摄照片后旋转实际数据,如下所示:

Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
bitmap=  Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
currentData = stream.toByteArray();

But this approach takes 10 seconds (which is too long) and while I could put this code in an AsyncTask, I need the data one to a few seconds later, so then I'd still need to wait. 但是这种方法需要10秒钟(这太长了)虽然我可以将这些代码放在AsyncTask中,但是我需要一到几秒钟的数据,所以我还是需要等待。

So far I haven't found a better solution. 到目前为止,我还没有找到更好的解决方案。

I have found a solution that makes me still need to wait for a second or two, but that's fast enough for me. 我找到了一个解决方案,让我仍然需要等待一两秒钟,但这对我来说足够快。 It's a pretty simple fix, just use the Matrix.postRotate method, but change: 这是一个非常简单的修复,只需使用Matrix.postRotate方法,但更改:

bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

to: 至:

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

For me this is a pretty good solution, especially because everywhere else I already used .jpg images so it didn't make much sense compressing the Bitmap to a .png anyway. 对我来说这是一个非常好的解决方案,特别是因为在其他任何地方我已经使用过.jpg图像,所以无论如何将Bitmap压缩到.png都没有多大意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM