首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >相机预览以4:3的纵横比水平拉伸

相机预览以4:3的纵横比水平拉伸
EN

Stack Overflow用户
提问于 2021-06-10 02:25:55
回答 1查看 30关注 0票数 0

当宽高比从16:9更改为4:3时,相机预览会被拉伸。

我正在更改IconButton上的previewSize单击

摄像头插件版本: 0.8.1+3

设备信息: Zenfone Max Pro M1·android-arm64·Android 11 (API 30)

相机的默认预览尺寸为1280x720,宽高比为16/9。我正在通过将预览尺寸更改为320x240来更改宽高比。所以我的纵横比是4/3。

我使用的代码如下:

代码语言:javascript
复制
class CameraScreenGithub extends StatefulWidget {
  const CameraScreenGithub({Key? key}) : super(key: key);

  @override
  _CameraScreenGithubState createState() => _CameraScreenGithubState();
}

class _CameraScreenGithubState extends State<CameraScreenGithub> {
  CameraController? _cameraController;
  Future<void>? _initializeControllerFuture;
  List<CameraDescription> _cameraDescription = [];

  @override
  void initState() {
    super.initState();
    _initializeCamera();
  }

  Future<void> _initializeCamera() async {
    _cameraDescription.addAll(await availableCameras());
    _setCamera(_cameraDescription[0]);
  }

  _setCamera(CameraDescription description) {
    _cameraController = CameraController(description, ResolutionPreset.high);
    _initializeControllerFuture = _cameraController?.initialize();
    if (mounted) setState(() {});
  }

  @override
  void dispose() {
    _cameraController?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: Stack(
                children: [
                  Center(
                    child: _cameraPreview(),
                  ),
                  Positioned(
                    top: 20,
                    left: 20,
                    right: 20,
                    child: _aspectRatioButton(),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _cameraPreview() => ClipRRect(
        borderRadius: BorderRadius.circular(16),
        child: FutureBuilder<void>(
          future: _initializeControllerFuture,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return CameraPreview(_cameraController!);
            }
            return Container();
          },
        ),
      );

  Widget _aspectRatioButton() => IconButton(
        icon: Icon(
          Icons.aspect_ratio,
          color: Colors.white,
        ),
        onPressed: () {
          _cameraController?.value =
              _cameraController!.value.copyWith(previewSize: Size(320, 240));
          setState(() {});
        },
      );
}
EN

回答 1

Stack Overflow用户

发布于 2021-06-10 11:19:35

尝试遵循我提供的代码:

camera aspect ratio

您需要使用变换比例

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67909798

复制
相关文章

相似问题

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