How to Use MediaCapture to solve the problem of QR code scanning and mediacapture Scanning

Source: Internet
Author: User

How to Use MediaCapture to solve the problem of QR code scanning and mediacapture Scanning

The implementation of QR code scanning can be divided into three steps: "imaging", "", and "recognition ".

In UWP development, the most common media tool is not MediaCapture. The following describes how to use MediaCapture to scan and identify two-dimensional Codes Using Zxing, as well as problems and precautions.

1. Initialization and Imaging

1 private async void InitMediaCaptureAsync () 2 {3 // find the rear camera 4 var allVideoDevices = await DeviceInformation. findAllAsync (DeviceClass. videoCapture); 5 var cameraDevice = allVideoDevices. firstOrDefault (x => x. enclosureLocation! = Null & x. enclosureLocation. panel = Windows. devices. enumeration. panel. back); 6 7 if (cameraDevice = null) 8 {9 Debug. writeLine ("No camera device found! "); 10 11 return; 12} 13 14 var settings = new MediaCaptureInitializationSettings15 {16 StreamingCaptureMode = StreamingCaptureMode. video, 17 // required, otherwise it will be very slow 18 PhotoCaptureSource = PhotoCaptureSource. videoPreview, 19 VideoDeviceId = cameraDevice. id20}; 21 22 _ mediaCapture = new MediaCapture (); 23 24 try25 {26 await _ mediaCapture. initializeAsync (settings); 27 _ initialized = true; // initialization successful 28} 29 catch (UnauthorizedAccessException) 30 {31 Debug. writeLine ("The app was denied access to the camera"); 32} 33 catch (Exception ex) 34 {35 Debug. writeLine ("Exception when initializing MediaCapture with {0 }:{ 1}", cameraDevice. id, ex. toString (); 36} 37 38 if (_ initialized) 39 {40 var focusControl = _ mediaCapture. videoDeviceController. focusControl; 41 42 if (focusControl. supported) 43 {44 var focusSettings = new FocusSettings () 45 {46 Mode = focusControl. supportedFocusModes. firstOrDefault (f => f = FocusMode. continuous), 47 DisableDriverFallback = true, 48 AutoFocusRange = focusControl. supportedFocusRanges. firstOrDefault (f => f = AutoFocusRange. fullRange), 49 Distance = focusControl. supportedFocusDistances. firstOrDefault (f => f = ManualFocusDistance. nearest) 50}; 51 52 // set the focus. It is best to use FocusMode. continuous. Otherwise, the impact will be blurred, making it difficult to identify 53 focusControl. configure (focusSettings); 54} 55 56 captureElement. source = _ mediaCapture; 57 captureElement. flowDirection = FlowDirection. leftToRight; 58 59 try60 {61 await _ mediaCapture. startPreviewAsync (); 62 _ previewing = true; 63} 64 catch (Exception ex) 65 {66 Debug. writeLine ("Exception when starting the preview: {0}", ex. toString (); 67} 68 69 if (_ previewing) 70 {71 try72 {73 if (_ mediaCapture. videoDeviceController. flashControl. supported) 74 {75 // turn off the flashlight 76 _ mediaCapture. videoDeviceController. flashControl. enabled = false; 77} 78} 79 catch80 {81} 82 83 if (focusControl. supported) 84 {85 // start to focus on 86 await focusControl. focusAsync (); 87} 88} 89} 90}View Code

2. and Identification

1 private void InitTimer () 2 {3 _ timer = new DispatcherTimer (); 4 // cut the figure 5 _ timer every 50 milliseconds. interval = TimeSpan. fromMilliseconds (50); 6 _ timer. tick + = _ timer_Tick; 7 _ timer. start (); 8}View Code 1 private async void _ timer_Tick (object sender, object e) 2 {3 using (var stream = new InMemoryRandomAccessStream () 4 {5 var previewProperties = _ mediaCapture. videoDeviceController. getMediaStreamProperties (MediaStreamType. videoPreview) as VideoEncodingProperties; 6 // write to the memory stream 7 await _ mediaCapture. capturePhotoToStreamAsync (ImageEncodingProperties. createJpeg (), stream); 8 9 // identified by Zxing Failed: Stop timer; failed: Continue 10 var reader = new BarcodeReader (); 11 var bitmapWriteable = new WriteableBitmap (int) previewProperties. width, (int) previewProperties. height); 12 bitmapWriteable. setSource (stream); 13 var result = reader. decode (bitmapWriteable); 14 15 if (! String. IsNullOrEmpty (result. Text) 16 {17 _ timer. Stop (); 18} 19} 20}View Code

Here, by the way, how to Install Zxing, open the nuget Manager Command window, enter Install-Package ZXing. Net, and press Enter. There are many tutorials on how to use Zxing.

3. Problems and Optimization

A) Sound

When CapturePhotoToStreamAsync is used to capture an image, sometimes the image will be "Rubbed and rubbed", which affects the user experience. The best practice is to find a method that can directly intercept the image from the video stream, here I have to say that MediaCapture is really powerful. MediaCapture provides us with the GetPreviewFrameAsync method to retrieve one of the frames from the video stream directly. So I modified the Code as follows, that is to say, there is no annoying "smooth and smooth" sound.

1 private async void _ timer_Tick (object sender, object e) 2 {3 var previewProperties = _ mediaCapture. videoDeviceController. getMediaStreamProperties (MediaStreamType. videoPreview) as VideoEncodingProperties; 4 5 using (var videoFrame = new VideoFrame (BitmapPixelFormat. rgba8, (int) previewProperties. width, (int) previewProperties. height) 6 {7 using (var currentFrame = await _ mediaCapture. getPreviewFr AmeAsync (videoFrame) 8 {9 using (var previewFrame = currentFrame. softwareBitmap) 10 {11 var buffer = new Windows. storage. streams. buffer (uint) (4 * previewFrame. pixelWidth * previewFrame. pixelHeight); 12 previewFrame. copyToBuffer (buffer); 13 14 using (var stream = buffer. asStream (). asRandomAccessStream () 15 {16 // identify using Zxing. Success: Stop timer; Failure: Continue 17 var reader = new BarcodeReader (); 18 var bitmapWriteable = New WriteableBitmap (int) previewProperties. width, (int) previewProperties. height); 19 bitmapWriteable. setSource (stream); 20 var result = reader. decode (bitmapWriteable); 21 22 if (! String. IsNullOrEmpty (result. Text) 23 {24 _ timer. Stop (); 25} 26} 27} 28} 29} 30}View Code

By the way, remember to use the following two namespaces:

using System.IO;using System.Runtime.InteropServices.WindowsRuntime;

Otherwise, buffer. AsStream (). AsRandomAccessStream () cannot be implemented ()

B) continuous focus

Not all models support Continuous focus (FocusMode. Continuous). At this time, you can only achieve intermittent Continuous focus on your own.

C) Post-Image Processing

Sometimes, in order to achieve some functions (such as scanning boxes) or improve the recognition rate, we need to perform some secondary processing, cropping, scaling, or rotating on the captured image, we can use BitmapDecoder and BitmapEncoder to implement

Using (var stream = buffer. asStream (). asRandomAccessStream () {var decoder = await BitmapDecoder. createAsync (BitmapDecoder. includecoderid, stream); var destStream = new InMemoryRandomAccessStream (); var encoder = await BitmapEncoder. createForTranscodingAsync (destStream, decoder); // crops encoder. bitmapTransform. bounds = new BitmapBounds () {X = 0, Y = 0, Width = 100, Height = 100}; // scale encoder. bitmapTransform. scaledWidth = 100; encoder. bitmapTransform. scaledHeight = 100; // rotate encoder. bitmapTransform. rotation = BitmapRotation. clockwise90Degrees; await encoder. flushAsync (); await destStream. flushAsync ();
}

D) Suspend and wake up

In addition, it is worth noting that there are a series of state transitions in the Suspending and Resuming scenarios, which can easily cause bugs and should be handled to avoid crash.

4. Last

The recognized string processing is generally two types of hyperlinks and common text. Of course, the bar code scanning function can also be added to identify the encoding, no matter what, you can handle the problem according to the specific needs of the project.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.