Android如何显示前置摄像头(仅适用于镜子)

时间:2018-11-26 09:01:20

标签: android kotlin android-camera android-permissions

我想显示仅用于镜子的布局中的前置摄像头,我只想要那样,并且在摄像头布局上可能没有几个简单的按钮。 注意:我不想拍照。只想显示前置摄像头的镜子。 是否有实现该目标的最佳方法。 我曾经尝试下面的代码,但是相机显示的旋转错误类似于横向模式,并且图像拉伸。

public Person Post([FromBody]Person person)
{
    Person dbPerson = Persons.Find((x) => x.Id == person.Id);
    if (dbPerson != null)
    {
        dbPerson = person;
    }
    else
    {
        Persons.Add(dbPerson);
    }
    return dbPerson;
}

1 个答案:

答案 0 :(得分:1)

以下代码段有助于打开前置摄像头

private Camera openFrontFacingCamera() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx<cameraCount; camIdx++) {
        Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            try {
                cam = Camera.open(camIdx);
            } catch (RuntimeException e) {
                Log.e("Your_TAG", "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }
    return cam; }
  

setDisplayOrientation()

方法可用于旋转相机视图。尝试使用以下代码片段来管理相机方向。

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