Flutter Tutorial: Ngonversi Canvas menyang Gambar

Ing Flutter, sampeyan bisa ngowahi a Canvas kanggo gambar nggunakake toImage() cara saka ui.Image kelas. Kelas Canvas ngidini sampeyan nggambar grafis lan wujud ing widget khusus utawa sajrone tahap lukisan widget CustomPainter. Sawise sampeyan wis digambar kabeh ing canvas, sampeyan bisa ngowahi menyang gambar nggunakake toImage() cara.

Mangkene pandhuan langkah-langkah babagan carane ngowahi a Canvas dadi gambar ing Flutter:

Impor paket sing dibutuhake

import 'package:flutter/material.dart';  
import 'dart:ui' as ui;  

Nggawe widget khusus utawa ing CustomPainter ngendi sampeyan bakal nggambar ing canvas

class MyCanvasWidget extends StatelessWidget {  
  @override  
  Widget build(BuildContext context) {  
    return CustomPaint(  
      size: Size(200, 200), // Set the size of the canvas  
      painter: MyCanvasPainter(),  
   );  
  }  
}  
  
class MyCanvasPainter extends CustomPainter {  
  @override  
  void paint(Canvas canvas, Size size) {  
    // Draw on the canvas here  
    // For example, draw a red circle at the center  
    final Paint paint = Paint()..color = Colors.red;  
    final double centerX = size.width / 2;  
    final double centerY = size.height / 2;  
    final double radius = 50.0;  
    canvas.drawCircle(Offset(centerX, centerY), radius, paint);  
  }  
  
  @override  
  bool shouldRepaint(CustomPainter oldDelegate) {  
    return false;  
  }  
}  

Nggawe fungsi kanggo ngowahi canvas menyang gambar

Future<ui.Image> captureCanvasToImage() async {  
  final pictureRecorder = ui.PictureRecorder();  
  final canvas = Canvas(pictureRecorder);  
  
  // Create the custom widget or draw on the canvas using a CustomPainter  
  final widget = MyCanvasWidget();  
  widget.paint(canvas, Size(200, 200)); // Set the size of the canvas to match the custom widget size  
  
  final recordedPicture = pictureRecorder.endRecording();  
  return await recordedPicture.toImage(200, 200); // Set the image size, should match the canvas size  
}  

Telpon captureCanvasToImage() fungsi lan nangani gambar

void _convertCanvasToImage() async {  
  ui.Image image = await captureCanvasToImage();  
  // Use the image here as needed, such as displaying it in an `Image` widget or saving it to a file  
}  

Ing conto iki, kita nggawe widget khusus jenenge MyCanvasWidget, sing nggambar bunder abang ing tengah canvas. Fungsi kasebut captureCanvasToImage() nggawe Canvas, nggambar nganggo widget khusus utawa CustomPainter, banjur ngowahi dadi ui.Image.

Elinga yen canvas ukuran kudu disetel ing widget adat( MyCanvasWidget) lan toImage() cara kanggo mesthekake yen gambar lan gambar duwe dimensi sing bener. Ing conto iki, kita nyetel canvas ukuran kanggo 200x200, nanging sampeyan bisa nyetel ukuran sing dikarepake.

Elinga kanggo nangani kesalahan lan ngenteni operasi asinkron kanthi bener nalika nggarap fungsi Futures lan asinkron. Uga, priksa manawa nelpon _convertCanvasToImage() yen cocok kanggo njupuk canvas lan njupuk gambar.