android - 是否可以裁剪相机预览?

标签 android android-camera

我还没有找到任何方法来裁剪相机预览,然后将其显示在 SurfaceView 上。

Android - 是否可以裁剪相机预览?

最佳答案

您可以在没有叠加 View 的情况下执行此操作(并非在所有情况下都有效)。

子类 ViewGroup,添加 SurfaceView 作为唯一的 child ,然后:

  1. 在 onMeasure 中提供您想要的裁剪尺寸。
  2. 在 onLayout 中,使用未裁剪的尺寸布局 SurfaceView。

基本上,

public class CroppedCameraPreview extends ViewGroup {
  private SurfaceView cameraPreview;
  public CroppedCameraPreview( Context context ) {
    super( context );
    // i'd probably create and add the SurfaceView here, but it doesn't matter
  }
  @Override
  protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) {
    setMeasuredDimension( croppedWidth, croppedHeight );
  }
  @Override
  protected void onLayout( boolean changed, int l, int t, int r, int b ) {
    if ( cameraPreview != null ) {
      cameraPreview.layout( 0, 0, actualPreviewWidth, actualPreviewHeight );
    }
  }
}

关于android - 是否可以裁剪相机预览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8601982/

相关文章:

android - 如何将 ListView 的内容放到 EditText 中?

android - 如何制作假相机

Android 无法使用前置摄像头录制视频,MediaRecorder 启动失败 : -19

android - ActivityCompat 和 ContextCompat 有什么区别?

java - 当子字符串包含某些字符时更改整个字符串

android - 如何在集群管理器中的标记中调用 showinfowindow()?

java - 设置首选项(在 PreferenceFragment 内)响应单击

Android CameraSource 对多种设备的工作方式不同并且不会旋转图像

android - 如何使用 Android 11 中另一个前台服务的打开相机运行前台服务?

android - 当我尝试在选项卡之间切换时应用程序崩溃