我正在尝试将
screenshot
包捕获的图像转换为位图。这是代码:
import 'package:flutter/material.dart';
import 'package:screenshot/screenshot.dart';
import 'package:image/image.dart' as img;
onPressed: () {
screenshotController
.captureAsUiImage(delay: Duration(milliseconds: 10))
.then((capturedImage) async {
final encoder = img.BmpEncoder();
final encodedImg =
encoder.encodeImage(capturedImage as img.Image);
}).catchError((onError) {
print(onError);
});
},
它构建时没有错误,但是当我单击按钮时,我在终端中看到以下内容:
flutter: type 'Image' is not a subtype of type 'Image' in type cast where
Image is from dart:ui
Image is from package:image/src/image.dart
我理解这个问题,
captureAsUiImage()
返回ui:Image
,而encodeImage()
需要img:Image
。但我不知道如何在它们之间进行转换。
或者是否有更好的方法将捕获的图像转换为位图。
尝试将图像捕获为 Uint8List:
onPressed: () {
screenshotController
.capture()
.then((Uint8List bytes) {
Bitmap bitmapImg = _fromUint8List(bytes);
}).catchError((onError) {
print(onError);
});
},
_fromUint8ListToBitmap(Uint8List bytes){
Bitmap bitmap = Bitmap.fromHeadful(imageWidth, imageHeight, bytes);
return bitmap;
}
并根据图像的宽度和高度更改
imageWidth
和 imageHeight
。