public void testMultiQRCodes() { var path = buildTestBase("test/data/blackbox/multi-qrcode-1"); var source = new BitmapLuminanceSource((Bitmap)Bitmap.FromFile(Path.Combine(path, "1.png"))); var bitmap = new BinaryBitmap(new HybridBinarizer(source)); var reader = new QRCodeMultiReader(); var results = reader.decodeMultiple(bitmap); Assert.IsNotNull(results); Assert.AreEqual(4, results.Length); var barcodeContents = new HashSet <String>(); foreach (Result result in results) { barcodeContents.Add(result.Text); Assert.AreEqual(BarcodeFormat.QR_CODE, result.BarcodeFormat); var metadata = result.ResultMetadata; Assert.IsNotNull(metadata); } var expectedContents = new HashSet <String> { "You earned the class a 5 MINUTE DANCE PARTY!! Awesome! Way to go! Let's boogie!", "You earned the class 5 EXTRA MINUTES OF RECESS!! Fabulous!! Way to go!!", "You get to SIT AT MRS. SIGMON'S DESK FOR A DAY!! Awesome!! Way to go!! Guess I better clean up! :)", "You get to CREATE OUR JOURNAL PROMPT FOR THE DAY! Yay! Way to go! " }; foreach (var expected in expectedContents) { Assert.That(barcodeContents.Contains(expected), Is.True); } }
void createWorker() { //create worker thread for reading barcode readerWorker = new Task(async() => { //use stopwatch to time the execution, and execute the reading process repeatedly var watch = System.Diagnostics.Stopwatch.StartNew(); var reader = new QRCodeMultiReader(); SoftwareBitmap bitmap; HybridBinarizer binarizer; while (true) { try { lock (bufLock) { bitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, width, height); bitmap.CopyFromBuffer(decodedDataBuf.AsBuffer()); } } catch { //the size maybe incorrect due to unknown reason await Task.Delay(10); continue; } var source = new SoftwareBitmapLuminanceSource(bitmap); binarizer = new HybridBinarizer(source); var results = reader.decodeMultiple(new BinaryBitmap(binarizer)); if (results != null && results.Length > 0) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { foreach (var result in results) { if (!readed.Contains(result.Text)) { readed.Add(result.Text); Textbox.Text += result.Text + "\n"; } } }); } watch.Stop(); int elapsed = (int)watch.ElapsedMilliseconds; //run at max 5Hz await Task.Delay(Math.Max(0, 200 - elapsed)); } }); }
public void testProcessStructuredAppend() { var sa1 = new Result("SA1", new byte[] { }, new ResultPoint[] { }, BarcodeFormat.QR_CODE); var sa2 = new Result("SA2", new byte[] { }, new ResultPoint[] { }, BarcodeFormat.QR_CODE); var sa3 = new Result("SA3", new byte[] { }, new ResultPoint[] { }, BarcodeFormat.QR_CODE); sa1.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE, (0 << 4) + 2); sa1.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, "L"); sa2.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE, (1 << 4) + 2); sa2.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, "L"); sa3.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE, (2 << 4) + 2); sa3.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, "L"); var nsa = new Result("NotSA", new byte[] { }, new ResultPoint[] { }, BarcodeFormat.QR_CODE); nsa.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, "L"); var inputs = new List <Result>(); inputs.Add(sa3); inputs.Add(sa1); inputs.Add(nsa); inputs.Add(sa2); var results = QRCodeMultiReader.ProcessStructuredAppend(inputs); Assert.That(results, Is.Not.Null); Assert.That(results.Count, Is.EqualTo(2)); var barcodeContents = new HashSet <string>(); foreach (Result result in results) { barcodeContents.Add(result.Text); } var expectedContents = new HashSet <string>(); expectedContents.Add("NotSA"); expectedContents.Add("SA1SA2SA3"); Assert.That(barcodeContents, Is.EqualTo(expectedContents)); }
/// <summary> /// 从指定图片流中解析多个二维码/条形码 /// </summary> /// <param name="Stream">图片流</param> /// <param name="Encode">编码,默认utf-8</param> /// <returns>返回解析的结果,成功则返回解析结果集合,失败则返回空</returns> public static List <string> MultiRead(Bitmap bitmap, string Encode = "utf-8") { List <string> result = new List <string>(); QRCodeMultiReader qc = new QRCodeMultiReader(); LuminanceSource source = new BitmapLuminanceSource(bitmap); BinaryBitmap binarybitmap = new BinaryBitmap(new HybridBinarizer(source)); IDictionary <DecodeHintType, object> hints = new Dictionary <DecodeHintType, object>(); hints.Add(DecodeHintType.CHARACTER_SET, Encode); //尝试更多读取 hints.Add(DecodeHintType.TRY_HARDER, "3"); Result[] r = qc.decodeMultiple(binarybitmap, hints); if (r != null) { foreach (Result res in r) { result.Add(res.Text); } } return(result.Count > 0 ? result : null); }
/// <summary> /// Tries to decode barcodes within an image which is given by a luminance source. /// That method gives a chance to prepare a luminance source completely before calling /// the time consuming decoding method. On the other hand there is a chance to create /// a luminance source which is independent from external resources (like Bitmap objects) /// and the decoding call can be made in a background thread. /// </summary> /// <param name="luminanceSource">The luminance source.</param> /// <returns></returns> public virtual Result[] DecodeMultiple(LuminanceSource luminanceSource) { var results = default(Result[]); var binarizer = CreateBinarizer(luminanceSource); var binaryBitmap = new BinaryBitmap(binarizer); var rotationCount = 0; var rotationMaxCount = 1; MultipleBarcodeReader multiReader = null; if (AutoRotate) { Options.Hints[DecodeHintType.TRY_HARDER_WITHOUT_ROTATION] = true; rotationMaxCount = 4; } var formats = Options.PossibleFormats; if (formats != null && formats.Count == 1 && formats.Contains(BarcodeFormat.QR_CODE)) { multiReader = new QRCodeMultiReader(); } else { multiReader = new GenericMultipleBarcodeReader(Reader); } for (; rotationCount < rotationMaxCount; rotationCount++) { results = multiReader.decodeMultiple(binaryBitmap, Options.Hints); if (results == null) { if (TryInverted && luminanceSource.InversionSupported) { binaryBitmap = new BinaryBitmap(CreateBinarizer(luminanceSource.invert())); results = multiReader.decodeMultiple(binaryBitmap, Options.Hints); } } if (results != null || !luminanceSource.RotateSupported || !AutoRotate) break; binaryBitmap = new BinaryBitmap(CreateBinarizer(luminanceSource.rotateCounterClockwise())); } if (results != null) { foreach (var result in results) { if (result.ResultMetadata == null) { result.putMetadata(ResultMetadataType.ORIENTATION, rotationCount * 90); } else if (!result.ResultMetadata.ContainsKey(ResultMetadataType.ORIENTATION)) { result.ResultMetadata[ResultMetadataType.ORIENTATION] = rotationCount * 90; } else { // perhaps the core decoder rotates the image already (can happen if TryHarder is specified) result.ResultMetadata[ResultMetadataType.ORIENTATION] = ((int)(result.ResultMetadata[ResultMetadataType.ORIENTATION]) + rotationCount * 90) % 360; } } OnResultsFound(results); } return results; }
public void ReadQRCode(string fileName, CallbackContext callbackContext) { try { m_Logger.Debug($"[QRCodeUtils]ReadRQCode=>fileName:{fileName}"); var qrCodeResult = new QRCodeResult(); List <string> textList = new List <string>(); if (!File.Exists(fileName)) { qrCodeResult.IsSuccess = false; textList.Add("文件路径不存在"); qrCodeResult.Content = textList.ToArray(); callbackContext.SendPluginResult(new PluginResult(PluginResult.Status.OK, qrCodeResult)); return; } using (FileStream fs = new FileStream(fileName, FileMode.Open)) { QRCodeMultiReader qc = new QRCodeMultiReader(); Image image = Image.FromStream(fs); Bitmap bitmap = new Bitmap(image); LuminanceSource source = new BitmapLuminanceSource(bitmap); 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[] decodeResult = qc.decodeMultiple(binarybitmap, hints); m_Logger.Debug($"[QRCodeUtils]ReadRQCode=>decodeResult == null:{decodeResult == null}"); if (decodeResult == null || decodeResult.Length == 0) { qrCodeResult.IsSuccess = false; textList.Add("无法识别图片中的二维码"); qrCodeResult.Content = textList.ToArray(); callbackContext.SendPluginResult(new PluginResult(PluginResult.Status.OK, qrCodeResult)); return; } foreach (Result res in decodeResult) { if (res.Text != null) { textList.Add(res.Text); } } } qrCodeResult.Content = textList.ToArray(); m_Logger.Debug($"[QRCodeUtils]ReadRQCode=>textList.ToArray().Length:{textList.ToArray().Length}"); if (textList.ToArray().Length == 0) { qrCodeResult.IsSuccess = false; callbackContext.SendPluginResult(new PluginResult(PluginResult.Status.OK, qrCodeResult)); return; } qrCodeResult.IsSuccess = true; callbackContext.SendPluginResult(new PluginResult(PluginResult.Status.OK, qrCodeResult)); } catch (Exception err) { m_Logger.Error(err); callbackContext.Error(new { type = err.GetType().Name, code = "", message = err.Message, details = err.StackTrace }); } }
public async Task <IList <IDictionary <string, float> > > processSoftwareBitmap(SoftwareBitmap bitmap) { SoftwareBitmap bitmapToProcess = SoftwareBitmap.Convert(bitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); uint cropSize = Convert.ToUInt32(bitmap.PixelHeight); uint sideDiff = Convert.ToUInt32(bitmap.PixelWidth - bitmap.PixelHeight); SoftwareBitmap refCropedImage = await cropImageWithPixel(bitmap, sideDiff, 0, cropSize, cropSize); bitmapToProcess = await resizeImgForDetect(refCropedImage); //Windows.Storage.StorageFile testFile = await folder.CreateFileAsync("test.jpg", Windows.Storage.CreationCollisionOption.ReplaceExisting); //using (IRandomAccessStream stream = await testFile.OpenAsync(FileAccessMode.ReadWrite)) //{ // Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId; // BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream); // encoder.SetSoftwareBitmap(refCropedImage); // await encoder.FlushAsync(); //} //Convert SoftwareBitmap into VideoFrame using (VideoFrame frame = VideoFrame.CreateWithSoftwareBitmap(bitmapToProcess)) { if (frame == null) { return(null); } try { modelInput input = new modelInput(); input.data = ImageFeatureValue.CreateFromVideoFrame(frame); if (input.data == null) { return(null); } IList <PredictionModel> result = await detection.PredictImageAsync(frame); //await saveDebugImage(refCropedImage, result); var reader = new QRCodeMultiReader(); for (int predictIdx = 0; predictIdx < result.Count; predictIdx++) { PredictionModel predictInfo = result[predictIdx]; SoftwareBitmap cropedImage = await cropImage(refCropedImage, Math.Max(predictInfo.BoundingBox.Left, 0), Math.Max(predictInfo.BoundingBox.Top, 0), predictInfo.BoundingBox.Width, predictInfo.BoundingBox.Height); //Windows.Storage.StorageFile testFile = await folder.CreateFileAsync("test" + predictIdx + ".jpg", Windows.Storage.CreationCollisionOption.ReplaceExisting); //using (IRandomAccessStream stream = await testFile.OpenAsync(FileAccessMode.ReadWrite)) //{ // Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId; // BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream); // encoder.SetSoftwareBitmap(cropedImage); // await encoder.FlushAsync(); //} var source = new SoftwareBitmapLuminanceSource(cropedImage); HybridBinarizer binarizer = new HybridBinarizer(source); var results = reader.decodeMultiple(new BinaryBitmap(binarizer)); var info = parseZXingResult(results); string locText = info["locText"]; string boxText = info["boxText"]; if (locText.Equals("") || !locText.StartsWith(zone)) { continue; } if (finalResult.ContainsKey(locText)) { if (finalResult[locText].Equals("")) { finalResult[locText] = boxText; } } else { finalResult.Add(locText, boxText); } } } catch (InvalidOperationException ie) { return(null); } catch (Exception e) { return(null); } finally { bitmap.Dispose(); } } return(null); }
void createWorker() { //create worker thread for reading barcode readerWorker = new Task(async() => { //use stopwatch to time the execution, and execute the reading process repeatedly var watch = System.Diagnostics.Stopwatch.StartNew(); var reader = new QRCodeMultiReader(); SoftwareBitmap bitmap; HybridBinarizer binarizer; while (true) { try { lock (bufLock) { bitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, width, height); bitmap.CopyFromBuffer(decodedDataBuf.AsBuffer()); } } catch { //the size maybe incorrect due to unknown reason await Task.Delay(10); continue; } var source = new SoftwareBitmapLuminanceSource(bitmap); binarizer = new HybridBinarizer(source); var results = reader.decodeMultiple(new BinaryBitmap(binarizer)); if (results != null && results.Length > 0) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { foreach (var result in results) { if (!readed.Contains(result.Text)) { readed.Add(result.Text); Textbox.Text += result.Text + "\n"; } } }); } //System.Diagnostics.Debug.WriteLine("Looping...\n"); watch.Stop(); int elapsed = (int)watch.ElapsedMilliseconds; //broadcasting messages here System.Diagnostics.Debug.WriteLine("currentAltitude:{0}", currentAltitude); //System.Diagnostics.Debug.WriteLine("latitude:{0}\tlongitude:{1}", latitude, longitude); System.Diagnostics.Debug.WriteLine("Aircraft heading direction:{0}", true_north_heading); //System.Diagnostics.Debug.WriteLine("3D-plane coordinate: x={0}\ty={1}\tz={2}", N_position, E_position, D_position); System.Diagnostics.Debug.WriteLine("2D-plane coordinate: x={0}\ty={1}", current2Dpostion.x, current2Dpostion.y); //Update GUI View await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { StateBox.Text = "Current State: " + mission_state; AltitudeBox.Text = "Current Altitude: " + currentAltitude; PositionBox.Text = "Current Position: " + Math.Round(current2Dpostion.x, 5) + " " + Math.Round(current2Dpostion.y, 5); RotationBox.Text = "Current Heading: " + true_north_heading; if (current2Dpostion.x == 0 && current2Dpostion.y == 0) { Message.Text = "Initialization Failed. Restart the App after connected to drone."; } }); //run at max 5Hz await Task.Delay(Math.Max(0, 200 - elapsed)); await DJISDKManager.Instance.ComponentManager.GetCameraHandler(0, 0).SetCameraColorAsync(new CameraColorMsg() { value = CameraColor.BLACK_WHITE }); } }); }
void createWorker() { //create worker thread for reading barcode readerWorker = new Task(async() => { //use stopwatch to time the execution, and execute the reading process repeatedly var watch = System.Diagnostics.Stopwatch.StartNew(); var reader = new QRCodeMultiReader(); SoftwareBitmap bitmap; HybridBinarizer binarizer; while (true) { try { lock (bufLock) { bitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, width, height); bitmap.CopyFromBuffer(decodedDataBuf.AsBuffer()); } } catch { //the size maybe incorrect due to unknown reason await Task.Delay(10); continue; } var source = new SoftwareBitmapLuminanceSource(bitmap); binarizer = new HybridBinarizer(source); var results = reader.decodeMultiple(new BinaryBitmap(binarizer)); if (results != null && results.Length > 0) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { var frame = this._timer.ElapsedMilliseconds / 100; var toAdd = new List <string>(); var sizes = new List <double>(); foreach (var result in results) { var pos = (X: result.ResultPoints[0].X, Y: result.ResultPoints[0].Y); var code = result.Text; //this.Textbox.Text += $"{code} -> ({pos.X}, {pos.Y})" + Environment.NewLine; toAdd.Add($"{frame}, {code}, {pos.X}, {pos.Y}"); var dists = new List <double>(); if (result.ResultPoints.Length == 3) { var p0 = result.ResultPoints[0]; var p1 = result.ResultPoints[1]; var p2 = result.ResultPoints[2]; dists.Add(Math.Sqrt(Math.Pow(p0.X - p1.X, 2) + Math.Pow(p0.Y - p1.Y, 2))); dists.Add(Math.Sqrt(Math.Pow(p0.X - p2.X, 2) + Math.Pow(p0.Y - p2.Y, 2))); dists.Add(Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2))); sizes.Add(dists.Average()); } } if (sizes.Any()) { _sizes = sizes; _codes = results.ToList(); if (_codes.Any()) { _prevLocCode = _currLocCode; var locCodes = _codes .Where(x => x.Text.StartsWith("LB")) .Select(x => Int32.TryParse(x.Text.Replace("LB", ""), out var id) ? id : (int?)null) .Where(x => x.HasValue) .ToList(); if (locCodes.Any()) { _currLocCode = locCodes.Cast <int>().Max(); } } UpdatePitchCommand(); UpdateAction(); //Textbox.Text = $"Average code size is: {_sizes.Average()}" + Environment.NewLine; } lock (toAdd) { //using (var fs = File.Open(@"qrcode.csv", FileMode.Append)) { //using (var sw = new StreamWriter(fs)) { //toAdd.ForEach(sw.WriteLine); } } } }); }