如何确定摄像机预览帧的默认方向?

Com*_*are 9 android android-camera textureview android-textureview

我正在尝试(再次)创建实际上正常工作的相机预览逻辑,适用于所有场景:

  • 任何设备:手机,平板电脑,烤面包机,等等
  • 任何相机:前向,后向,侧向,面向狗,等等
  • android.hardware.Cameraandroid.hardware.camera2
  • 肖像和风景设备方向

因为我minSdkVersion15岁,因为我并不特别关心表现,所以我试图用一个TextureView.并且,根据fadden在这里这里的地方的建议,我试图setTransform()TextureView适当的情况下使用Matrix它:

  • 正确定位预览,考虑设备方向
  • TextureView完全填充,以TextureView纵横比与预览帧宽高比不匹配的裁剪为代价
  • 不会拉伸图像,因此预览正方形项目(例如,3"方形Post-ItNote®)会在预览中显示正方形

在我的情况下,TextureView填充屏幕,减去状态栏和导航栏.

adjustAspectRatio()GrafikaPlayMovieActivity.java开始,我现在有:

  private void adjustAspectRatio(int videoWidth, int videoHeight,
                                 int rotation) {
    if (iCanHazPhone) {
      int temp=videoWidth;
      videoWidth=videoHeight;
      videoHeight=temp;
    }

    int viewWidth=getWidth();
    int viewHeight=getHeight();
    double aspectRatio=(double)videoHeight/(double)videoWidth;
    int newWidth, newHeight;

    if (getHeight()>(int)(viewWidth*aspectRatio)) {
      newWidth=(int)(viewHeight/aspectRatio);
      newHeight=viewHeight;
    }
    else {
      newWidth=viewWidth;
      newHeight=(int)(viewWidth*aspectRatio);
    }

    int xoff=(viewWidth-newWidth)/2;
    int yoff=(viewHeight-newHeight)/2;

    Matrix txform=new Matrix();

    getTransform(txform);

    float xscale=(float)newWidth/(float)viewWidth;
    float yscale=(float)newHeight/(float)viewHeight;

    txform.setScale(xscale, yscale);

    switch(rotation) {
      case Surface.ROTATION_90:
        txform.postRotate(270, newWidth/2, newHeight/2);
        break;

      case Surface.ROTATION_270:
        txform.postRotate(90, newWidth/2, newHeight/2);
        break;
    }

    txform.postTranslate(xoff, yoff);

    setTransform(txform);
  }
Run Code Online (Sandbox Code Playgroud)

在这里,videoWidthvideoHeight有摄像头预览的大小,方法本身是对的子类实现TextureView.当我确定了相机预览尺寸以及TextureView自身调整大小后,我正在调用此方法.

这似乎很接近但不完全正确.特别是,iCanHazPhone黑客 - 翻转视频宽度和高度 - 是在黑暗中刺伤,没有这个,而SONY平板电脑Z2运行良好,Nexus 5变得可怕(拉伸预览不填满屏幕).

随着iCanHazPhone设置true,我得到在Nexus 5的好成绩:

Nexus 5,Camera2预览,风景,后置摄像头

Nexus 5,Camera2预览,人像,后置摄像头

随着iCanHazPhone集来false,我得到的东西,如:

Nexus 5,Camera2预览,人像,后置摄像头,拉伸

同样,iCanHazPhone设置为false,我在SONY平板电脑Z2上获得了良好的效果:

SONY平板电脑Z2,Camera2预览,风景,后置摄像头

但如果我把它翻到true,我得到:

SONY平板电脑Z2,Camera2预览,风景,后置摄像头,拉伸

我目前的理论是,不同的设备具有不同的默认摄像机方向,并且根据默认方向,我需要在计算中翻转预览宽度和高度.

所以,问题:

  1. 相机保证(与涉及Android硬件的任何内容一样)是否具有与默认设备方向匹配的默认方向?例如,Nexus 9可以正常iCanHazPhone设置为true,表示它不是手机与平板电脑,而是默认纵向与默认景观.

  2. 有没有更好的方法来解决这个问题?

ele*_*ers 8

对两个问题的回答是:使用Camera/Camera2 API提供的传感器方向来调整预览图像.

要计算相对于屏幕的相机旋转(可用于转换预览),我使用:

static int getRelativeImageOrientation(int displayRotation, int sensorOrientation,
                                       boolean isFrontFacing, boolean compensateForMirroring) {
    int result;
    if (isFrontFacing) {
        result = (sensorOrientation + displayRotation) % 360;
        if (compensateForMirroring) {
            result = (360 - result) % 360;
        }
    } else {
        result = (sensorOrientation - displayRotation + 360) % 360;
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

其中displayRotation是当前的显示旋转:

static int getDisplayRotation(Context context) {
    WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    int rotation = windowManager.getDefaultDisplay().getRotation();
    switch (rotation) {
        case Surface.ROTATION_0:
            return 0;
        case Surface.ROTATION_90:
            return 90;
        case Surface.ROTATION_180:
            return 180;
        case Surface.ROTATION_270:
            return 270;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

传统相机的sensorOrientation:

Camera.CameraInfo.orientation
Run Code Online (Sandbox Code Playgroud)

而对于Camera2:

CameraCharacteristics#get(CameraCharacteristics.SENSOR_ORIENTATION)
Run Code Online (Sandbox Code Playgroud)

在计算相机预览方向时,您应该为compansateForMirror传递false,并在计算传统的相机JPG方向时传递true.

我已经在许多设备上测试了它 - 它似乎有效,但我不能保证这是防弹的;]