Redth / ZXing.Net.Mobile

Barcode Scanner for Xamarin.iOS, Xamarin.Android, UWP and Tizen
MIT License
1.07k stars 703 forks source link

Scanning of specific formats not working in Xamarin.Forms Xaml #1026

Closed g0dpain closed 2 years ago

g0dpain commented 2 years ago

Hello I am currently working on a few template projects for my work and wanted to implement a Xaml MVVM solution. I worked a lot with this library but never did CustomOverlay/Xamarin.Forms with it, only with MobileBarcodeReader-class.

I happened to notice that in Forms xaml possible formats aren't working as they should be, by trying binding them through a viewmodel and also adding them after creating viewmodel.

PossibleFormats is just (BarcodeFormat.EAN_13 & BarcodeFormat.UPC_EAN_EXTENSION) and it still reads something like qr_codes (basically everything)

Versions tried: 2.4.1 - current 3.1

On request I can provide link to repository and images to scan, since I don't know if I am doing something wrong

Edit System tested: Android 8.1 (API 27)

Adrianotiger commented 2 years ago

I noticed the same problem on my app. But it was because I was trying to add (append) the other formats once I created the enumeration list.

With this code, it works:

  var formats = new List<ZXing.BarcodeFormat>();
  formats.Add(ZXing.BarcodeFormat.QR_CODE);

  if (Parameters.Options.AcceptBarcode_Code)
  {
      formats.Add(ZXing.BarcodeFormat.CODE_39);
      formats.Add(ZXing.BarcodeFormat.CODE_93);
      formats.Add(ZXing.BarcodeFormat.CODE_128);
      formats.Add(ZXing.BarcodeFormat.CODABAR);
  }
  if (Parameters.Options.AcceptBarcode_Ean)
  {
      formats.Add(ZXing.BarcodeFormat.EAN_8);
      formats.Add(ZXing.BarcodeFormat.EAN_13);
  }
  if (Parameters.Options.AcceptBarcode_Upc)
  {
      formats.Add(ZXing.BarcodeFormat.UPC_A);
      formats.Add(ZXing.BarcodeFormat.UPC_E);
      formats.Add(ZXing.BarcodeFormat.UPC_EAN_EXTENSION);
  }

  options.PossibleFormats = new List<ZXing.BarcodeFormat>(formats);

Hope this can help others too.

g0dpain commented 2 years ago

@Adrianotiger it seemed so, thanks :)