qr_image_exporter

pub package

An extension for the popular qr.dart package, providing a simple, robust way to export QR code modules as PNG byte data.


Features

This package adds the toPngBytes() method as an extension on the QrImage class from package:qr.

  • Simple Export: Easily generate a Uint8List (PNG byte data) from any QrImage instance.
  • Highly Configurable: Customize the output image with parameters for:
    • Module Size: Set the pixel size of each QR code dot.
    • Margin: Define the border size around the QR code.
    • Colors: Specify custom ARGB colors for the dark and light modules.

Getting Started

Installation

Add qr_image_exporter to your project's pubspec.yaml file:

dependencies:
  qr_image_exporter: ^1.0.0 # Use the latest version

Usage

Import the qr_image_exporter package. The necessary classes (QrCode, QrImage) are automatically exposed, allowing you to use the toPngBytes() extension method immediately.

import 'dart:io';
import 'dart:typed_data';
import 'package:qr_image_exporter/qr_image_exporter.dart';

void main() {
  // 1. Generate the core QrImage data
  final QrCode qrCode = QrCode.fromData(
    data: 'https://pub.dev/packages/qr_image_exporter',
    errorCorrectLevel: QrErrorCorrectLevel.M,
  );
  final QrImage qrImage = QrImage(qrCode);

  // 2. Use the 'toPngBytes' extension method to export
  final Uint8List? pngBytes = qrImage.toPngBytes(
    // Optional: Customize the image output
    moduleSize: 8, // Larger dots (higher resolution)
    margin: 30, // Larger border
    darkColor: 0xFF0057B8, // Example: Blue
    lightColor: 0xFFF0DA49, // Example: Yellow
  );

  if (pngBytes != null) {
    // Example: Save the byte data to a file
    File('example_qr_code.png').writeAsBytesSync(pngBytes);
    print('QR code successfully exported!');
  }
}

Example Output

Here is the QR code generated by the example above (using Blue/Yellow custom colors):

Custom QR Code Example

API Reference

QrImageExporter Extension

The extension provides one main method:

toPngBytes

Uint8List? toPngBytes({
  int moduleSize = 4,
  int margin = 20,
  int darkColor = 0xFF000000,
  int lightColor = 0xFFFFFFFF,
})
Parameter Type Default Description
moduleSize int 4 The pixel size of each QR module (dot). Larger value = higher resolution.
margin int 20 The pixel border added around the QR code.
darkColor int 0xFF000000 The ARGB color of the dark modules (e.g., black).
lightColor int 0xFFFFFFFF The ARGB color of the light modules/background (e.g., white).

Returns: A Uint8List containing the PNG image data, or null if encoding fails.

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request if you have suggestions or bug fixes.

This package was created to address the long-standing image export request from the original qr.dart package (Issue #62).

Libraries

qr_image_exporter
An extension library for 'package:qr' to export QR code data as highly configurable PNG byte data (Uint8List).