I'm migrating from Xamarin to .NET7 and have a WInUI3 Application which uses a QR Code Reader. In WinUI3 there is no CaptureElement for the Preview Stream of the Camera. But there is now a possibility to get the Camera Stream and every Frame. I tried to decode a frame with the Barcode Reader Object but it does not detect any QR Code.
Do You have any idea why? Here is the code snippet for getting every MediaPlayer frame
var videoCaptureDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
var deviceid = videoCaptureDevices.First().Id;
_mediaFrameSourceGroup = await MediaFrameSourceGroup.FromIdAsync(deviceid);
_mediaCapture = new MediaCapture();
var mediaCaptureInitializationSettings = new MediaCaptureInitializationSettings()
{
SourceGroup = _mediaFrameSourceGroup,
SharingMode = MediaCaptureSharingMode.SharedReadOnly,
StreamingCaptureMode = StreamingCaptureMode.Video,
MemoryPreference = MediaCaptureMemoryPreference.Cpu,
};
await _mediaCapture.InitializeAsync(mediaCaptureInitializationSettings);
mediaPlayerElement.Source = MediaSource.CreateFromMediaFrameSource(_mediaCapture.FrameSources[_mediaFrameSourceGroup.SourceInfos[0].Id]);
await SetupAutoFocus();
_mediaFrameReader = await _mediaCapture.CreateFrameReaderAsync(_mediaCapture.FrameSources[_mediaFrameSourceGroup.SourceInfos[0].Id], MediaEncodingSubtypes.Argb32);
_mediaFrameReader.FrameArrived += MediaFrameReader_FrameArrived;
await _mediaFrameReader.StartAsync();
And here is the Eventhandler:
private void MediaFrameReader_FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
{
var mediaframeReader = sender as MediaFrameReader;
if (mediaframeReader != null)
{
var mfr = mediaframeReader.TryAcquireLatestFrame();
var videoMediaFrame = mfr?.VideoMediaFrame;
var softwareBitmap = videoMediaFrame?.SoftwareBitmap;
if (softwareBitmap == null)
{
return;
}
Debug.WriteLine($"SoftwareBitMap:{BitConverter.ToString(EncodedBytes(softwareBitmap, BitmapEncoder.JpegEncoderId).Result)}");
if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
softwareBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied)
{
softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
}
// From this point on, follow Microsoft's 'CameraFrames' universal sample to process the frame and do what you want with it: https://github.com/microsoft/Windows-universal-samples
var formats = new List<BarcodeFormat>
{
BarcodeFormat.DATA_MATRIX,
BarcodeFormat.QR_CODE
};
var barcodeReader = new BarcodeReader()
{
//AutoRotate = true,
//TryInverted = true
};
barcodeReader.Options.PossibleFormats = formats;
// Create our luminance source
var luminanceSource = new SoftwareBitmapLuminanceSource(softwareBitmap);
Debug.WriteLine(BitConverter.ToString(luminanceSource.Matrix, 0, 50));
var result = barcodeReader.Decode(luminanceSource);
if (result != null)
{
Debug.WriteLine($"Result Format:{result.BarcodeFormat}");
}
// Check if a result was found
if (result != null && !string.IsNullOrEmpty(result.Text))
{
Debug.WriteLine($"ScanResult is Text: {result.Text}");
}
softwareBitmap?.Dispose();
mfr?.Dispose();
}
}
I'm migrating from Xamarin to .NET7 and have a WInUI3 Application which uses a QR Code Reader. In WinUI3 there is no
CaptureElementfor the Preview Stream of the Camera. But there is now a possibility to get the Camera Stream and every Frame. I tried to decode a frame with theBarcode ReaderObject but it does not detect any QR Code.Do You have any idea why? Here is the code snippet for getting every
MediaPlayerframeAnd here is the Eventhandler: