我目前正在尝试在颤动的画布上绘制图像,然后在图像上绘制矩形以覆盖特定数据。我遇到了一个问题,矩形画得很好,但图像没有画在后面。它看起来好像根本没有被绘制。
我正在将图像从字节加载到 Dart UI Image 对象中。加载图像后,我创建一个 PictureRecorder 并将其传递给 Canvas 对象。然后我尝试首先绘制图像对象,然后是顶部的矩形。完整代码如下:
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:common/ocr/scraped_card_field.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class ObfuscatedCardRenderer {
ui.Image _backgroundImage;
///
///
///
Future<ByteData> renderImage(
Uint8List imageBytes, List<ScrapedCardField> fields) async {
await _loadImageBackground(imageBytes);
var obfuscatedImage = await _obfuscateImage(fields);
var byteD =
await obfuscatedImage.toByteData(format: ui.ImageByteFormat.png);
return byteD;
}
///
///
///
Future<ui.Image> _obfuscateImage(List<ScrapedCardField> fields) async {
ui.PictureRecorder recorder = ui.PictureRecorder();
ui.Canvas c = ui.Canvas(recorder);
var paint = ui.Paint();
c.drawImage(_backgroundImage, ui.Offset(0.0, 0.0), new Paint());
for (ScrapedCardField field in fields) {
paint.color = Colors.white;
c.drawRect(field.boundingBox, paint);
}
var picture = recorder.endRecording();
return picture.toImage(_backgroundImage.width, _backgroundImage.height);
}
///
///
///
Future<void> _loadImageBackground(Uint8List imageBytes) async {
if (imageBytes != null) {
_backgroundImage = await _getImageFromBytes(imageBytes);
} else {
return null;
}
}
///
///
///
Future<ui.Image> _getImageFromBytes(Uint8List imageBytes) async {
var completer = Completer<ui.Image>();
ui.decodeImageFromList(imageBytes, (image) {
completer.complete(image);
});
return completer.future;
}
}
结果如下:
如您所见,矩形绘制得非常好,但是,背景中的图像不可见。我还应该做些什么来绘制图像,还是有其他方法可以做到这一点?
谢谢!