repaint_boundary_utils.dart
4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* @Author: 王长春
* @Date: 2022-03-14 09:24:34
* @LastEditors: 王长春
* @LastEditTime: 2022-03-17 10:01:41
* @Description: 截图工具,生成截图,保存到相册或者保存本地cash文件夹返回文件路径(供分享使用)
*/
import 'dart:io';
import 'dart:typed_data';
import 'dart:async';
import 'dart:ui';
import 'dart:ui' as ui;
import 'package:flutter/rendering.dart';
// import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:one_key/utils/utils.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
class RepaintBoundaryUtils {
//全局key-截图key
// final GlobalKey boundaryKey = GlobalKey();
//生成截图
/// 截屏图片生成图片流ByteData
Future<String> captureImage(boundaryKey) async {
RenderRepaintBoundary boundary =
boundaryKey.currentContext.findRenderObject() as RenderRepaintBoundary;
double dpr = ui.window.devicePixelRatio; // 获取当前设备的像素比
var image = await boundary.toImage(pixelRatio: dpr);
// 将image转化成byte
ByteData byteData = (await image.toByteData(format: ImageByteFormat.png))!;
var filePath = "";
Uint8List pngBytes = byteData.buffer.asUint8List();
// 获取手机存储(getTemporaryDirectory临时存储路径)
Directory applicationDir = await getTemporaryDirectory();
// getApplicationDocumentsDirectory();
// 判断路径是否存在
bool isDirExist = await Directory(applicationDir.path).exists();
if (!isDirExist) Directory(applicationDir.path).create();
// 直接保存,返回的就是保存后的文件
File saveFile;
if (Platform.isOhos) {
// 直接保存,返回的就是保存后的文件
saveFile = await File(
applicationDir.path + "/${DateTime.now().toIso8601String()}.jpg")
.writeAsBytes(pngBytes);
} else {
saveFile = await File(
applicationDir.path + "${DateTime.now().toIso8601String()}.jpg")
.writeAsBytes(pngBytes);
}
filePath = saveFile.path;
// if (Platform.isAndroid) {
// // 如果是Android 的话,直接使用image_gallery_saver就可以了
// // 图片byte数据转化unit8
// Uint8List images = byteData!.buffer.asUint8List();
// // 调用image_gallery_saver的saveImages方法,返回值就是图片保存后的路径
// String result = await ImageGallerySaver.saveImage(images);
// // 需要去除掉file://开头。生成要使用的file
// File saveFile = new File(result.replaceAll("file://", ""));
// filePath = saveFile.path;
//
//
// } else if (Platform.isIOS) {
// // 图片byte数据转化unit8
//
// }
return filePath;
}
//申请存本地相册权限
Future<bool> getPormiation() async {
if (Platform.isIOS || Platform.isOhos) {
var status = await Permission.photos.status;
if (status.isDenied) {
Map<Permission, PermissionStatus> statuses = await [
Permission.photos,
].request();
// saveImage(globalKey);
}
return status.isGranted;
} else {
var status = await Permission.storage.status;
if (status.isDenied) {
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
].request();
}
return status.isGranted;
}
}
//保存到相册
void savePhoto(boundaryKey) async {
RenderRepaintBoundary boundary =
boundaryKey.currentContext.findRenderObject() as RenderRepaintBoundary;
double dpr = ui.window.devicePixelRatio; // 获取当前设备的像素比
var image = await boundary.toImage(pixelRatio: dpr);
// 将image转化成byte
ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
//获取保存相册权限,如果没有,则申请改权限
bool permition = await getPormiation();
var status = await Permission.photos.status;
if (permition) {
if (Platform.isIOS) {
if (status.isGranted) {
Uint8List images = byteData!.buffer.asUint8List();
final result = await ImageGallerySaver.saveImage(images,
quality: 60, name: "hello");
Utils.showToast("保存成功");
}
if (status.isDenied) {
print("IOS拒绝");
}
} else {
//安卓
if (status.isGranted) {
print("Android已授权");
Uint8List images = byteData!.buffer.asUint8List();
final result = await ImageGallerySaver.saveImage(images, quality: 60);
if (result != null) {
Utils.showToast("保存成功");
} else {
print('error');
// toast("保存失败");
}
}
}
} else {
//重新请求--第一次请求权限时,保存方法不会走,需要重新调一次
savePhoto(boundaryKey);
}
}
}