Created
September 9, 2019 18:42
-
-
Save luigi-rosso/6afb2e2bf3b494f1bc19398ca1ccb5d9 to your computer and use it in GitHub Desktop.
Basic flutter to image of canvas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:typed_data'; | |
import 'package:flutter/material.dart'; | |
import 'dart:ui' as ui; | |
void main() async { | |
const double width = 800; | |
const double height = 600; | |
final ui.PictureRecorder recorder = new ui.PictureRecorder(); | |
final ui.Canvas canvas = | |
new ui.Canvas(recorder, Rect.fromLTWH(0, 0, width, height)); | |
canvas.drawRect( | |
Rect.fromLTWH(0, 0, width, height), | |
Paint() | |
..style = PaintingStyle.fill | |
..color = ui.Color.fromRGBO(128, 0, 0, 1.0) | |
..isAntiAlias = false); | |
final ui.Picture picture = recorder.endRecording(); | |
ui.Image image = await picture.toImage(width.floor(), height.floor()); | |
ByteData data = await image.toByteData(); | |
// Should be 128 as specified in our Paint fill. | |
print("First pixel's R: ${data.getUint8(0)}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment