将相机SurfaceView旋转为纵向

fra*_*rak 12 camera android rotation

我已经查看了一些关于使用表面视图更改相机方向的帖子,但我从以下示例中获取了代码:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

提供尺寸的功能看起来像这样......

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我更改设备的方向时,预览图片保持风景.我尝试设置相机的方向,但这导致了非常奇怪的结果.有谁知道我需要更改才能正确旋转?

Edd*_*ala 32

正确调整相机预览方向的代码有点复杂,因为它必须考虑在内

  1. 传感器的相对方向和设备的"自然"方向(电话的纵向,平板电脑的景观,通常)
  2. 设备的当前UI方向(纵向,横向或每个的反向)
  3. 相关摄像头是前置摄像头还是后置摄像头(因为前置预览流是水平镜像的)

Camera.setDisplayOrientation的文档包含有关如何正确处理此问题的示例代码.我在这里复制它:

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);
}
Run Code Online (Sandbox Code Playgroud)

在绘制UI之后调用此方法(onSurfaceChanged将用作指示符)或设备UI旋转(onConfigurationChanged将用作指示符).


小智 9

通过将以下代码放在onSurfaceChanged()中,我能够解决旋转问题:

    if (mHolder.getSurface() == null) {
        // preview surface does not exist
        return;
    }

    // stop preview before making changes
    try {
        mCamera.stopPreview();
    } catch (Exception e) {
        // ignore: tried to stop a non-existent preview
    }

    // make any resize, rotate or reformatting changes here
    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {

        mCamera.setDisplayOrientation(90);

    } else {

        mCamera.setDisplayOrientation(0);

    }
    // start preview with new settings
    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e) {
        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
Run Code Online (Sandbox Code Playgroud)

但这创造了另一个问题,或更好的放置,没有解决问题 - 虽然正确定位,预览图像仍然只占用景观视图所做的相同数量的空间.我最终只是放弃并强迫横向进行:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Run Code Online (Sandbox Code Playgroud)

在onCreate()中设计了我的横向布局.祝你好运,我希望有人能解决这个次要问题!


VIG*_*GAN 6

试试这个,但我试过三星Galaxy Tab

public void surfaceCreated(SurfaceHolder holder)
{
   // The Surface has been created, acquire the camera and tell it where to draw.
   mCamera = Camera.open();

   Parameters params = mCamera.getParameters();

   if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
   {
    params.set("orientation", "portrait");
    mCamera.setDisplayOrientation(90);
   }

    try
      {
      mCamera.setPreviewDisplay(holder);
      }
      catch (IOException exception)
      {
        mCamera.release();
        mCamera = null;
      }

}
Run Code Online (Sandbox Code Playgroud)