是setDisplayOrientation样品code正确吗? [英] Is the setDisplayOrientation sample code correct?

查看:277
本文介绍了是setDisplayOrientation样品code正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的<一个href="http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29"相对=nofollow>文档页 Camera.setDisplayOrientation包含以下code样本说明使用,它将使摄像机图像显示在相同的方向,显示:

The documentation page for Camera.setDisplayOrientation contains the following code sample stating that using it will "make the camera image show in the same orientation as the display":

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

不过,我用它,有时图像会出现倒挂有问题。经过一些试验和错误,我发现了正确的code会(将最后8行的方法):

However, I had problems using it, sometimes the image would appear upside down. After some trial-and-error, I found that the correct code would be (replacing the last 8 lines of the method):

    int result = (360 + info.orientation - degrees) % 360;
    camera.setDisplayOrientation(result);

(这意味着计算背置摄像头是正确的前摄像头了。)而补偿镜报的评论是有点不可思议,因为镜像无法通过旋转撤消,该操作只交换90°和270°旋转这并没有真正意义的我。

(Which means the calculation for back-facing cameras is correct for front cameras too.) The "compensate the mirror" comment is a bit weird since mirroring cannot be undone by rotating, that operation only swaps 90° and 270° rotations which doesn't really make sense to me.

所以,问题是:是样本错误确实还是我失去了一些东西 我试图在多个设备上,无论是正面和背面摄像头以及所有支持的方向,所以我知道,我的code是工作。一个小细节,可能是值得一提:我所有的设备返回90° info.orientation

编辑: 这里是我的相机相关的code,我有在Nexus One的与三星Galaxy S另外进行了测试。这是用在我的头跟踪的3D应用,的preVIEW显示在左下角和应该总是具有正确的取向。

Here is my camera-related code, I have tested it on a Nexus One and a Samsung Galaxy S Plus. It is used in my head-tracking 3D app, the preview is shown in the lower left corner and should always have the correct orientation.

解决方案(排序):它看起来像code是正确的,但我的测试手机(三星Galaxy S加)返回的前置摄像头的CameraInfo.orientation不正确的值。有关于preVIEW很多相关的讨论正在显示倒挂这种模式(实例<一href="http://support.tango.me/entries/20699798-android-my-outgoing-video-is-upside-down-or-rotated-what-can-i-do"相对=nofollow>这里和的>)。办法解决是包括选项手动旋转图像。

SOLUTION (sort of): It looks like the code is correct, but my testing phone (Samsung Galaxy S Plus) returns an incorrect value for the front camera's CameraInfo.orientation. There are many related discussions about the preview being shown upside down on this model (examples here and here). A way to fix is to include an option to manually rotate the image.

推荐答案

你所引用的代码段,这在我以前在我的项目中应用,在我的情况下没有问题。

The snippet you've quoted, which I used and applied in my project, is no problem in my circumstances.

对于所有我用于测试的设备(Galaxy Note的银河S III,Galaxy Nexus的银河王牌二,歌Nexus S), info.Orientation 上的所有收益270前置摄像头,以及90背面的摄像头。

For all the devices (Galaxy Note, Galaxy S III, Galaxy Nexus, Galaxy Ace II, Nexus S) I used for test, info.Orientation all return 270 on front camera, and 90 on back camera.

在这样的问题加注了一些讨论,我才发现我误会了问题,所以我把答案两部分。

After a few discuss with the question raiser, I found I misunderstood the questions, so I divide the answer in two parts.

有关错误的方向在相机preVIEW,请参见该解决方案:

For the wrong orientation in camera preview, please refer to this solution:

首先请确保 info.Orientation 将在前置摄像头,背面摄像头返回270,90。 然后,请尽量将相机设定为preVIEW活动(或同等级的处理preVIEW)方向为横向。

First please ensure info.Orientation will return 270 on front camera, 90 on back camera. Then please try set your camera preview activity (or similar class that handles the preview) orientation to landscape.

因此​​,当你通过code,你会发现:

Thus, when you go through the code, you'll find:

度= 90 的屏幕方向, info.Orientation = 270 的前置摄像头。然后你会得到的结果=(270 - 90 + 360)%360 的结果= 180 ,这意味着它将旋转顺时针180为您的前置摄像头的视野,正确的前置摄像头倒置的问题。

degree = 90 for screen orientation, info.Orientation = 270 for the front camera. Then you'll get result = (270 - 90 + 360) % 360, result = 180, which means it will rotate clock wise 180 for your front camera view that will correct the front camera upside-down issue.

如果这个 info.Orientation 适用于你,那么问题可能是:

If this info.Orientation applies to you, then the problem may be:

  1. 对于一些三星(或其他)设备(如Galaxy Note的,银河SIII),相机会只写了方向标签,而不是旋转的实际像素。
  2. 如果您使用的是前置摄像头,并使用上述code,它会显示preVIEW的正确方向,但会显示û的倒挂的图片,如果你拿的出手的。

解决方法:

/**
 *
 * Get the orientation from EXIF
 * @param filepath
 * @return orientation
 */
public int getExifOrientation(String filepath) {
    int degree = 0;
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException ex) {
        Log.e("EXIF info", "cannot read exif", ex);
    }
    if (exif != null) {
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            // We only recognize a subset of orientation tag values.
            switch(orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
            }
        }
    } else {
        degree = 1;
    }
    Log.i("EXIF info", "Orientation degrees: " + degree);
    return degree;
}

然后

if (isFromCamera) {

    if (fromFrontCam) {
        // try change here if the orientation still wrong, -90 means rotate counter-clockwise 90 degrees.
        matrix.preRotate(-90);
     } else {
        matrix.preRotate(90);
     }
} else {
    // read EXIF data
    getExifOrientation(path)
    // don't forget to handle the situation that some devices won't write exif data
    matrix.preRotate(degree);
}

这篇关于是setDisplayOrientation样品code正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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