【问题标题】:Camera preview is stretching horizonatlly with 4:3 aspect ratio相机预览以 4:3 纵横比水平拉伸
【发布时间】:2021-06-09 18:25:54
【问题描述】:

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

我正在更改 IconButton click 上的 previewSize
相机插件版本:0.8.1+3
设备信息:Zenfone Max Pro M1 • android-arm64 • Android 11 (API 30)

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

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(() {});
        },
      );
}

【问题讨论】:

    标签: flutter camera


    【解决方案1】:

    尝试按照我提供的代码进行操作:

    camera aspect ratio

    你需要使用Transform scale

    【讨论】:

    • 因为对我不起作用。你能提供我完整的工作代码,这样对我更有帮助吗
    • 我给定的代码在默认模式(16/9)下正常工作。我想将纵横比更改为 (4/3)
    猜你喜欢
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多