如何解决相机抖动中照片的旋转错误? [英] How to fix wrong rotation of photo from camera in flutter?

查看:152
本文介绍了如何解决相机抖动中照片的旋转错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新的相机插件版本拍照,并且使用的是flutter示例中的代码.这是我选择相机的方式:

I'm taking a photo with the newest camera plugin version and I'm using code from flutter example. This is how I pick a camera:

final cameras = await availableCameras();
final firstCamera = cameras.first;

这是init内部的内容:

This is inside init:

_cameraController = CameraController(
  widget.camera,
  ResolutionPreset.medium,
  enableAudio: false,
);

这是相关代码的其余部分:

This is the rest of the relevant code:

Future _takePhoto(BuildContext context) async {
  try {
    await _initializeControllerFuture;
    final path = join(
      (await getTemporaryDirectory()).path,
      '${DateTime.now()}.png',
    );

    await _cameraController.takePicture(path);

    setState(() {
      _imagePath = path;
    });
  } catch (e) {
    print(e);
  }
}

然后,我用Image.file(File(_imagePath))向用户展示照片,然后将照片发送给API. 问题是照片有时会以错误的方向拍摄. (我对此很确定,因为照片也在数据库中旋转了.)例如,在3岁的小米手机上,它可以完美运行,但是在某些新的三星手机上,照片总是在旋转.

Afterwards, I show the photo to the user with Image.file(File(_imagePath)) and later I send the photo to API. The problem is that the photo is sometimes captured with a wrong orientation. (I'm sure about this because the photo is also rotated in the database.) For example, on 3 years old Xiaomi phone, it works flawlessly, but on a certain new Samsung phone, the photo is always rotated.

如何确保旋转始终正确? (甚至在ios设备上)

How to make sure that the rotation is always correct? (Even on ios devices)

推荐答案

您可以使用软件包 https://pub .dev/packages/flutter_exif_rotation
支持iOSAndroid
在某些设备中,当exif数据实际为纵向时,它们会以横向模式显示图片.
该插件可修复使用这些设备拍摄的照片的方向.

You can use package https://pub.dev/packages/flutter_exif_rotation
Support iOS and Android
In some devices the exif data shows picture in landscape mode when they're actually in portrait.
This plugin fixes the orientation for pictures taken with those devices.

对于Android
将此添加到您的AndroidManifest.xml

For Android
Add this in your AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

代码段

image = await FlutterExifRotation.rotateImage(path: image.path);

//Note : iOS not implemented
image = await FlutterExifRotation.rotateAndSaveImage(path: image.path);

这篇关于如何解决相机抖动中照片的旋转错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆