Skip to content

Commit

Permalink
new formats
Browse files Browse the repository at this point in the history
  • Loading branch information
afriscic committed Jun 12, 2024
1 parent 3580114 commit a4cd95b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
46 changes: 35 additions & 11 deletions BarcodeScanning.Native.Maui/Platform/Windows/CameraManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings()
UpdateResolution();
_mediaPlayerElement.Source = MediaSource.CreateFromMediaFrameSource(_mediaCapture?.FrameSources[_selectedCamera?.Id]);
var mediaPlayer = _mediaPlayerElement.MediaPlayer;
}
if (_mediaFrameReader is null)
Expand All @@ -149,26 +151,21 @@ await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings()

private void _mediaFrameReader_FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
{

if (sender is not null)
{
_barcodeReader.TryInvert = _cameraView?.ForceInverted ?? false;

using var frame = sender.TryAcquireLatestFrame();
using var videoBitmap = frame?.VideoMediaFrame?.SoftwareBitmap;
using var buffer = videoBitmap.LockBuffer(BitmapBufferAccessMode.Read);
using var reference = buffer.CreateReference();

unsafe
if (videoBitmap is not null && _cameraView is not null)
{
((Methods.IMemoryBufferByteAccess)reference).GetBuffer(out byte* dataInBytes, out uint capacity);
var iv = new ImageView(new IntPtr(dataInBytes), videoBitmap.PixelWidth, videoBitmap.PixelHeight, Methods.ConvertImageFormats(videoBitmap.BitmapPixelFormat));
var barcodes = _barcodeReader.From(iv);
if (_cameraView.CaptureNextFrame)
CaptureImage(videoBitmap);
else
PerformBarcodeDetection(videoBitmap);
}

var device = CanvasDevice.GetSharedDevice();

var image = new PlatformImage(device, CanvasBitmap.CreateFromSoftwareBitmap(device, videoBitmap));

}
}

Expand Down Expand Up @@ -306,6 +303,33 @@ private void ReportZoomFactors()
}
}

private void CaptureImage(SoftwareBitmap bitmap)
{
_cameraView.CaptureNextFrame = false;
var device = CanvasDevice.GetSharedDevice();
var image = new PlatformImage(device, CanvasBitmap.CreateFromSoftwareBitmap(device, bitmap));
_cameraView.TriggerOnImageCaptured(image);
}

private void PerformBarcodeDetection(SoftwareBitmap bitmap)
{
using var buffer = bitmap.LockBuffer(BitmapBufferAccessMode.Read);
using var reference = buffer.CreateReference();

List<Barcode> barcodes = [];
unsafe
{
((Methods.IMemoryBufferByteAccess)reference).GetBuffer(out byte* dataInBytes, out uint capacity);
var iv = new ImageView(new IntPtr(dataInBytes), bitmap.PixelWidth, bitmap.PixelHeight, Methods.ConvertImageFormats(bitmap.BitmapPixelFormat));
barcodes = _barcodeReader.From(iv);
}

foreach (var barcode in barcodes)
{
barcode.Position.
}
}

public void Dispose()
{
Dispose(true);
Expand Down
22 changes: 13 additions & 9 deletions BarcodeScanning.Native.Maui/Platform/Windows/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ internal static ZXingCpp.BarcodeFormats ConvertBarcodeFormats(BarcodeFormats bar
if (barcodeFormats.HasFlag(BarcodeFormats.Itf))
formats |= ZXingCpp.BarcodeFormats.ITF;
if (barcodeFormats.HasFlag(BarcodeFormats.QRCode))
{
formats |= ZXingCpp.BarcodeFormats.QRCode;
formats |= ZXingCpp.BarcodeFormats.RMQRCode;
}
if (barcodeFormats.HasFlag(BarcodeFormats.Upca))
formats |= ZXingCpp.BarcodeFormats.UPCA;
if (barcodeFormats.HasFlag(BarcodeFormats.Upce))
Expand All @@ -41,12 +38,19 @@ internal static ZXingCpp.BarcodeFormats ConvertBarcodeFormats(BarcodeFormats bar
if (barcodeFormats.HasFlag(BarcodeFormats.Aztec))
formats |= ZXingCpp.BarcodeFormats.Aztec;
if (barcodeFormats.HasFlag(BarcodeFormats.MicroQR))
{
formats |= ZXingCpp.BarcodeFormats.MicroQRCode;
formats |= ZXingCpp.BarcodeFormats.RMQRCode;
}
if (barcodeFormats.HasFlag(BarcodeFormats.GS1DataBar))
{
formats |= ZXingCpp.BarcodeFormats.DataBar;
formats |= ZXingCpp.BarcodeFormats.DataBarExpanded;
}
if (barcodeFormats.HasFlag(BarcodeFormats.MaxiCode))
formats |= ZXingCpp.BarcodeFormats.MaxiCode;
if (barcodeFormats.HasFlag(BarcodeFormats.DXFilmEdge))
formats |= ZXingCpp.BarcodeFormats.DXFilmEdge;
if (barcodeFormats.HasFlag(BarcodeFormats.All))
formats = ZXingCpp.BarcodeFormats.Any;
return formats;
Expand All @@ -67,16 +71,16 @@ private static BarcodeFormats ConvertFromZxingFormats(ZXingCpp.BarcodeFormats fo
ZXingCpp.BarcodeFormats.EAN8 => BarcodeFormats.Ean8,
ZXingCpp.BarcodeFormats.EAN13 => BarcodeFormats.Ean13,
ZXingCpp.BarcodeFormats.ITF => BarcodeFormats.Itf,
ZXingCpp.BarcodeFormats.MaxiCode => BarcodeFormats.,
ZXingCpp.BarcodeFormats.MaxiCode => BarcodeFormats.MaxiCode,
ZXingCpp.BarcodeFormats.PDF417 => BarcodeFormats.Pdf417,
ZXingCpp.BarcodeFormats.QRCode => BarcodeFormats.QRCode,
ZXingCpp.BarcodeFormats.UPCA => BarcodeFormats.Upca,
ZXingCpp.BarcodeFormats.UPCE => BarcodeFormats.Upce,
ZXingCpp.BarcodeFormats.PDF417 => BarcodeFormats.Pdf417,
ZXingCpp.BarcodeFormats.PDF417 => BarcodeFormats.Pdf417,
ZXingCpp.BarcodeFormats.PDF417 => BarcodeFormats.Pdf417,

}
ZXingCpp.BarcodeFormats.MicroQRCode => BarcodeFormats.MicroQR,
ZXingCpp.BarcodeFormats.RMQRCode => BarcodeFormats.MicroQR,
ZXingCpp.BarcodeFormats.DXFilmEdge => BarcodeFormats.DXFilmEdge,
_ => BarcodeFormats.None
};
}

internal static ZXingCpp.ImageFormat ConvertImageFormats(BitmapPixelFormat pixelFormat)
Expand Down
4 changes: 3 additions & 1 deletion BarcodeScanning.Native.Maui/Shared/BarcodeFormats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ public enum BarcodeFormats
MicroPdf417 = 16384,
I2OF5 = 32768,
GS1DataBar = 65536,
All = 131072
MaxiCode = 131072,
DXFilmEdge = 262144,
All = 524288
}

0 comments on commit a4cd95b

Please sign in to comment.