From 7483c8d6ea04866a4749025409b2d0926e251ace Mon Sep 17 00:00:00 2001 From: Tsubasa Kobayashi Date: Wed, 10 Apr 2024 18:05:14 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=80=E9=83=A8=E3=81=AEAndroid?= =?UTF-8?q?=E7=AB=AF=E6=9C=AB=E3=81=A7QR=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E3=81=8C=E8=AA=AD=E3=81=BF=E5=8F=96=E3=82=8C=E3=81=AA=E3=81=84?= =?UTF-8?q?=E3=83=90=E3=82=B0=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit reference: https://github.com/Redth/ZXing.Net.Maui/issues/107 --- .../Android/CameraManager.android.cs | 1 + ZXing.Net.MAUI/ZXingBarcodeReader.cs | 41 ++++++++++++++++--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs b/ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs index 51b52f9..3d17430 100644 --- a/ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs +++ b/ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs @@ -66,6 +66,7 @@ public void Connect() imageAnalyzer = new ImageAnalysis.Builder() .SetDefaultResolution(new Android.Util.Size(640, 480)) //.SetOutputImageRotationEnabled(true) // FIXED: Could not read QRCode. + .SetOutputImageFormat(ImageAnalysis.OutputImageFormatRgba8888) .SetBackpressureStrategy(ImageAnalysis.StrategyKeepOnlyLatest) .Build(); diff --git a/ZXing.Net.MAUI/ZXingBarcodeReader.cs b/ZXing.Net.MAUI/ZXingBarcodeReader.cs index d10cc1d..a96c013 100644 --- a/ZXing.Net.MAUI/ZXingBarcodeReader.cs +++ b/ZXing.Net.MAUI/ZXingBarcodeReader.cs @@ -1,5 +1,4 @@ -using Microsoft.Maui.Graphics; - + namespace ZXing.Net.Maui.Readers { public class ZXingBarcodeReader : Readers.IBarcodeReader @@ -34,14 +33,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(); @@ -50,5 +50,36 @@ public BarcodeResult[] Decode(PixelBufferHolder image) return null; } - } + +#if ANDROID + public 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 + } }