teslamotors / react-native-camera-kit

A high performance, easy to use, rock solid camera library for React Native apps.
MIT License
2.46k stars 585 forks source link

Android onReadCode is only fired once #462

Open Epick362 opened 2 years ago

Epick362 commented 2 years ago

Describe the bug Barcode scanning callback only runs once on Android. Works just fine on iOS. Library version: 12.1.0

To Reproduce Steps to reproduce the behavior:

  1. Have a screen with Camera component like so
        <Camera
          style={{
            flex: 1,
            backgroundColor: 'transparent',
          }}
          cameraType={CameraType.Back}
          flashMode={'auto'}
          focusMode={'on'}
          zoomMode={'on'}
          scanBarcode={true}
          onReadCode={onScan}
        />
  2. Scan a QR/barcode
  3. Callback executed
  4. wait a few seconds
  5. try scanning any other QR code
  6. Callback not executed

Expected behavior Callback gets executed multiple times.

Smartphone (please complete the following information):

Epick362 commented 2 years ago

I tried downgrading and this issue is happening since version 10.0.0

DibyajyotiMishra commented 2 years ago

Never found that happening. Can you describe further ?

Epick362 commented 2 years ago

Callback function onScan() passed to prop onReadCode is only fired on the first detection of a QR code and then stops working. This is happening only on Android devices. After downgrading to 10.0.0 the callback works as expected multiple times.

Maxth commented 2 years ago

We are experiencing this as well. Same smartphone model Samsung Galaxy A12.

hardikcc commented 2 years ago

Anyone able to solve this issue?

saaymeen commented 2 years ago

Experiencing this issue as well, Galaxy Note 9 & S21.

hardikcc commented 2 years ago

Anyone had any luck on this?

SteveWatson75 commented 1 year ago

Also experiencing this, tried to force reload the component and set a navigation focus listener but still doesn't work

https://user-images.githubusercontent.com/88657039/196120702-8e94f64e-d40d-487d-a192-6575e1587dc6.mp4

DamianJudek commented 1 year ago

Hi guys, I think I found some workaround. In my case the onReadCode callback was also fired once. I'm using React Navigation and I have a barcode scanning feature on one screen. So I decided to use the useIsFocused hook and unmount the camera when I leave the screen. Now the scan works as expected. It seems like Camera Screen lost the reference to onReadCode callback after scanning/refreshing the state. Maybe this will be a clue that will help you.

export const Camera = ({navigation}: any) => {
  const isFocused = useIsFocused();
  const [barcode, setBarcode] = useState<string>('');

  const handleBarcodeScan = (e: any) => {
    setBarcode(e.nativeEvent.codeStringValue);
  };

  return isFocused ? (
    <Layout>
      <CameraContainer>
        <CameraScreen
          style={{flex: 1}}
          scanBarcode={true}
          onReadCode={handleBarcodeScan}
          showFrame={true}
          laserColor="darkorange"
          frameColor="darkorange"
          hideControls={true}
        />
      </CameraContainer>
    </Layout>
  ) : null;
};
danyzor commented 1 year ago

@DamianJudek , worked for me, thanks a lot! :)

Epick362 commented 1 year ago

Thanks @DamianJudek! This seems like a good workaround in the meantime.