Advertisement
Guest User

Untitled

a guest
Apr 13th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1.     public sealed partial class Scanner2 : Page
  2.     {
  3.         private readonly MediaCapture _mediaCapture = new MediaCapture();
  4.         private Result _result;
  5.  
  6.         public Scanner2()
  7.         {
  8.             InitializeComponent();
  9.         }
  10.  
  11.         protected override async void OnNavigatedTo(NavigationEventArgs e)
  12.         {
  13.             try
  14.             {
  15.                 var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
  16.                 if (cameras.Count < 1)
  17.                 {
  18.                     throw new NoCameraAvailableException();
  19.                 }
  20.                 MediaCaptureInitializationSettings settings;
  21.                 if (cameras.Count == 1)
  22.                 {
  23.                     settings = new MediaCaptureInitializationSettings {VideoDeviceId = cameras[0].Id};
  24.                 }
  25.                 else
  26.                 {
  27.                     DeviceInformation backWebcam = (from webcam in cameras
  28.                                                     where webcam.EnclosureLocation != null
  29.                                                     && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
  30.                                                     select webcam).FirstOrDefault();
  31.  
  32.                     settings = new MediaCaptureInitializationSettings {VideoDeviceId = backWebcam.Id};
  33.                 }
  34.  
  35.                 await _mediaCapture.InitializeAsync(settings);
  36.                 VideoCapture.Source = _mediaCapture;
  37.                 await _mediaCapture.StartPreviewAsync();
  38.  
  39.                 var focusSetting = new FocusSettings
  40.                 {
  41.                     Mode = FocusMode.Continuous,
  42.                     AutoFocusRange = AutoFocusRange.Normal,
  43.                     DisableDriverFallback = false,
  44.                     WaitForFocus = true
  45.                 };
  46.  
  47.                 _mediaCapture.VideoDeviceController.FocusControl.Configure(focusSetting);
  48.                 await _mediaCapture.VideoDeviceController.ExposureControl.SetAutoAsync(true);
  49.  
  50.                 _mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
  51.                 _mediaCapture.VideoDeviceController.FlashControl.Enabled = false;
  52.  
  53.                 while (_result == null)
  54.                 {
  55.                     using (var stream = new InMemoryRandomAccessStream())
  56.                     {
  57.                         await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);  
  58.  
  59.                         stream.Seek(0);
  60.  
  61.                         var writeableBitmap = new WriteableBitmap(1, 1);
  62.                         await writeableBitmap.SetSourceAsync(stream);
  63.  
  64.                         _result = ScanBitmap(writeableBitmap);
  65.                     }
  66.                 }
  67.  
  68.                 await _mediaCapture.StopPreviewAsync();
  69.                 VideoCapture.Visibility = Visibility.Collapsed;
  70.                 CaptureImage.Visibility = Visibility.Visible;
  71.                
  72.                 Debug.WriteLine("Scanned text: {0}", _result.Text);
  73.                 Frame.GoBack();
  74.             }
  75.             catch (UnauthorizedAccessException ex)
  76.             {
  77.  
  78.             }
  79.             catch (NoCameraAvailableException ex)
  80.             {
  81.                
  82.             }
  83.             catch (Exception ex)
  84.             {
  85.                
  86.             }
  87.         }
  88.  
  89.         private Result ScanBitmap(WriteableBitmap writeableBmp)
  90.         {
  91.             var barcodeReader = new BarcodeReader
  92.             {
  93.                 Options = new DecodingOptions
  94.                 {
  95.                     PossibleFormats = new[] { BarcodeFormat.QR_CODE },
  96.                     TryHarder = true
  97.                 },
  98.                 AutoRotate = true
  99.             };
  100.             var result = barcodeReader.Decode(writeableBmp);
  101.  
  102.             if (result != null)
  103.             {
  104.                 CaptureImage.Source = writeableBmp;
  105.             }
  106.  
  107.             return result;
  108.         }
  109.  
  110.         protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
  111.         {
  112.             await _mediaCapture.StopPreviewAsync();
  113.         }
  114.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement