在Android设备(OnePlus 6)上拍照时,在takePicture()
之后稍作裁剪。
不进行任何处理,使用返回的filePath
中的图像,图像的各面大约放大/裁剪5-10%。
iOS设备上完全相同的代码,没有此问题。
初始化
getCameras().then((_) {
cameraController = CameraController(cameras[0], ResolutionPreset.high);
cameraController.initialize().then((_) {
if (!mounted) return;
setState(() {});
});
});
UI
Container(
width: previewWidth,
height: previewWidth,
child: cameraController != null
? cameraController.value.isInitialized
? ClipRect(
child: OverflowBox(
alignment: Alignment.center,
child: FittedBox(
fit: BoxFit.fitWidth,
child: Container(
width: previewWidth,
height: previewWidth / cameraController.value.aspectRatio,
child: cameraController.value.isInitialized && cameraController != null
? RotatedBox(child: CameraPreview(cameraController), quarterTurns: 0,)
: Container(), // this is my CameraPreview
),
),
),
)
: Container()
: Container(),
),
onPressed
快门按钮
onPressed: () async {
String filePath = await takePicture();
setState(() {
node.image = Image.file(File(filePath));
node.imageFile = File(filePath);
});
},
Future<String> takePicture() async {
if (!cameraController.value.isInitialized) {
return null;
}
final Directory extDir = await getTemporaryDirectory();
final String dirPath = '${extDir.path}/images/ptb';
await Directory(dirPath).create(recursive: true);
String fileName =
'temp_' + (DateTime.now().millisecondsSinceEpoch * 1000).toString();
final String filePath = '$dirPath/$fileName.jpg';
if (cameraController.value.isTakingPicture) {
return null;
}
try {
await cameraController.takePicture(filePath);
} on CameraException catch (e) {
Alert().showSimpleAlert(
context: context,
title: "Oops! Error",
message: 'Error: ${e.code}\n${e.description}');
return null;
}
return filePath;
}