Как сделать так, чтобы изображение с камеры отображалось в SurfaceView без сжатия? - PullRequest
0 голосов
/ 25 октября 2018

Мой код 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 Mobile vision для распознавания текста с помощью камеры

    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 голосов
/ 26 октября 2018

Может быть, размер предварительного просмотра камеры установлен неправильно. Когда вы запускаете камеру, вам лучше ее установить. Экран телефона 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 .

...