flutter camera 获取图片转base64
时间: 2023-07-26 14:45:42 浏览: 201
要使用 Flutter 的相机插件获取图片并将其转换为 Base64 格式,您可以使用以下方法:
1. 添加相机插件依赖
在 `pubspec.yaml` 文件中添加下面的依赖:
```yaml
dependencies:
camera: ^0.9.4+5
```
2. 导入相机插件
在需要使用相机的文件中,导入相机插件:
```dart
import 'package:camera/camera.dart';
```
3. 初始化相机
在 `initState` 方法中初始化相机,并设置参数:
```dart
List<CameraDescription> cameras;
Future<void> _initializeCamera() async {
cameras = await availableCameras();
controller = CameraController(cameras[0], ResolutionPreset.medium);
await controller.initialize();
setState(() {});
}
```
4. 拍照并将图片转换为 Base64
在需要拍照的方法中,使用 `takePicture` 方法获取图片,并使用 `dart:convert` 包中的 `base64Encode` 方法将图片转换为 Base64 格式:
```dart
void _takePicture() async {
if (!controller.value.isInitialized) {
return;
}
final Directory extDir = await getApplicationDocumentsDirectory();
final String dirPath = '${extDir.path}/Pictures/flutter_test';
await Directory(dirPath).create(recursive: true);
final String filePath = '$dirPath/${DateTime.now().millisecondsSinceEpoch}.jpg';
await controller.takePicture(filePath);
final File file = File(filePath);
List<int> imageBytes = await file.readAsBytes();
String base64Image = base64Encode(imageBytes);
print(base64Image);
}
```
5. 完整示例代码
```dart
import 'dart:convert';
import 'dart:io';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Camera Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Camera Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
CameraController controller;
List<CameraDescription> cameras;
Future<void> _initializeCamera() async {
cameras = await availableCameras();
controller = CameraController(cameras[0], ResolutionPreset.medium);
await controller.initialize();
setState(() {});
}
@override
void initState() {
super.initState();
_initializeCamera();
}
void _takePicture() async {
if (!controller.value.isInitialized) {
return;
}
final Directory extDir = await getApplicationDocumentsDirectory();
final String dirPath = '${extDir.path}/Pictures/flutter_test';
await Directory(dirPath).create(recursive: true);
final String filePath = '$dirPath/${DateTime.now().millisecondsSinceEpoch}.jpg';
await controller.takePicture(filePath);
final File file = File(filePath);
List<int> imageBytes = await file.readAsBytes();
String base64Image = base64Encode(imageBytes);
print(base64Image);
}
@override
Widget build(BuildContext context) {
if (!controller.value.isInitialized) {
return Container();
}
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: CameraPreview(controller),
),
floatingActionButton: FloatingActionButton(
onPressed: _takePicture,
tooltip: 'Take Picture',
child: Icon(Icons.camera),
),
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
```
在上面的示例代码中,我们使用 `camera` 插件获取相机图片,并将其转换为 Base64 格式。在点击拍照按钮时,我们调用 `_takePicture` 方法,该方法会将图片保存到应用程序文档目录中的 `Pictures/flutter_test` 文件夹中,并将其转换为 Base64 格式。完成后,Base64 编码的图片数据将打印到控制台上。
阅读全文