Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zxing and WinUI3 #1080

Open
SprengerS opened this issue Aug 14, 2023 · 0 comments
Open

Zxing and WinUI3 #1080

SprengerS opened this issue Aug 14, 2023 · 0 comments

Comments

@SprengerS
Copy link

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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant