Android相机在纵向模式下保存图片 [英] Android Camera Saving Pictures in Portrait Mode

查看:199
本文介绍了Android相机在纵向模式下保存图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用中使用相机,我希望能够在横向和纵向模式下使用它。我在横向模式下创建图片没有任何困难,但我还没有找到一种以纵向模式保存图片的好方法。

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.

当我想拍照时在纵向模式下,我需要先将displayorientation设置为portrait,如下所示:

When I want to make a picture in portrait mode I need to set the displayorientation to portrait first, like this:

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);
}

这种方法的问题在于它可能只是在EXIF中设置方向标题并没有实际旋转图片(我正在使用的设备就是这种情况)。

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).

另一种方法是在拍摄照片之后旋转实际数据:

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();

但这种方法需要10秒钟(这太长了)而我可以将此代码放入AsyncTask,我需要一到几秒后的数据,所以我还需要等待。

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.

到目前为止,我还没有找到更好的解决方案。 / p>

So far I haven't found a better solution.

推荐答案

我找到了一个让我仍然需要等待一两秒的解决方案,但这对我来说足够快了。
这是一个非常简单的修复,只需使用Matrix.postRotate方法,但更改:

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:

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

to:

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

对我来说这是一个非常好的解决方案,特别是因为其他地方我已经使用了.jpg图像所以它无论如何,将Bitmap压缩为.png并没有多大意义。

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.

这篇关于Android相机在纵向模式下保存图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆