Trying to read a barcode through an image upload

So, I'm using Xamarin.Forms Zxing barcode scanner, and I know that my "luminanceSource" variable has a value, but once it gets to the decoding step, "result" variable always returns null.

   var assembly = this.GetType().GetTypeInfo().Assembly;
                byte[] imageBuffer;
                using (var s = assembly.GetManifestResourceStream("NapaShowtime.Assets.qr-code.png")) {
                    var length = s.Length;
                    imageBuffer = new byte[length];
                    s.Read(imageBuffer, 0, (int)length);

                    var reader = new BarcodeReader();
                    var luminanceSource = new RGBLuminanceSource(imageBuffer, 10, 10);
                    var result = reader.Decode(luminanceSource);
                    if (result != null)
                        _viewModel.ExecuteScanResultCommand(result);
                }

Any help would be very much appreciated!

Best Answer

  • a_solo25a_solo25 ✭✭
    Accepted Answer

    I did attempt that but for some reason it still made no difference, my result would still be null. Instead I did this and it worked just fine!

     var resultPoints = new ResultPoint[4];
                        var bytesArray = new byte[]{ 113, 164, 33, 104, 116, 116, 112, 58, 47, 47,
                            77, 105, 99, 104, 97, 101, 108, 95, 109, 111, 114, 114, 105, 115, 111, 110, 64,
                            103, 101, 110, 112, 116, 46, 99, 111, 109, 0, 236, 17, 236, 17, 236, 17, 236, 17,
                            236, 17, 236, 17, 236, 17, 236, 17, 236, 17 };
                        var result = new Result("text", bytesArray, resultPoints, BarcodeFormat.QR_CODE);
    
                        if (result != null)
                            _viewModel.ExecuteScanBarcodeCommand(result);
    

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to change the type from RGBLuminanceSource to Bitmap.

    var luminanceSource = new RGBLuminanceSource(imageBuffer, 10, 10);
    HybridBinarizer hb = new HybridBinarizer(luminanceSource);
    var a = hb.createBinarizer(source);
    BinaryBitmap bBitmap = new BinaryBitmap(a);
    

    Refer to:
    https://www.programcreek.com/java-api-examples/index.php?api=com.google.zxing.RGBLuminanceSource

  • a_solo25a_solo25 Member ✭✭
    Accepted Answer

    I did attempt that but for some reason it still made no difference, my result would still be null. Instead I did this and it worked just fine!

     var resultPoints = new ResultPoint[4];
                        var bytesArray = new byte[]{ 113, 164, 33, 104, 116, 116, 112, 58, 47, 47,
                            77, 105, 99, 104, 97, 101, 108, 95, 109, 111, 114, 114, 105, 115, 111, 110, 64,
                            103, 101, 110, 112, 116, 46, 99, 111, 109, 0, 236, 17, 236, 17, 236, 17, 236, 17,
                            236, 17, 236, 17, 236, 17, 236, 17, 236, 17 };
                        var result = new Result("text", bytesArray, resultPoints, BarcodeFormat.QR_CODE);
    
                        if (result != null)
                            _viewModel.ExecuteScanBarcodeCommand(result);
    
Sign In or Register to comment.