Scanning barcodes in Win10 via surface pro 6 camera
Paul Long
1
Reputation point
I found this code in a 2014/5 question in the MSDN forums...
Can someone tell me which technology this is using? It looks like WPF to me, but I'm hoping I can convert it and integrate it into an existing VB2017 project i have. Is that possible? What dependencies does this code use? Do i need references, nuget packages, or is this something else?
private async void StartPreview_Click(object sender, RoutedEventArgs e)
{
//var resultCamera = await GetMediaCapture();
Windows.Devices.Enumeration.Panel location = Windows.Devices.Enumeration.Panel.Back;
var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
foreach (var camera in cameras)
{
if (camera.EnclosureLocation.Panel == location)
{
var mcs = new MediaCaptureInitializationSettings();
mcs.VideoDeviceId = camera.Id;
mcs.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
mcs.StreamingCaptureMode = StreamingCaptureMode.Video;
var mc = new MediaCapture();
await mc.InitializeAsync(mcs);
VideoStream.Source = mc;
await mc.StartPreviewAsync();
mc.SetPreviewMirroring(false);
ShowSettings.Visibility = Visibility.Visible;
StartPreview.IsEnabled = false;
var properties = mc.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
var videoEncodingProperties = properties as VideoEncodingProperties;
if (videoEncodingProperties != null)
{
var writeableBitmap = new WriteableBitmap((int)videoEncodingProperties.Width, (int)videoEncodingProperties.Height);
await Task.Delay(1000);
bool barCodeNotFound = true;
while (barCodeNotFound)
{
stream.Seek(0);
await mc.CapturePhotoToStreamAsync(imageEncoding, stream);
stream.Seek(0);
await writeableBitmap.SetSourceAsync(stream);
var result = ScanBitmap(writeableBitmap);
if (result != null)
{
ScanResult.Text = result.Text;
barCodeNotFound = false;
await mc.StopPreviewAsync();
mc = null;
previewStarted = false;
VideoStream.Source = null;
StartPreview.IsEnabled = true;
}
}
}
// return mc;
}
}