【发布时间】: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(() {});
},
);
}
【问题讨论】: