很多应用都需要使用到设备的相机模块拍摄图片和视频。因此,Flutter 提供了 camera
插件。camera
插件提供了一系列可用的相机,并使用特定的相机展示相机预览、拍照、录视频。
这个章节将会讲解如何使用 camera
插件去展示相机预览、拍照并显示。
步骤
CameraController
CameraPreview
展示相机的帧流
CameraController
拍摄一张图片
Image
组件展示图片
为了完成这个章节,你需要向你的应用添加三个依赖:
camera
- 提供使用设备相机模块的工具
path_provider
- 寻找存储图片的正确路径
path
- 创建适配任何平台的路径
dependencies:
flutter:
sdk: flutter
camera:
path_provider:
path:
2. 获取可用相机列表
接着,你可以使用 camera
插件获取可用相机列表。
CameraController
。
在这个过程中,与设备相机建立了连接并允许你控制相机并展示相机的预览帧流。
(1)创建一个带有 State
类的 StatefulWidget
组件
(2)添加一个变量到 State
类来存放 CameraController
(3)添加另外一个变量到 State
类中来存放 CameraController.initialize()
返回的 Future
(4)在 initState()
方法中创建并初始化控制器
(5)在 dispose()
方法中销毁控制器
CameraController
,你就
不能
使用相机预览和拍照。
camera
中的
CameraPreview
组件来展示相机预览帧流。
_initializeControllerFuture()
执行完毕才去展示
CameraPreview
。
FutureBuilder
完成这个任务。
// You must wait until the controller is initialized before displaying the
// camera preview. Use a FutureBuilder to display a loading spinner until the
// controller has finished initializing.
FutureBuilder(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview.
return CameraPreview(_controller);
} else {
// Otherwise, display a loading indicator.
return Center(child: CircularProgressIndicator());
}
},
)
CameraController
的
takePicture
方法拍照。
在这个示例中,创建了一个浮动按钮
FloatingActionButton
,用户点击这个按钮,就能通过
CameraController
来拍摄图片。
(1)确保相机模块已经被初始化完成
(2)创建图片需要被保存的路径
(3)使用控制器拍摄一张图片并保存结果到上述路径
try / catch
方法块中来处理可能发生的异常。
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);
}
},
),
)
Image
组件展示被保存的图片。
在这个示例中,这张图片是以文件的形式存储在设备中。
File
给
Image.file
构造函数。
你能够通过传递你在上一步中创建的路径来创建一个
File
类的实例。
Image.file(File('path/to/my/picture.png'))
import 'dart:async';
import 'dart:io';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart' show join;
import 'package:path_provider/path_provider.dart';
Future main() async {
// 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(),
home: TakePictureScreen(
// Pass the appropriate camera to the TakePictureScreen widget.
camera: firstCamera,
),
),
);
}
// A screen that allows users to take a picture using a given camera.
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 {
CameraController _controller;
Future _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) {
return Scaffold(
appBar: AppBar(title: Text('Take a picture')),
// Wait until the controller is initialized before displaying the
// camera preview. Use a FutureBuilder to display a loading spinner
// until the controller has finished initializing.
body: FutureBuilder(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview.
return CameraPreview(_controller);
} else {
// Otherwise, display a loading indicator.
return Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: 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
// pattern 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);
// If the picture was taken, display it on a new screen.
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(imagePath: path),
),
);
} catch (e) {
// If an error occurs, log the error to the console.
print(e);
}
},
),
);
}
}
// A widget that displays the picture taken by the user.
class DisplayPictureScreen extends StatelessWidget {
final String imagePath;
const DisplayPictureScreen({Key key, this.imagePath}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Display the Picture')),
// The image is stored as a file on the device. Use the `Image.file`
// constructor with the given path to display the image.
body: Image.file(File(imagePath)),
);
}
}