问题描述
我正在寻找从Flutter应用程序中生成图像(jpeg或png)的路径。图像将由圆圈,线条,文本等组成。
似乎确实存在一种使用画布在屏幕上绘画的方法(),但是在创建可以在应用程序内部显示或发送/使用的图像方面似乎并不等效。
是否有飞镖可用于绘制图像的库?考虑到潜在的skia框架,这似乎是可能的。在Dart-html程序包中有一个CanvasRenderingContext2D。
编辑:开始以下工作(按照Richard的建议):
import'package:flutter / material.dart';
import‘package:path_provider / path_provider.dart’;
导入 dart:ui;
导入 dart:typed_data;
导入 dart:async;
导入 dart:io;
void main()=> runApp(new MyApp());
类MyApp扩展了StatelessWidget {
//此小部件是应用程序的根。
@override
小部件构建(BuildContext上下文){
返回新的MaterialApp(
标题: Flutter Demo,
主题:新的ThemeData(
primarySwatch :Colors.blue,
),
home:new MyHomePage(title:'Flutter Demo Home Page'),
);
}
}
类MyHomePage扩展了StatefulWidget {
MyHomePage({Key key,this.title}):super(key:key);
最终字符串标题;
@override
_MyHomePageState createState()=>新的_MyHomePageState();
}
类_MyHomePageState扩展State< MyHomePage> {
Image _image;
@override
void initState(){
super.initState();
_image =新的Image.network(
’https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png’,
);
}
Future< String> get _localPath async {
最终目录=
等待getApplicationDocumentsDirectory(); //从path_provider包
返回directory.path;
}
Future< File>得到_localFile异步{
最终路径=等待_localPath;
返回新文件( $ path / tempImage.png);
}
Future< File> writeImage(ByteData pngBytes)异步{
最终文件=等待_localFile;
//写入文件
file.writeAsBytes(pngBytes.buffer.asUint8List());
返回文件;
}
_generateImage(){
_generate()。then((val)=> setState((){
_image = val;
}));
}
Future< Image> _generate()异步{
PictureRecorder记录器= new PictureRecorder();
Canvas c = new Canvas(recorder);
var rect = new Rect.fromLTWH(0.0,0.0,100.0,100.0);
c.clipRect(rect);
最终绘画= new Paint();
paint.strokeWidth = 2.0;
paint.color = const Color(0xFF333333);
paint.style = PaintingStyle.fill;
最终偏移量=新偏移量(50.0,50.0);
c.drawCircle(offset,40.0,paint);
var picture = recorder.endRecording();
最终pngBytes =
等待picture.toImage(100,100).toByteData(format:ImageByteFormat.png);
//目标#1。用生成的图像更新_image。
var image = Image.memory(pngBytes.buffer.asUint8List());
返回图片;
//新Image.memory(pngBytes.buffer.asUint8List());
// _image = new Image.network(
//'https://github.com/flutter/website/blob/master/_includes/code/layout/lakes/images/lake.jpg吗? raw = true',
//);
//目标#2。将映像写入文件系统。
// writeImage(pngBytes);
//制作一个临时文件(请参见SO上的其他内容)并writeAsBytes(pngBytes.buffer.asUInt8List())
}
@override
小部件build(BuildContext context){
return new Scaffold(
appBar:new AppBar(
title:new Text(widget.title),
),
body:new Center(
子级:new Column(
mainAxisAlignment:MainAxisAlignment.center,
子级:< Widget> [
_image,
],
),
),
floatActionButton:新的FloatingActionButton(
onPressed:_generateImage,
工具提示:'Generate',
子级:new Icon(Icons.add),
),
);
}
}
PictureRecorder
可让您创建Canvas,使用Canvas绘制方法并提供 endRecording()
返回图片
。您可以将此图片绘制到其他场景或画布上,或使用 .toImage(width,height).toByteData(format)
将其转换为PNG(或原始格式-jpeg是'
例如:
import'dart: ui';
导入 dart:typed_data;
....
PictureRecorder记录器= new PictureRecorder();
Canvas c = new Canvas(recorder);
c.drawPaint(paint); //等
图片p = records.endRecording();
ByteData pngBytes =
等待p.toImage(100,100).toByteData(format:ImageByteFormat.png);
请确保您的拍打水平为0.4.4,否则您可能没有 format
参数可用。
虽然看到了您的修改,但我怀疑您确实在寻找 CustomPainter
,其中的小部件会为您提供可以在其上绘制的画布。这是一个来自类似问题的。
I'm looking to find a path to generating an image (jpeg or png) from within a flutter application. The image would be composed of circles, lines, text etc.
There does appear to be a means of drawing to the screen using a canvas (https://docs.flutter.io/flutter/dart-ui/Canvas/Canvas.html), however there doesn't appear to be the equivalent for creating an image that could be presented within or sent/used outside the application.
Is there any dart library available for drawing an image? It would seem that it possible given the underlying skia framework. In the Dart-html package there is a CanvasRenderingContext2D.
Edit: Getting something like the following working (as per Richard's suggestions) would be a start:
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:ui';
import 'dart:typed_data';
import 'dart:async';
import 'dart:io';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Image _image;
@override
void initState() {
super.initState();
_image = new Image.network(
'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png',
);
}
Future<String> get _localPath async {
final directory =
await getApplicationDocumentsDirectory(); //From path_provider package
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return new File('$path/tempImage.png');
}
Future<File> writeImage(ByteData pngBytes) async {
final file = await _localFile;
// Write the file
file.writeAsBytes(pngBytes.buffer.asUint8List());
return file;
}
_generateImage() {
_generate().then((val) => setState(() {
_image = val;
}));
}
Future<Image> _generate() async {
PictureRecorder recorder = new PictureRecorder();
Canvas c = new Canvas(recorder);
var rect = new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0);
c.clipRect(rect);
final paint = new Paint();
paint.strokeWidth = 2.0;
paint.color = const Color(0xFF333333);
paint.style = PaintingStyle.fill;
final offset = new Offset(50.0, 50.0);
c.drawCircle(offset, 40.0, paint);
var picture = recorder.endRecording();
final pngBytes =
await picture.toImage(100, 100).toByteData(format: ImageByteFormat.png);
//Aim #1. Upade _image with generated image.
var image = Image.memory(pngBytes.buffer.asUint8List());
return image;
//new Image.memory(pngBytes.buffer.asUint8List());
// _image = new Image.network(
// 'https://github.com/flutter/website/blob/master/_includes/code/layout/lakes/images/lake.jpg?raw=true',
// );
//Aim #2. Write image to file system.
//writeImage(pngBytes);
//Make a temporary file (see elsewhere on SO) and writeAsBytes(pngBytes.buffer.asUInt8List())
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_image,
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _generateImage,
tooltip: 'Generate',
child: new Icon(Icons.add),
),
);
}
}
PictureRecorder
lets you create a Canvas, use the Canvas drawing methods and provides endRecording()
returning a Picture
. You can draw this Picture to other Scenes or Canvases, or use .toImage(width, height).toByteData(format)
to convert it to PNG (or raw - jpeg isn't supported).
For example:
import 'dart:ui';
import 'dart:typed_data';
....
PictureRecorder recorder = new PictureRecorder();
Canvas c = new Canvas(recorder);
c.drawPaint(paint); // etc
Picture p = recorder.endRecording();
ByteData pngBytes =
await p.toImage(100, 100).toByteData(format: ImageByteFormat.png);
Make sure that you are on flutter 0.4.4, otherwise you may not have the format
parameter available.
Having seen your edit, though, I suspect you are really looking for CustomPainter
where a Widget gives you a Canvas on which you can draw. Here's an example from a similar question.
这篇关于可以用Dart / Flutter绘制图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!