[英]Flutter Camera Package: Stream/Picture from Camera is -90° rotated
I am using the official Flutter (Flutter 1.22.6 • channel stable) camera Plugin ( https://pub.dev/packages/camera ) within 2 Flutter apps.我在 2 个 ZC047B10EEE763AFB6164E077ZCF1 应用程序中使用官方 Flutter(Flutter 1.22.6 • 通道稳定)相机插件( https://pub.dev/packages/camera )。 The stripped-down code sniped is used in both apps (pure copy & paste).两个应用程序都使用了精简的代码狙击(纯复制和粘贴)。
However, results in both apps are different in terms of their orientation.但是,两个应用程序的结果在方向上是不同的。 The problem is not to provide the user a correct output on the screen.问题是没有在屏幕上为用户提供正确的 output。 This is possible with either CameraPreview
or RotatedBox
.这可以通过CameraPreview
或RotatedBox
。 I am using the buildPreview()
method from the CameraController
to see "What the camera sees" (at least I hope it does).我正在使用 CameraController 中的CameraController
buildPreview()
方法来查看“相机所见”(至少我希望如此)。
What is needed is to record a picture from the CameraController-Stream in the correct orientation (straight up) to process it with some AI SDKs.需要的是从 CameraController-Stream 以正确的方向(笔直向上)记录图片,以使用一些 AI SDK 处理它。 Or as a workaround some easy and lightweight way to turn it 90 degree as an Uint8List
.或者作为一种解决方法,使用一些简单轻量的方法将其旋转 90 度作为Uint8List
。 What is not possible is to do it just with some meta-data.仅使用一些元数据是不可能的。
Should be worth mentioning the more complex app (where the rotation is wrong) has two restrictions:值得一提的是更复杂的应用程序(旋转错误)有两个限制:
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
该应用程序在 main-Method 中使用await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
进行初始化What I already tried without success:我已经尝试过但没有成功:
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
设置SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
in the initState在初始化状态await controller.lockCaptureOrientation(DeviceOrientation.portraitUp);
使用await controller.lockCaptureOrientation(DeviceOrientation.portraitUp);
I have no clue why there is this difference.我不知道为什么会有这种差异。 Any thoughts what I should try out or this is would be amazing!任何想法我应该尝试或这将是惊人的!
import 'dart:async';
import 'dart:typed_data';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Timer timer;
CameraController controller;
CameraImage image;
Future<CameraController> init;
@override
void initState() {
init = initialize();
super.initState();
}
Future<CameraController> initialize() async {
var cameras = await availableCameras();
controller = CameraController(cameras[0], ResolutionPreset.medium);
await controller.initialize();
controller.startImageStream((image) {
this.image = image;
});
timer = Timer.periodic(Duration(seconds: 5), (timer) async {
print('start scanning');
});
return controller;
}
Future<void> outText(CameraImage image) async {
Uint8List bytes = image.planes[0].bytes;
// AI magic...
return;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: FutureBuilder<CameraController>(
future: init,
builder: (context, snapshot) {
if (snapshot.hasData)
return Center(
child: snapshot.hasData
? SizedBox(child: snapshot.data.buildPreview())
: CircularProgressIndicator());
else
return SizedBox.shrink();
},
),
),
);
}
}
After a few more hours the only difference is the camera plugin version between both results.几个小时后,唯一的区别是两个结果之间的相机插件版本。
It looks like a bug that happens with camera: ^0.7.0
and later.它看起来像camera: ^0.7.0
及更高版本。
To get the correct result the workaround would be to use: camera: ^0.6.4+5
or earlier.要获得正确的结果,解决方法是使用: camera: ^0.6.4+5
或更早。
A bug is opened there: https://github.com/flutter/flutter/issues/76210那里打开了一个错误: https://github.com/flutter/flutter/issues/76210
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.