diff --git a/README.md b/README.md index 9268413..d0ac70a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ With this package, users can quickly and easily scan barcodes and QR codes with If you want to provide your React Native app the ability to read barcodes and QR codes, you should definitely give this package some thought. -The `@pushpendersingh/react-native-scanner` package also includes a flashlight feature that can be turned on and off. This can be useful when scanning QR codes in low light conditions. +The `@pushpendersingh/react-native-scanner` package also includes a flashlight feature that can be turned on and off. This can be useful when scanning QR codes & barcodes in low light conditions. ## Getting started @@ -77,68 +77,162 @@ To use @pushpendersingh/react-native-scanner, `import` the `@pushpendersingh/rea Here is an example of basic usage: ```js -import React, { useEffect, useState } from 'react'; +import React, {useEffect, useRef, useState} from 'react'; import { Alert, Platform, - useWindowDimensions, Text, - SafeAreaView + SafeAreaView, + Button, + View, + StyleSheet, } from 'react-native'; -import { request, PERMISSIONS, openSettings, RESULTS } from 'react-native-permissions'; -import { ReactNativeScannerView } from "@pushpendersingh/react-native-scanner"; +import { + request, + PERMISSIONS, + openSettings, + RESULTS, +} from 'react-native-permissions'; // For camera permission +import { + Commands, + ReactNativeScannerView, +} from '@pushpendersingh/react-native-scanner'; export default function App() { - - const { height, width } = useWindowDimensions(); - const [isCameraPermissionGranted, setIsCameraPermissionGranted] = useState(false); + const scannerRef = useRef(null); + const [isCameraPermissionGranted, setIsCameraPermissionGranted] = + useState(false); + const [isActive, setIsActive] = useState(true); + const [scannedData, setScannedData] = useState(null); useEffect(() => { checkCameraPermission(); }, []); + const handleBarcodeScanned = event => { + const {data, bounds, type} = event?.nativeEvent; + setScannedData({data, bounds, type}); + console.log('Barcode scanned:', data, bounds, type); + }; + + // Pause the camera after barcode / QR code is scanned + const pauseScanning = () => { + if (scannerRef?.current) { + Commands.pauseScanning(scannerRef?.current); + console.log('Camera preview paused'); + } + }; + + // Resume the camera after barcode / QR code is scanned + const resumeScanning = () => { + if (scannerRef?.current) { + Commands.resumeScanning(scannerRef?.current); + console.log('Camera preview resumed'); + } + }; + const checkCameraPermission = async () => { - request(Platform.OS === 'ios' ? PERMISSIONS.IOS.CAMERA : PERMISSIONS.ANDROID.CAMERA) - .then(async (result: any) => { - switch (result) { - case RESULTS.UNAVAILABLE: - // console.log('This feature is not available (on this device / in this context)'); - break; - case RESULTS.DENIED: - Alert.alert("Permission Denied", "You need to grant camera permission first"); - openSettings(); - break; - case RESULTS.GRANTED: - setIsCameraPermissionGranted(true); - break; - case RESULTS.BLOCKED: - Alert.alert("Permission Blocked", "You need to grant camera permission first"); - openSettings(); - break; - } - }) + request( + Platform.OS === 'ios' + ? PERMISSIONS.IOS.CAMERA + : PERMISSIONS.ANDROID.CAMERA, + ).then(async (result: any) => { + switch (result) { + case RESULTS.UNAVAILABLE: + // console.log('This feature is not available (on this device / in this context)'); + break; + case RESULTS.DENIED: + Alert.alert( + 'Permission Denied', + 'You need to grant camera permission first', + ); + openSettings(); + break; + case RESULTS.GRANTED: + setIsCameraPermissionGranted(true); + break; + case RESULTS.BLOCKED: + Alert.alert( + 'Permission Blocked', + 'You need to grant camera permission first', + ); + openSettings(); + break; + } + }); }; if (isCameraPermissionGranted) { return ( - - { - console.log(value.nativeEvent); - }} - /> + + {isActive && ( + + )} + + +