0

My axml code is

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

I am using Mobile vision text API to recognize text by camera

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

how to make camera adjust to SurfaceView, at the moment my cam is shrinking to SurfaceView height

enter image description here

VINNUSAURUS
  • 1,518
  • 3
  • 18
  • 38

1 Answers1

0

Maybe your camera preview size is not setted correctly.When you init camera ,you'd better set it.The screen of an android phone too more,if haven't a corret preview size ,it will be wrong.

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

then set preview size when init camera:

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

I find a similar discussion about this but is native android,you can refer to this.

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30