mrousavy / react-native-vision-camera

📸 A powerful, high-performance React Native Camera library.
https://react-native-vision-camera.com
MIT License
7.51k stars 1.09k forks source link

Barcode scanner returns different strings in Android an iOS when scanning a UPC-A format code #2118

Closed nthchild1 closed 9 months ago

nthchild1 commented 1 year ago

What's happening?

I am trying to scan upc-a (12 digit) codes which are not directly supported by this library. However, this library does support ean-13 which is a superset standard with an extra digit (13 digit). You can convert any upc-a code to an ean-13 code by just adding a zero to the beginning of the string.

The problem is that, when scanning an upc-a code with the ean-13 codeType enabled, the scanner returns a 13 digit ean-13 code as expected for iOS (it is adding an extra zero to the start of the string). It seems that in Android it is not adding a zero to the start of the string and is returning the original 12 digit code, resulting string is different for each platform.

For example:

Scanning the following code:

image

returns 050000463916 in Android and 0050000463916 in iOS

Reproduceable Code

import 'react-native-reanimated';
import React, { useEffect } from 'react';
import { StyleSheet, View } from 'react-native';
import { useIsFocused } from '@react-navigation/native';
import { Camera, useCameraDevice, useCodeScanner } from 'react-native-vision-camera';

const ScannerScreen = () => {
  const isFocused = useIsFocused();
  const device = useCameraDevice('back');

  const codeScanner = useCodeScanner({
    codeTypes: ['ean-13'],
    onCodeScanned: (codes) => {
      console.log(codes);
    },
  });

  useEffect(() => {
    void Camera.getCameraPermissionStatus();
  }, []);

  return (
    <>
      <View style={{ flex: 1 }}>
        {isFocused && device && (
          <Camera
            style={StyleSheet.absoluteFillObject}
            device={device}
            isActive={!!device}
            codeScanner={codeScanner}
          />
        )}
      </View>
    </>
  );
};

export default ScannerScreen;

Relevant log output

No logs

Camera Device

Any camera device

Device

Any iOS or Android smartphone

VisionCamera Version

3.3.1

Can you reproduce this issue in the VisionCamera Example app?

Yes, I can reproduce the same issue in the Example app here

Additional information

gary-cohen commented 1 year ago

Yes, it would be nice if the lib supported upc-a especially since BarcodeScannerOptions supports it.

Gambitboy commented 11 months ago

Same, I also need ability to scan upc_a, coming from vision-camera-code-scanner which was deprecated in favour of v3

jimsideout commented 11 months ago

Same issue. Seems clunky to need to remove the leading 0 in iOS.

Gambitboy commented 11 months ago

I managed to get this working by modifying the android files to include upc-a and running patch-package. iOS already has the ability to scan upc-a because of ean-13, I just do a check to include the extra digit.

marcshilling commented 10 months ago

@Gambitboy can you share your patch file please?

Gambitboy commented 10 months ago

@marcshilling Here is the patch file. Its quite a big. Not sure why, as it wasn't a lot of changes. I still have a type issue for upc-a which I haven't resolved yet.

https://gist.github.com/Gambitboy/0a2afa2485fd7c285a52b093de1c32b2

arisyo13 commented 10 months ago

I have a similar issue. On android it can scan multiple barcodes and returns an array with all the barcodes scanned but on ios it returns only one item inside the array. Screenshot 2024-01-04 at 11 20 55

mrousavy commented 9 months ago

On iOS I use the native QR scanner from Apple, on Android I use Google's MLKit model. If there are differences, there's nothing I can do.

You can instead write a custom Frame Processor Plugin to use the same libs on both devices, or something like ZXing.

Couzix commented 8 months ago

Hi ! MLKit actually provides support for upc-a ! "The following formats are supported: [...] UPC-A (FORMAT_UPC_A)". Sorry for tagging you @mrousavy, but do you consider in supporting it in your lib ? Thanks a lot !!

mrousavy commented 8 months ago

Hey @Couzix - Apple's Vision library doesn't support UPC-A though, so it would not be a cross-platform feature, which I like to avoid.

Gambitboy commented 8 months ago

When I read through the apple docs it said it indirectly supported upc-a through ean. So once I added the missing code for android both devices types were able to scan a upc-a code.

mrousavy commented 8 months ago

Oh, interesting. Well I guess I could add those types and just mark them as @platform Android then..

Gambitboy commented 8 months ago

I'll quickly try to find the docs mentioning it, also since its working on my project. I don't mind doing a pull request. The only bit I never got to finish was type checking the UPC-A for android

Gambitboy commented 8 months ago

I think it was here.