当宽高比从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。
我使用的代码如下:
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(() {});
},
);
}发布于 2021-06-10 11:19:35
https://stackoverflow.com/questions/67909798
复制相似问题