我最近开始使用 flutter 和 dart 编写代码,并且正在尝试学习它。在开发应用程序时,我试图将第一个屏幕上的一个按钮链接到另一个屏幕,在该屏幕上我启动相机以拍照。
Future<void> main() async {
// Ensure that plugin services are initialized so that `availableCameras()`
// can be called before 'runApp()'
WidgetsFlutterBinding.ensureInitialized();
// Obtain a list of the available cameras on the device.
final cameras = await availableCameras();
// Get a specific camera from the list of available cameras.
final firstCamera = cameras.first;
runApp(
MaterialApp(
theme: ThemeData.dark(),
title: "Ipm-p2",
home: Scaffold(
appBar: AppBar(title: const Text("Ipm-p2")),
body: MyStatelessWidget(firstCamera),
),
/*TakePictureScreen(
// Pass the appropriate camera to the TakePictureScreen widget.
camera: firstCamera,
),*/
),
);
}
MyStatelessWidget 是我的第一个屏幕,我有启动相机的按钮:
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
MyStatelessWidget(CameraDescription firstCamera, {Key key, this.camera}) : super(key: key);
final CameraDescription camera;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const SizedBox(height: 30),
RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => TakePictureScreen(camera: this.camera))
);
},
child: const Text('Enabled Button', style: TextStyle(fontSize: 20)),
),
],
),
);
}
}
我的问题是,当我启动应用程序并点击按钮时,它会将我带到相机屏幕,但相机无法正常工作。我需要如何传递 firstCamera 才能使其正常工作?
谢谢。
最佳答案
步骤 1-> 添加所需的依赖项。
dependencies:
flutter:
sdk: flutter
camera:
path_provider:
path:
第 2 步->获取可用摄像头的列表。
WidgetsFlutterBinding.ensureInitialized();
// Obtain a list of the available cameras on the device.
final cameras = await availableCameras();
// Get a specific camera from the list of available cameras.
final firstCamera = cameras.first;
步骤 3-> 创建并初始化 CameraController。
此过程会建立与设备摄像头的连接,让您可以控制摄像头并显示摄像头画面的预览。
// A screen that takes in a list of cameras and the Directory to store images.
class TakePictureScreen extends StatefulWidget {
final CameraDescription camera;
const TakePictureScreen({
Key key,
@required this.camera,
}) : super(key: key);
@override
TakePictureScreenState createState() => TakePictureScreenState();
}
class TakePictureScreenState extends State<TakePictureScreen> {
// Add two variables to the state class to store the CameraController and
// the Future.
CameraController _controller;
Future<void> _initializeControllerFuture;
@override
void initState() {
super.initState();
// To display the current output from the camera,
// create a CameraController.
_controller = CameraController(
// Get a specific camera from the list of available cameras.
widget.camera,
// Define the resolution to use.
ResolutionPreset.medium,
);
// Next, initialize the controller. This returns a Future.
_initializeControllerFuture = _controller.initialize();
}
@override
void dispose() {
// Dispose of the controller when the widget is disposed.
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// Fill this out in the next steps.
}
}
第 4 步->使用 CameraPreview 显示相机的画面。
您可以使用 CameraController 的 takePicture() 方法拍照。在此示例中,创建一个 FloatingActionButton,当用户点击按钮时使用 CameraController 拍照。
第 5 步->使用 CameraController 拍照。
您可以使用 CameraController 的 takePicture() 方法拍照。在此示例中,创建一个 FloatingActionButton,当用户点击按钮时使用 CameraController 拍照。
FloatingActionButton(
child: Icon(Icons.camera_alt),
// Provide an onPressed callback.
onPressed: () async {
// Take the Picture in a try / catch block. If anything goes wrong,
// catch the error.
try {
// Ensure that the camera is initialized.
await _initializeControllerFuture;
// Construct the path where the image should be saved using the path
// package.
final path = join(
// Store the picture in the temp directory.
// Find the temp directory using the `path_provider` plugin.
(await getTemporaryDirectory()).path,
'${DateTime.now()}.png',
);
// Attempt to take a picture and log where it's been saved.
await _controller.takePicture(path);
} catch (e) {
// If an error occurs, log the error to the console.
print(e);
}
},
)
第 6 步->使用图像小部件显示图片。
如果您成功拍摄照片,则可以使用图像小部件显示保存的照片。在这种情况下,图片作为文件存储在设备上。
Image.file(File('path/to/my/picture.png'))
https://stackoverflow.com/questions/64819295/
相关文章:
unity3d - Rigidbody AddForce 在 Photon Unity 中不起作用
angular - 从 - "createAction"函数获取类型
html - 使用 selenium 抓取网页时缺少 HTML 内容
ios - Swift 2.0 .contextMenu 从核心数据中多次删除
elasticsearch - FluentBit 和 ES 的重复和缺失日志条目
javascript - 如何使 vuetify 复选框仅在单击框而不是标签时使用react?
perl - 我怎样才能确保函数永远不会在 perl 中同时执行?