问题描述
我正在尝试将字节缓冲区中的图像添加到画布元素:
I am trying at add an image from a byte buffer, to a canvas element:
Uint8List image = bd.buffer.asUint8List(++offset, len);
await ui.decodeImageFromList(image, addIMage);
其中回调函数addImage()为
Where the callback function, addImage() is
void printImage(data){
print("Image stuff");
print(data);
canvasPainter.addItem(data);
}
画布画家实现了CustomPainter,如下所示:
Canvas painter is implements CustomPainter and is shown below:
class CanvasPainter implements CustomPainter{
var items = new List();
void addItems(var item){
items.add(item);}
@override
void paint(Canvas canvas, Size size){
for (var item in items){
canvas.drawImage(item,new Offset(50.0,50.0),new Paint();
}
...
}
class WriteScreen extends StatefulWidget {
ScreenState state;
@override
ScreenState createState() => state = new ScreenState();
}
class ScreenState extends State<WriteScreen> {
GestureDetector touch;
CustomPaint canvas;
CanvasPainter canvasPainter;
@override
void initState() {
super.initState();
canvasPainter = new CanvasPainter(const Color.fromRGBO(255, 255, 255, 1.0));
}
@override
Widget build(BuildContext context) {
touch = new GestureDetector(
onPanStart: panStart
);
final mediaQueryData = MediaQuery.of(context);
canvasPainter.size = mediaQueryData.size;
canvas = new CustomPaint( painter: canvasPainter);
}
}
我在CustomPainter类中收到编译时错误:
I am receiving a compile time error in my CustomPainter class:
compiler message: lib/canvas_painter.dart:64:28: Error: A value of type '#lib1::Image' can't be assigned to a variable of type 'dart.ui::Image'.
compiler message: Try changing the type of the left hand side, or casting the right hand side to 'dart.ui::Image'.
compiler message: canvas.drawImage(item, new Offset(50.0, 50.0), strokePaint);
compiler message:
由于我正在导入图像,因此我对错误消息感到非常困惑来自dart.ui,所以我不确定为什么编译器会抛出此异常。
I am very confused by the error message, as I am importing Image from dart.ui so I am unsure as to why the compiler is throwing this exception.
任何帮助/建议都将不胜感激。
Any help/suggestions would be much appreciated.
推荐答案
在CanvasPainter中添加一些类型,并使用任何新的错误消息更新问题。
Add some types in CanvasPainter and update the question with any new error message.
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
....
class CanvasPainter extends CustomPainter {
List<ui.Image> items = new List<ui.Image>();
void addItems(ui.Image item) {
items.add(item);
}
@override
void paint(Canvas canvas, Size size) {
for (ui.Image item in items) {
canvas.drawImage(item, new Offset(50.0, 50.0), new Paint());
}
}
}
这篇关于Flutter:将图像添加到画布,编译时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!