颤动相机 - 预览旋转

问题描述 投票:0回答:2

由于某种原因(仅在某些手机上)我的相机预览旋转了 90 度。

调试时的设备方向实用程序对于工作和不工作的手机说了同样的事情:

D/CameraOrientationUtil(14552): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90

使用

camera: ^0.11.0+1

这就是我构建预览的方式:

Expanded(child: _cameraController!.buildPreview()),

结果是:

enter image description here

当我拍照时,旋转很好:

enter image description here

我可以旋转视图,但“工作”手机会旋转...如果有人有任何想法,我将非常感激。

flutter flutter-camera
2个回答
0
投票

我遇到了同样的问题:只需将相机降级到版本^0.10.6


0
投票

试试这个

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:image/image.dart' as img;
import 'package:image_picker/image_picker.dart';

Future<img.Image?> fixImageRotation(String imagePath) async {
  try {
    final bytes = await File(imagePath).readAsBytes();
    final image = img.decodeImage(Uint8List.fromList(bytes));
    if (image == null) return null;

    final exifData = await readExifFromBytes(bytes);
    final orientation = exifData['Orientation']?.printable ?? '1';

    int rotationDegrees;
    switch (orientation) {
      case '3':
        rotationDegrees = 180;
        break;
      case '6':
        rotationDegrees = 90;
        break;
      case '8':
        rotationDegrees = 270;
        break;
      default:
        rotationDegrees = 0;
    }

    if (rotationDegrees != 0) {
      return img.copyRotate(image, rotationDegrees);
    }
    return image;
  } catch (e) {
    print(e);
    return null;
  }
}

Future<Map<String, IfdTag>> readExifFromBytes(Uint8List bytes) async {
  final byteData = ByteData.sublistView(bytes);
  final exifData = await readExifFromByteData(byteData);
  return exifData;
}
© www.soinside.com 2019 - 2024. All rights reserved.