private async Task <Result> Decode(IRandomAccessStream stream) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync(); LuminanceSource luminanceSource = new SoftwareBitmapLuminanceSource(bitmap); Binarizer binarizer = new HybridBinarizer(luminanceSource); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); MultiFormatReader reader = new MultiFormatReader(); return(reader.decode(binaryBitmap)); }
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)); } }); }
/// <summary> /// Continuously look for a QR code /// NOTE: this method won't work if recording is enabled ('hey Cortana, start recording' thing). /// </summary> public static async Task <string> ReadAsync(CancellationToken token = default(CancellationToken)) { var mediaCapture = new MediaCapture(); await mediaCapture.InitializeAsync(); await mediaCapture.AddVideoEffectAsync(new MrcVideoEffectDefinition(), MediaStreamType.Photo); var reader = new BarcodeReader(); reader.Options.TryHarder = false; while (!token.IsCancellationRequested) { var imgFormat = ImageEncodingProperties.CreateJpeg(); using (var ras = new InMemoryRandomAccessStream()) { await mediaCapture.CapturePhotoToStreamAsync(imgFormat, ras); var decoder = await BitmapDecoder.CreateAsync(ras); using (var bmp = await decoder.GetSoftwareBitmapAsync()) { Result result = await Task.Run(() => { var source = new SoftwareBitmapLuminanceSource(bmp); return(reader.Decode(source)); }); if (!string.IsNullOrEmpty(result?.Text)) { return(result.Text); } } } await Task.Delay(DelayBetweenScans); } return(null); }
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); } } } }); }
public BinaryBitmap(SoftwareBitmapLuminanceSource luminanceSource) { this.luminanceSource = luminanceSource; }