afriscic / BarcodeScanning.Native.Maui

Barcode scanning library for .NET MAUI
https://www.nuget.org/packages/BarcodeScanning.Native.Maui
MIT License
162 stars 31 forks source link

What is expected inside a byte[] array for iOS Methods.ScanFromImage? #25

Closed HavenDV closed 6 months ago

HavenDV commented 7 months ago

Hi. Thanks a lot for your library, it looks like the best thing that can be used for scanning barcodes in MAUI among Open-Source projects.

To make it possible to quickly debug the code that is used after scanning within the emulator, I noticed the Methods.ScanFromImage method and tried to use it. I tried passing PNG and Jpeg bytes, but it wasn't recognized. I came across this and it looks like a similar problem: https://stackoverflow.com/questions/58270939/how-to-create-image-programmatically-from-byte-array-in-ios

Perhaps it's worth adding explicit xml documentation for this or switching to CGImage.FromPNG? https://learn.microsoft.com/en-us/dotnet/api/coregraphics.cgimage.frompng?view=xamarin-ios-sdk-12

HavenDV commented 7 months ago

Also a quick question - what is your opinion on the use of dev side dependencies and source generators in general? Will you accept the PR regarding moving to DependencyPropertyGenerator for this file? https://github.com/afriscic/BarcodeScanning.Native.Maui/blob/master/BarcodeScanning.Native.Maui/CameraView.cs

It will look like:

[DependencyProperty<bool>("VibrationOnDetected", DefaultBindingMode = DefaultBindingMode.TwoWay,
    Description = "Disables or enables vibration on barcode detection.")
public partial class CameraView : View
{

instead

public partial class CameraView : View
{
    public static readonly BindableProperty VibrationOnDetectedProperty = BindableProperty.Create(nameof(VibrationOnDetected)
        , typeof(bool)
        , typeof(CameraView)
        , true
        , defaultBindingMode: BindingMode.TwoWay
        , propertyChanged: (bindable, value, newValue) => ((CameraView)bindable).VibrationOnDetected = (bool)newValue);
    /// <summary>
    /// Disables or enables vibration on barcode detection.
    /// </summary>
    public bool VibrationOnDetected
    {
        get => (bool)GetValue(VibrationOnDetectedProperty);
        set => SetValue(VibrationOnDetectedProperty, value);
    }

and will also allow you to use WeakEvent for the OnDetectionFinished event (there is even more boilerboard code to implement)

afriscic commented 7 months ago

Hello.

  1. TBH I really don't remember when did I last tested this methods. As per documentation bot Android and iOS methods should recognise the type of image data that is in the byte array and load it accordingly. But now that you mention this, I'll add an override to load image data directly from image file in next release. I think it will be both more convenient and more reliable.
  2. I'm in for reducing boilerplate. If you want you can do a PR and I'll look into it.
HavenDV commented 7 months ago

PR is ready.

Other ideas for which I can provide PR:

HavenDV commented 7 months ago

But now that you mention this, I'll add an override to load image data directly from image file in next release. I think it will be both more convenient and more reliable.

I'm primarily interested in input data as byte[] or as Stream. To test recognition on iOS/Android in the absence of a camera, I generate a QRCode PNG image in the form of byte[]/Stream and pass it to this method. But this doesn't work for iOS right now. There is a chance that the image is too large to be recognized (the QR code takes up most of the space with some padding around the edges)

afriscic commented 6 months ago

Implemented multiple overloads in 1.2.5. See if any fits your use case.