Flutter - 将图像转换为位图

问题描述 投票:0回答:2

我正在尝试将

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
。但我不知道如何在它们之间进行转换。 或者是否有更好的方法将捕获的图像转换为位图。

flutter dart
2个回答
1
投票

尝试将图像捕获为 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


0
投票

尝试这个套餐

final encodedImg = capturedImage.bmpUint8List;

我是作者。

© www.soinside.com 2019 - 2024. All rights reserved.