【问题标题】:Camera 2 : Unable to record video in full screen?相机 2:无法全屏录制视频?
【发布时间】:2016-09-23 07:13:13
【问题描述】:

我正在使用 google 官方 sample 通过使用 Camera API 2 来解决视频录制问题。但问题是我无法使其全屏显示,因为我将屏幕方向限制为仅纵向。这里是我编辑的 xml 和 official xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <Button
            android:id="@+id/video"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/label_record" />

        <ImageView
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:contentDescription="Description"
            android:padding="20dp"
            android:src="@drawable/ic_more" />

    </FrameLayout>

</RelativeLayout>

我认为这里是按宽高比将视频屏幕尺寸设置为TextureView,但我无法将其设置为全屏。非常感谢任何帮助。

mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height, mVideoSize);
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
    mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
} else {
    mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
}

【问题讨论】:

    标签: android camera-api


    【解决方案1】:

    也许您可以实现一个变换方法,将TextureView 缩放并旋转到正确的方向。

    private void transformImage(int width, int height) {
        if (mPreviewSize == null || mTextureView == null) {
            return;
        }
        Matrix matrix = new Matrix();
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        RectF textureRectF = new RectF(0, 0, width, height);
        RectF previewRectF = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
        float centerX = textureRectF.centerX();
        float centerY = textureRectF.centerY();
        if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
            previewRectF.offset(centerX - previewRectF.centerX(),
                    centerY - previewRectF.centerY());
            matrix.setRectToRect(textureRectF, previewRectF, Matrix.ScaleToFit.FILL);
            float scale = Math.max((float) width / mPreviewSize.getWidth(),
                    (float) height / mPreviewSize.getHeight());
            matrix.postScale(scale, scale, centerX, centerY);
            matrix.postRotate(90 * (rotation - 2), centerX, centerY);
        }
        mTextureView.setTransform(matrix);
    }
    

    之后你应该从onResume调用transform方法:

    public void onResume() {
        super.onResume();
        if (mTextureView.isAvailable()) {
            transformImage(mTextureView.getWidth(), mTextureView.getHeight());
        } else {
            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        }
    }
    

    SurfaceTextureListener.Callback:

    private TextureView.SurfaceTextureListener mSurfaceTextureListener =
            new TextureView.SurfaceTextureListener() {
                @Override
                public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                    transformImage(width, height);
                }
    
                @Override
                public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
                }
    
                @Override
                public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                    return false;
                }
    
                @Override
                public void onSurfaceTextureUpdated(SurfaceTexture surface) {
                }
            };
    

    Source.

    【讨论】:

      【解决方案2】:

      您需要处理预览大小来控制录制的屏幕大小。使用相同的chooseVideoSize 方法通过获取SurfaceTexture 类的Output Sizes 来使预览全屏。

      private static Size chooseVideoSize(Size[] choices) {
              for (Size size : choices) {
                  if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
                      return size;
                  }
              }
              Log.e(TAG, "Couldn't find any suitable video size");
              return choices[choices.length - 1];
          }
      

      那是

        mPreviewSize = chooseVideoSize(map.getOutputSizes(SurfaceTexture.class));
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多