如何使相机视图在SurfaceView中显示而不会缩小?

时间:2018-10-25 14:33:30

标签: c# android-studio xamarin.android surfaceview google-vision

我的axml代码是

 <RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:padding="16dp"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
 <SurfaceView
    android:id="@+id/surface_view"
    android:layout_width="match_parent"
    android:layout_height="200px"/>
</RelativeLayout>  

我正在使用移动视觉文本API通过摄像头识别文本

    cameraSource = new CameraSource.Builder(ApplicationContext, 
        textRecognizer)     
       .SetFacing(CameraFacing.Back).SetRequestedPreviewSize(1280,height).
                SetRequestedFps(20.0f).
                SetAutoFocusEnabled(true).Build();

当我的凸轮缩小到SurfaceView高度时,如何使相机适应SurfaceView

enter image description here

1 个答案:

答案 0 :(得分:0)

也许您的相机预览尺寸设置不正确。当您启动相机时,最好对其进行设置。Android手机的屏幕太多,如果没有正确的预览尺寸,那就错了。 >

 //Compare the preview size closest to the aspect ratio by comparison (if the same size, preference)
 //return Get the closest to the original width ratio
   public static Camera.Size getCloselyPreSize(bool isPortrait, int surfaceWidth, int surfaceHeight, List<Camera.Size> preSizeList)
    {
        int reqTmpWidth;
        int reqTmpHeight;
        // When the screen is vertical, you need to change the width and height to ensure that the width is greater than the height.
        if (isPortrait)
        {
            reqTmpWidth = surfaceHeight;
            reqTmpHeight = surfaceWidth;
        }
        else
        {
            reqTmpWidth = surfaceWidth;
            reqTmpHeight = surfaceHeight;
        }
        //First find the size of the same width and height as the surfaceview in the preview.
        foreach (Camera.Size size in preSizeList)
        {
            if ((size.Width == reqTmpWidth) && (size.Height == reqTmpHeight))
            {
                return size;
            }
        }
        // Get the size closest to the incoming aspect ratio
        float reqRatio = ((float)reqTmpWidth) / reqTmpHeight;
        float curRatio, deltaRatio;
        float deltaRatioMin = Float.MaxExponent;
        Camera.Size retSize = null;
        foreach (Camera.Size size in preSizeList)
        {
            curRatio = ((float)size.Width) / size.Height;
            deltaRatio = System.Math.Abs(reqRatio - curRatio);
            if (deltaRatio < deltaRatioMin)
            {
                deltaRatioMin = deltaRatio;
                retSize = size;
            }
        }

        return retSize;
    }

然后在初始化相机时设置预览大小:

  Camera.Parameters parameters = mCamera.GetParameters();
  Camera.Size preSize = CameraUtil.getCloselyPreSize(true, surfaceViewWidth, surfaceViewHeight, 
  parameters.getSupportedPreviewSizes());
  parameters.SetPreviewSize(preSize.Width, preSize.height);
  mCamera.SetParameters(parameters);

我发现与此类似的讨论,但它是本地android系统,您可以参考this