static void Main(string[] args) { var path = Path.Combine(Environment.CurrentDirectory, "Images"); string[] images = new string[] { $"{path}/0xcf7d4b75b7bccdb8b4f992fe05970680e2ee1a02.png", $"{path}/0x396f0e55fa33513441d556f84a6ea5c6fd7d217b.png", $"{path}/bad.png", $"{path}/联系方式.png", }; int i = 1; foreach (var imagePath in images) { var reader = new BarcodeReaderGeneric(); Bitmap image = (Bitmap)Image.FromFile(imagePath); using (image) { LuminanceSource source; source = new ZXing.Windows.Compatibility.BitmapLuminanceSource(image); //decode text from LuminanceSource Result result = reader.Decode(source); var message = result == null ? "读取二维码失败" : result.Text; Console.WriteLine($"{i++.ToString()}: {message}"); } } }
void AttemptDecode() { // get image data int width = activeCameraTexture.width; int height = activeCameraTexture.height; byte[] rawRGB = new byte[width * height * 3]; int i = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { UnityEngine.Color color = activeCameraTexture.GetPixel(x, y); rawRGB[i++] = (byte)(color.r * 255.0); rawRGB[i++] = (byte)(color.g * 255.0); rawRGB[i++] = (byte)(color.b * 255.0); } } // create a barcode reader instance ///QRCodeReader reader = new QRCodeReader(); BarcodeReaderGeneric reader = new BarcodeReaderGeneric(); //var barcodeBitmap = (Bitmap)Image.LoadFrom("C:\\sample-barcode-image.png"); // detect and decode the barcode inside the bitmap var result = reader.Decode(rawRGB, width, height, RGBLuminanceSource.BitmapFormat.RGB24); // do something with the result if (result != null) { Decoded(result); } }
private static string ExtractUrlFromQRImage(string qrCodeSetupImageUrl) { var headerLength = "data:image/png;base64,".Length; var rawImageData = qrCodeSetupImageUrl.Substring(headerLength, qrCodeSetupImageUrl.Length - headerLength); var imageData = Convert.FromBase64String(rawImageData); //var reader = new BarcodeReaderGeneric(); //reader.Options.PossibleFormats = new List<BarcodeFormat> { // BarcodeFormat.QR_CODE //}; #if NETFRAMEWORK var reader = new BarcodeReader(); reader.Options.PossibleFormats = new List <BarcodeFormat> { BarcodeFormat.QR_CODE }; using (var ms = new MemoryStream(imageData)) { var image = new System.Drawing.Bitmap(ms); return(reader.Decode(image).Text); } #else var reader = new BarcodeReaderGeneric(); reader.Options.PossibleFormats = new List <BarcodeFormat> { BarcodeFormat.QR_CODE }; var image = new ImageMagick.MagickImage(imageData); var wrappedImage = new ZXing.Magick.MagickImageLuminanceSource(image); return(reader.Decode(wrappedImage).Text); #endif }
static void Main(string[] args) { // // create a barcode reader instance // BarcodeReader reader = new BarcodeReader(); // reader.Options.CharacterSet = "UTF-8"; // // load a bitmap // //var barcodeBitmap = (Bitmap)Image.FromFile("2.png"); // Bitmap barcodeBitmap = new Bitmap("6.png"); //// Bitmap bmp = MakeGrayscale3(barcodeBitmap); // // detect and decode the barcode inside the bitmap // LuminanceSource source = new BitmapLuminanceSource(barcodeBitmap); // BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); // IDictionary<DecodeHintType, object> hints = new Dictionary<DecodeHintType, object>(); // hints.Add(DecodeHintType.CHARACTER_SET, "UTF-8"); // hints.Add(DecodeHintType.TRY_HARDER, "3"); // Result[] results = reader.DecodeMultiple(source); // // do something with the result // if (results != null) // { // // txtDecoderType.Text = result.BarcodeFormat.ToString(); // // txtDecoderContent.Text = result.Text; // //Console.WriteLine(result.BarcodeFormat.ToString()); // //Console.WriteLine(result.Text); // foreach (Result res in results ) // { // Console.WriteLine(res.Text); // } // } MultiFormatReader mReader = new MultiFormatReader(); BarcodeReaderGeneric <MultiFormatReader> gmReader = new BarcodeReaderGeneric <MultiFormatReader>(); Bitmap barcodeBitmap = new Bitmap("6.png"); LuminanceSource source = new BitmapLuminanceSource(barcodeBitmap); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); IDictionary <DecodeHintType, object> hints = new Dictionary <DecodeHintType, object>(); hints.Add(DecodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(DecodeHintType.TRY_HARDER, "3"); Result results = mReader.decode(binaryBitmap, hints); Console.ReadKey(); }
private static string ExtractUrlFromQRImage(string qrCodeSetupImageUrl) { var headerLength = "data:image/png;base64,".Length; var rawImageData = qrCodeSetupImageUrl.Substring(headerLength, qrCodeSetupImageUrl.Length - headerLength); var imageData = Convert.FromBase64String(rawImageData); var reader = new BarcodeReaderGeneric(); reader.Options.PossibleFormats = new List <BarcodeFormat> { BarcodeFormat.QR_CODE }; var image = new ImageMagick.MagickImage(imageData); var wrappedImage = new ZXing.Magick.MagickImageLuminanceSource(image); return(reader.Decode(wrappedImage).Text); }
public BarcodeReaderGeneric BuildBarcodeReader() { var reader = new BarcodeReaderGeneric(); if (TryHarder.HasValue) { reader.Options.TryHarder = TryHarder.Value; } if (PureBarcode.HasValue) { reader.Options.PureBarcode = PureBarcode.Value; } if (AutoRotate.HasValue) { reader.AutoRotate = AutoRotate.Value; } if (UseCode39ExtendedMode.HasValue) { reader.Options.UseCode39ExtendedMode = UseCode39ExtendedMode.Value; } if (!string.IsNullOrEmpty(CharacterSet)) { reader.Options.CharacterSet = CharacterSet; } if (TryInverted.HasValue) { reader.TryInverted = TryInverted.Value; } if (AssumeGS1.HasValue) { reader.Options.AssumeGS1 = AssumeGS1.Value; } if (PossibleFormats?.Any() ?? false) { reader.Options.PossibleFormats = new List <BarcodeFormat>(); foreach (var pf in PossibleFormats) { reader.Options.PossibleFormats.Add(pf); } } return(reader); }
private void InitBarcodeReaderIfNeeded() { if (_barcodeReader != null) { return; } _barcodeReader = new BarcodeReaderGeneric <FastJavaByteArrayYUVLuminanceSource>(); if (_scanningOptions.TryHarder.HasValue) { _barcodeReader.Options.TryHarder = _scanningOptions.TryHarder.Value; } if (_scanningOptions.PureBarcode.HasValue) { _barcodeReader.Options.PureBarcode = _scanningOptions.PureBarcode.Value; } if (!string.IsNullOrEmpty(_scanningOptions.CharacterSet)) { _barcodeReader.Options.CharacterSet = _scanningOptions.CharacterSet; } if (_scanningOptions.TryInverted.HasValue) { _barcodeReader.TryInverted = _scanningOptions.TryInverted.Value; } if (_scanningOptions.UseCode39ExtendedMode.HasValue) { _barcodeReader.Options.UseCode39ExtendedMode = _scanningOptions.UseCode39ExtendedMode.Value; } if (_scanningOptions.PossibleFormats != null && _scanningOptions.PossibleFormats.Count > 0) { _barcodeReader.Options.PossibleFormats = new List <BarcodeFormat>(); foreach (var pf in _scanningOptions.PossibleFormats) { _barcodeReader.Options.PossibleFormats.Add(pf); } } }
public void SetupCamera() { cameraEventListener.OnPreviewFrameReady += HandleOnPreviewFrameReady; cameraController.SetupCamera(); barcodeReader = scannerHost.ScanningOptions.BuildBarcodeReader(); }