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

Adds scanner recognition in some android devices #180

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void Connect()
// Frame by frame analyze
imageAnalyzer = new ImageAnalysis.Builder()
.SetDefaultResolution(new Android.Util.Size(640, 480))
.SetOutputImageFormat(ImageAnalysis.OutputImageFormatRgba8888)
.SetBackpressureStrategy(ImageAnalysis.StrategyKeepOnlyLatest)
.Build();

Expand Down
39 changes: 36 additions & 3 deletions ZXing.Net.MAUI/ZXingBarcodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ public BarcodeResult[] Decode(PixelBufferHolder image)
LuminanceSource ls = default;

#if ANDROID
ls = new ByteBufferYUVLuminanceSource(image.Data, w, h, 0, 0, w, h);
Java.Nio.ByteBuffer imageData = Bitmap2Yuv420p(image.Data, w, h);
ls = new ByteBufferYUVLuminanceSource(imageData, w, h, 0, 0, w, h);
#elif MACCATALYST || IOS
ls = new CVPixelBufferBGRA32LuminanceSource(image.Data, w, h);
#elif WINDOWS
ls = new SoftwareBitmapLuminanceSource(image.Data);
#endif

if (Options.Multiple)
if (Options.Multiple)
return zxingReader.DecodeMultiple(ls)?.ToBarcodeResults();

var b = zxingReader.Decode(ls)?.ToBarcodeResult();
Expand All @@ -51,5 +52,37 @@ public BarcodeResult[] Decode(PixelBufferHolder image)

return null;
}
}

#if ANDROID
private static unsafe Java.Nio.ByteBuffer Bitmap2Yuv420p(Java.Nio.ByteBuffer buffer, int w, int h)
{
byte[] image = new byte[buffer.Remaining()];
buffer.Get(image);

byte[] imageYuv = new byte[w * h];

fixed (byte* packet = image)
{
byte* pimage = packet;

fixed (byte* packetOut = imageYuv)
{
byte* pimageOut = packetOut;

for (int i = 0; i < (w * h); i++)
{
byte r = *pimage++;
byte g = *pimage++;
byte b = *pimage++;
pimage++; // a
*pimageOut++ = (byte)(((66 * r + 129 * g + 25 * b) >> 8) + 16);
}
}
}

return Java.Nio.ByteBuffer.Wrap(imageYuv);
}
#endif

}
}
Loading