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

Compatibility with some Android smart-phones #107

Open
gyaccuzzi opened this issue Feb 15, 2023 · 10 comments
Open

Compatibility with some Android smart-phones #107

gyaccuzzi opened this issue Feb 15, 2023 · 10 comments

Comments

@gyaccuzzi
Copy link

Here a solution to give compatibility to some models of smart-phones with Android. The problem is these modelos (like samsung A23) reports YUV format with another structure, so the processing of the picture fails. To solve it I had to change it to RGBA format and to apply a convertion:

// Frame by frame analyze
imageAnalyzer = new ImageAnalysis.Builder()
	.SetDefaultResolution(new Android.Util.Size(640, 480))
	.SetOutputImageFormat(ImageAnalysis.OutputImageFormatRgba8888)
	.SetBackpressureStrategy(ImageAnalysis.StrategyKeepOnlyLatest)
	.Build();

And into "public BarcodeResult[] Decode(PixelBufferHolder image)"

#if ANDROID
            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);
#endif

Here the function

#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
@knocte
Copy link

knocte commented Mar 14, 2023

@gyaccuzzi hey, it seems you have mashed both a bug report and a solution together into the same issue.

I guess you could separate the bug from the solution by proposing the above code as a PR, and then give more details about this:

The problem is these modelos (like samsung A23) reports YUV format with another structure, so the processing of the picture fails

As in, what does "fails" mean exactly? An exception thrown? Please paste the whole exception (ex.ToString()) here.

@gyaccuzzi
Copy link
Author

Hi, there's not an exception, the issue is in some models of smart-phones ImageAnalysis send the imagen in YUV format but with another standard, so when you analize the image the QR is not found because the image is malformed. A solution that I found was to convert YUV to RGB, surely you can found another better solution with less processing. This happened with two differente models, samsung and motorola.

@knocte
Copy link

knocte commented Mar 14, 2023

Interesting, thanks for sharing.

SR84 added a commit to GEOSOFT-Vermessungssysteme/ZXing.Net.Maui that referenced this issue Oct 6, 2023
… und wahrscheinlich auch anderen Geräten auszugeleichen

Lösung von Github siehe: Redth#107
@SR84
Copy link

SR84 commented Oct 16, 2023

It happens with Xiaomi phones as well.

That's all very unfortunate, as I'm having difficulties converting my existing apps to Maui.

sbricchi added a commit to BA-MINDS/ZXing.Net.Maui.BAMinds that referenced this issue Apr 9, 2024
sbricchi added a commit to sbricchi/ZXingMauiTest that referenced this issue Apr 9, 2024
@sbricchi
Copy link

sbricchi commented Apr 9, 2024

Hi @gyaccuzzi. It seems to be working ok! And your answer is somehow hidden in a third page of open issues. I hope more devs can reach here. Thanks!

@knocte
Copy link

knocte commented Apr 9, 2024

@sbricchi can you propose a PR so that we all benefit from this fix?

@sbricchi
Copy link

sbricchi commented Apr 9, 2024

@sbricchi can you propose a PR so that we all benefit from this fix?

I think, I need to do more testing. Also, as @gyaccuzzi mentioned, i'm not sure it's the FINAL solution for this issue.
In the meantime you can re-fork and modify my fork at https://github.com/sbricchi/ZXing.Net.Maui.BAMinds.

@sbricchi
Copy link

@sbricchi can you propose a PR so that we all benefit from this fix?

Done: #180

@Eskissimo
Copy link

Hi all,

Thanks to @sbricchi & @gyaccuzzi for the work ! I suggest this additional code in order to get a good detection :

1- Add a constructor to handle orientation change :

    public CameraManager()
    {
        DeviceDisplay.MainDisplayInfoChanged += DeviceDisplay_MainDisplayInfoChanged;
    }

2- Update camera on orientation change :

    private void DeviceDisplay_MainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
    {
        if (cameraPreview != null) UpdateCamera();
    }

3- Unregister event in Dispose :

    public void Dispose()
    {
        DeviceDisplay.MainDisplayInfoChanged -= DeviceDisplay_MainDisplayInfoChanged;

        cameraExecutor?.Shutdown();
        cameraExecutor?.Dispose();
    }

4- Configure camera preview with the same settings of the image analyzer :

            cameraPreview = new AndroidX.Camera.Core.Preview.Builder()
                .SetDefaultResolution(new Android.Util.Size(640, 480))
                .SetIsRgba8888SurfaceRequired(true)
                .Build();
            cameraPreview.SetSurfaceProvider(previewView.SurfaceProvider);

Tested on many Android phones/tablets including Samsung A23.

Regards.

@knocte
Copy link

knocte commented Apr 16, 2024

@Eskissimo can you post your review in the PR please, not here :)

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

5 participants