在颤振时相机旋转错误



当相机拍照后,图片的位置变得错误和不直,这里是我做的代码

Future pickImage(ImageSource source) async {
try {
final image = await ImagePicker.pickImage(source: source);
if(image == null) return;
final imageTemporary = File(image.path);
date = DateTime.now().toString();
print(imageTemporary);

setState(() {
this.image = imageTemporary;
this.date = date;
});


} on PlatformException catch(e) {
print('failed to pick image');
}

}

我认为这是一个bug在库你试过在物理设备?如果问题仍然存在,那么您可以使用以下解决方案:

Future<File> rotateAndCompressAndSaveImage(File image) async {
int rotate = 0;
List<int> imageBytes = await image.readAsBytes();
Map<String, IfdTag> exifData = await readExifFromBytes(imageBytes);
if (exifData != null &&
exifData.isNotEmpty &&
exifData.containsKey("Image Orientation")) {
IfdTag orientation = exifData["Image Orientation"];
int orientationValue = orientation.values[0];
if (orientationValue == 3) {
rotate = 180;
}
if (orientationValue == 6) {
rotate = -90;
}
if (orientationValue == 8) {
rotate = 90;
}
}
List<int> result = await FlutterImageCompress.compressWithList(imageBytes,
quality: 100, rotate: rotate);
await image.writeAsBytes(result);
return image;
}