windows rt 扫描二维码
项目中使用的是ZXing.net,应用商店程序。使用到的dll是ZXing.winmd。
大致思路为,使用MediaCapture捕获图片。获取到CapturePhotoToStreamAsync流,然后将其转换为WriteableBitmap,使用ZXing进行解析,该项目主要是为了获取二维码类型和文本值。
代码如下:
1 private async void CaptureBarcode() 2 { 3 mediaCapture = new MediaCapture(); 4 await mediaCapture.InitializeAsync(); 5 captureElement.Source = mediaCapture; 6 await mediaCapture.StartPreviewAsync(); 7 Result result = default(Result); 8 while (true) 9 { 10 IRandomAccessStream stream = new InMemoryRandomAccessStream(); 11 await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreatePng(), stream); 12 await stream.FlushAsync(); 13 stream.Seek(0); 14 IBarcodeReader reader = new BarcodeReader(); 15 WriteableBitmap bitmap = new WriteableBitmap(640, 480); 16 bitmap.SetSource(stream); 17 if (bitmap == null) 18 continue; 19 result = reader.Decode(bitmap); 20 if (result != null) 21 break; 22 continue; 23 } 24 if (result == default(Result)) 25 return; 26 await mediaCapture.StopPreviewAsync(); 27 jsonFunc("{ \"type\":\"" + result.BarcodeFormat + "\",\"code\":\"" + result.Text + "\"}"); 28 }