Skip to content

Comments

Add API to save generated barcode images#270

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/add-api-save-barcode-images
Draft

Add API to save generated barcode images#270
Copilot wants to merge 6 commits intomainfrom
copilot/add-api-save-barcode-images

Conversation

Copy link
Contributor

Copilot AI commented Oct 20, 2025

Fixes #96, fixes #80

This PR adds comprehensive functionality to generate and save barcode images programmatically, addressing user requests for saving barcodes created with BarcodeGeneratorView.

Overview

Users can now generate barcode images without requiring a view and save them to files or streams across all supported platforms (Android, iOS, MacCatalyst, and Windows).

Key Features

1. BarcodeGenerator Class

A new class for programmatic barcode generation without requiring a view:

var generator = new BarcodeGenerator
{
    Format = BarcodeFormat.QrCode,
    Width = 300,
    Height = 300,
    Margin = 10,
    ForegroundColor = Colors.Black,
    BackgroundColor = Colors.White,
    CharacterSet = "UTF-8"
};

var image = await generator.GenerateAsync("https://github.com/Redth/ZXing.Net.Maui");

2. Platform-Specific Save Extensions

Extension methods to save barcode images to files or streams:

// Save to file
await image.SaveAsync("/path/to/barcode.png");

// Save to stream with specific format
using var stream = new MemoryStream();
await image.SaveAsync(stream, BarcodeImageFormat.Jpeg);

Supported formats by platform:

  • Android: PNG, JPEG, WebP
  • iOS/MacCatalyst: PNG, JPEG
  • Windows: PNG, JPEG, BMP, GIF, TIFF

3. BarcodeGeneratorView Integration

Added GenerateBarcodeAsync() method to existing BarcodeGeneratorView controls:

// In XAML
<zxing:BarcodeGeneratorView 
    x:Name="barcodeView"
    HeightRequest="300"
    WidthRequest="300"
    Value="https://dotnet.microsoft.com"
    Format="QrCode" />

// In code-behind
var barcode = await barcodeView.GenerateBarcodeAsync();
await barcode.SaveAsync(filePath);

Documentation

The README has been updated with comprehensive documentation including:

  • Complete API usage examples
  • Platform-specific permission requirements:
    • Android: WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE (API ≤32), READ_MEDIA_IMAGES (API ≥33)
    • iOS: NSPhotoLibraryAddUsageDescription (for photo library access)
    • Windows: picturesLibrary, documentsLibrary capabilities (optional)
  • Recommendations for using app-specific directories (no permissions needed)
  • Complete working code examples

Sample App

The BigIslandBarcode sample app now includes a save button (💾) that demonstrates the new functionality by saving the displayed barcode to the app's data directory.

Implementation Notes

  • All methods follow async/await patterns for better responsiveness
  • Uses native platform APIs for optimal performance
  • Proper error handling with null checks and exceptions
  • Uses WidthRequest/HeightRequest instead of Width/Height to avoid issues with unmeasured views
  • Minimal changes to existing code - only adds new functionality
Original prompt

Please look at @Redth/ZXing.Net.Maui/issues/96 and @Redth/ZXing.Net.Maui/issues/80 and come up with an API to save generated barcode images. Take into account permissions for each platform that might be needed and make sure to update the README with the new information


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 5 commits October 20, 2025 13:26
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Copilot AI changed the title [WIP] Add API to save generated barcode images with permissions Add API to save generated barcode images Oct 20, 2025
Copilot AI requested a review from jfversluis October 20, 2025 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Question: How to Convert / Save BarcodeGeneratorView to image files How to Export the image when I generate the barcode

2 participants