public void OnPreviewFrame(byte [] bytes, Android.Hardware.Camera camera) { Console.WriteLine("PREVIEW FRAME"); if ((DateTime.Now - lastPreviewAnalysis).TotalMilliseconds < 250) { return; } try { byte[] rotatedData = new byte[bytes.Length]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { rotatedData[x * height + height - y - 1] = bytes[x + y * width]; } } var dataRect = GetFramingRectInPreview(); var luminance = new PlanarYUVLuminanceSource(rotatedData, width, height, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false); var binarized = new BinaryBitmap(new ZXing.Common.HybridBinarizer(luminance)); var result = reader.decodeWithState(binarized); lastPreviewAnalysis = DateTime.Now; if (result == null || string.IsNullOrEmpty(result.Text)) { return; } Android.Util.Log.Debug("ZXing.Mobile", "Barcode Found: " + result.Text); ShutdownCamera(); activity.OnScan(result); } catch (ReaderException) { Android.Util.Log.Debug("ZXing.Mobile", "No barcode Found"); // ignore this exception; it happens every time there is a failed scan } catch (Exception) { // TODO: this one is unexpected.. log or otherwise handle it throw; } }
public void OnPreviewFrame(byte [] bytes, Android.Hardware.Camera camera) { if ((DateTime.Now - lastPreviewAnalysis).TotalMilliseconds < options.DelayBetweenAnalyzingFrames) { return; } try { var cameraParameters = camera.GetParameters(); var img = new YuvImage(bytes, ImageFormatType.Nv21, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, null); if (barcodeReader == null) { barcodeReader = new BarcodeReader(null, null, null, (p, w, h, f) => new PlanarYUVLuminanceSource(p, w, h, 0, 0, w, h, false)); //new PlanarYUVLuminanceSource(p, w, h, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false)) if (this.options.TryHarder.HasValue) { barcodeReader.TryHarder = this.options.TryHarder.Value; } if (this.options.PureBarcode.HasValue) { barcodeReader.PureBarcode = this.options.PureBarcode.Value; } if (!string.IsNullOrEmpty(this.options.CharacterSet)) { barcodeReader.CharacterSet = this.options.CharacterSet; } if (this.options.TryInverted.HasValue) { barcodeReader.TryInverted = this.options.TryInverted.Value; } if (this.options.PossibleFormats != null && this.options.PossibleFormats.Count > 0) { barcodeReader.PossibleFormats = new List <BarcodeFormat>(); foreach (var pf in this.options.PossibleFormats) { barcodeReader.PossibleFormats.Add(pf); } } //Always autorotate on android barcodeReader.AutoRotate = true; } //Try and decode the result var result = barcodeReader.Decode(img.GetYuvData(), img.Width, img.Height, RGBLuminanceSource.BitmapFormat.Unknown); lastPreviewAnalysis = DateTime.Now; if (result == null || string.IsNullOrEmpty(result.Text)) { return; } Android.Util.Log.Debug("ZXing.Mobile", "Barcode Found: " + result.Text); ShutdownCamera(); activity.OnScan(result); } catch (ReaderException) { Android.Util.Log.Debug("ZXing.Mobile", "No barcode Found"); // ignore this exception; it happens every time there is a failed scan } catch (Exception) { // TODO: this one is unexpected.. log or otherwise handle it throw; } }
public void OnPreviewFrame(byte [] bytes, Android.Hardware.Camera camera) { if ((DateTime.Now - lastPreviewAnalysis).TotalMilliseconds < options.DelayBetweenAnalyzingFrames) { return; } try { /* OLD Android Code * //Fix for image not rotating on devices * byte[] rotatedData = new byte[bytes.Length]; * for (int y = 0; y < height; y++) { * for (int x = 0; x < width; x++) * rotatedData[x * height + height - y - 1] = bytes[x + y * width]; * } * * var cameraParameters = camera.GetParameters(); * * //Changed to using a YUV Image to get the byte data instead of manually working with it! * var img = new YuvImage(rotatedData, ImageFormatType.Nv21, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, null); * var dataRect = GetFramingRectInPreview(); * * var luminance = new PlanarYUVLuminanceSource (img.GetYuvData(), width, height, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false); * //var luminance = new PlanarYUVLuminanceSource(img.GetYuvData(), cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, 0, 0, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, false); * var binarized = new BinaryBitmap (new ZXing.Common.HybridBinarizer(luminance)); * var result = reader.decodeWithState(binarized); */ var cameraParameters = camera.GetParameters(); var img = new YuvImage(bytes, ImageFormatType.Nv21, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, null); var dataRect = GetFramingRectInPreview(); //var barcodeReader = new BarcodeReader(null, p => new PlanarYUVLuminanceSource(img.GetYuvData(), img.Width, img.Height, dataRect.Left, dataRect.Top, // dataRect.Width(), dataRect.Height(), false), null, null) //{ // AutoRotate = true, // TryHarder = true, //}; var barcodeReader = new BarcodeReader(null, null, null, (p, w, h, f) => new PlanarYUVLuminanceSource(p, w, h, 0, 0, w, h, false)) //new PlanarYUVLuminanceSource(p, w, h, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false)) { AutoRotate = true, TryHarder = false }; if (this.options.PureBarcode.HasValue && this.options.PureBarcode.Value) { barcodeReader.PureBarcode = this.options.PureBarcode.Value; } if (this.options.PossibleFormats != null && this.options.PossibleFormats.Count > 0) { barcodeReader.PossibleFormats = this.options.PossibleFormats; } var result = barcodeReader.Decode(img.GetYuvData(), img.Width, img.Height, RGBLuminanceSource.BitmapFormat.Unknown); lastPreviewAnalysis = DateTime.Now; if (result == null || string.IsNullOrEmpty(result.Text)) { return; } Android.Util.Log.Debug("ZXing.Mobile", "Barcode Found: " + result.Text); ShutdownCamera(); activity.OnScan(result); } catch (ReaderException) { Android.Util.Log.Debug("ZXing.Mobile", "No barcode Found"); // ignore this exception; it happens every time there is a failed scan } catch (Exception) { // TODO: this one is unexpected.. log or otherwise handle it throw; } }