Skip to content

Commit

Permalink
fix: 一部のAndroid端末でQRコードが読み取れないバグの修正
Browse files Browse the repository at this point in the history
reference: Redth#107
  • Loading branch information
neo-tsubasa committed Apr 10, 2024
1 parent 327c9da commit 7483c8d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
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 @@ -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();

Expand Down
41 changes: 36 additions & 5 deletions ZXing.Net.MAUI/ZXingBarcodeReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Maui.Graphics;


namespace ZXing.Net.Maui.Readers
{
public class ZXingBarcodeReader : Readers.IBarcodeReader
Expand Down Expand Up @@ -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();
Expand All @@ -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
}
}

0 comments on commit 7483c8d

Please sign in to comment.