首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >改变CameraPreview高宽比(颤振)

改变CameraPreview高宽比(颤振)
EN

Stack Overflow用户
提问于 2021-12-19 18:30:43
回答 1查看 482关注 0票数 1

我有一个应用程序,我有一个Scaffold和一个AppBar和底部广告横幅。

在中间,有来自相机插件的CameraPreview在颤振。

由于CameraPreview是用来获取设备/摄像机的高宽比的,所以CameraPreview并不占用整个可用空间,给大多数设备留下了额外的空间。

我试图对CameraPreview进行裁剪,以便只显示适合于可用空间的任何内容。它成功了,但是现在预览已经扩展了。

代码语言:javascript
运行
复制
LayoutBuilder(
    builder: (context, constraints) {
        final cameraController = controller.cameraController!;
        if(cameraController.value.previewSize != null) {
            return ClipRect(
                child: OverflowBox(
                alignment: Alignment.center,
                    child: FittedBox(
                    fit: BoxFit.fitWidth,
                    child: SizedBox(
                        width: constraints.maxWidth,
                        height: constraints.maxWidth,
                        child: AspectRatio(
                            aspectRatio: cameraController.value.aspectRatio,
                            child: CameraPreview(cameraController),
                            ),
                         ),
                     ),
                 ),
            );
        } else {
            return const SizedBox.shrink();
        }
    },
)

我尝试了其他解决方案,如Transform.scale,但这只是放大预览,它不会改变比率或拉伸。

在包中寻找解决方案本身也无济于事,大多数类似的问题都在拖延,或者已经因为拖延而关闭了。

我在这里该怎么做?我应该手动剪辑预览值吗?

EN

回答 1

Stack Overflow用户

发布于 2021-12-21 17:50:56

检查下面的代码,

使用MediaQuery获取屏幕大小&计算高宽比小部件的比例,并向其添加CameraPreview(),如下所示

代码语言:javascript
运行
复制
    // get screen size
    final size = MediaQuery.of(context).size;

    // calculate scale for aspect ratio widget
    var scale = cameraController.value.aspectRatio / size.aspectRatio;

    // check if adjustments are needed...
    if (cameraController.value.aspectRatio < size.aspectRatio) {
      scale = 1 / scale;
    }

return Transform.scale(
      scale: scale,
      child: Center(
        child: AspectRatio(
          aspectRatio: cameraController.value.aspectRatio,
          child: CameraPreview(cameraController),
        ),
      ),
    );

完整代码

代码语言:javascript
运行
复制
 @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        if (controller != null && controller.value.isRecordingVideo) {
          //stop video
        }
      },
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        key: _scaffoldKey,
        body: Container(
          child: Stack(
            children: [
              Positioned(
                child: Container(
                  alignment: Alignment.center,
                  child: cameraScreen(),
                ),
              ),
              Positioned(
                child: Container(
                  alignment: Alignment.bottomCenter,
                  child: Container(
                    height: MediaQuery.of(context).size.height * .1,
                    color: Colors.black54,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        //can add Controls
                      ],
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget cameraScreen() {
    final CameraController cameraController = controller;
    if (cameraController == null || !cameraController.value.isInitialized) {
      return Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        color: Colors.black,
        child: Center(
          child: Text(
            "Loading Camera...",
            style: CameraTextStyle.cameraUtilLoadingStyle(),
          ),
        ),
      );
    } else {
      return cameraWidget(context, cameraController);
    }
  }

  Widget cameraWidget(context, cameraController) {
    // get screen size
    final size = MediaQuery.of(context).size;

    // calculate scale for aspect ratio widget
    var scale = cameraController.value.aspectRatio / size.aspectRatio;

    // check if adjustments are needed...
    if (cameraController.value.aspectRatio < size.aspectRatio) {
      scale = 1 / scale;
    }

    return Transform.scale(
      scale: scale,
      child: Center(
        child: AspectRatio(
          aspectRatio: cameraController.value.aspectRatio,
          child: CameraPreview(cameraController),
        ),
      ),
    );
  }

  Widget cameraSwitch() {
    final CameraController cameraController = controller;
    return Container(
      child: InkWell(
        onTap: () {
          if (cameraController != null &&
              cameraController.value.isInitialized &&
              !cameraController.value.isRecordingVideo) {
            if (cameras.isNotEmpty) {
              if (selectedCamera == cameras[0]) {
                selectedCamera = cameras[1];
                onNewCameraSelected(selectedCamera);
              } else {
                selectedCamera = cameras[0];
                onNewCameraSelected(selectedCamera);
              }
            }
          }
          setState(() {});
        },
        child: Icon(
          Icons.switch_camera,
          size: 30,
          color: Colors.white,
        ),
      ),
    );
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70414255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档