I'm decoding barcodes using the built in camera, I do this with the capElement.Source.CapturePhotoToStreamAsync
to capture photos from the preview. it works, but freezes the app for a brief moment, which feels very clumsy and buggy.
So I'd like to this in background while at least leaving a responsive UI while processing the photos.
So far I came up with this to capture the video stream:
private async void ScanInBackground()
{
bool failedScan = true;
var stream = new InMemoryRandomAccessStream();
await capElement.Source.StartRecordToStreamAsync(MediaEncodingProfile.CreateWmv(VideoEncodingQuality.HD1080p), stream);
while(failedScan)
{
Byte[] bytes = await GetBytesFromStream(stream);
//How to split the bytes into frames?
Task.Delay(50);
}
Dispatcher.RunAsync(CoreDispatcherPriority.Low,() => StopCap());
}
and this method to get the bytes from the stream:
public static async Task<byte[]> GetBytesFromStream(IRandomAccessStream randomStream)
{
var reader = new DataReader(randomStream.GetInputStreamAt(0));
var bytes = new byte[randomStream.Size];
try
{
await reader.LoadAsync((uint)randomStream.Size); reader.ReadBytes(bytes);
}
catch(Exception ex)
{
Logger.LogExceptionAsync(ex, "GetBytesFromStream");
}
return bytes;
}
From the comment at the ScanInBackground
, you can see that I have no clue how to split the stream into photos/frames.
There is a sample on the Microsoft github page that is relevant, although they target Windows 10. You may be interested in migrating your project to get this functionality.
GetPreviewFrame: This sample will capture preview frames as opposed to full-blown photos. Once it has a preview frame, it can read and edit the pixels on it.
Here is the relevant part:
And declare this outside your class:
And of course, your project will have to allow unsafe code for all of this to work.
Have a closer look at the sample to see how to get all the details. Or, to have a walkthrough, you can watch the camera session from the recent //build/ conference, which includes a little bit of a walkthrough through some camera samples.
I believe that showing the media preview and handling the different possible exceptions is necessary, here a simple example of how to do that,
let say you have the following UI, with a
CaptureElement
to show the preview and anImage
control to show the captured pic,On the code behind declare a mediaCapture field,
then in the page loaded event handler you need to
and start the cam preview
And finally here how to properly take a shot, every thing is async so no lag or whatsoever