npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pushpendersingh/react-native-scanner

v1.2.0

Published

A QR code & Barcode Scanner for React Native Projects.

Downloads

733

Readme

@pushpendersingh/react-native-scanner

A QR code & Barcode Scanner for React Native Projects.

For React Native developers that need to scan barcodes and QR codes in their apps, this package is a useful resource. It supports React Native's new Fabric Native architecture and was created in Kotlin and Objective-C.

With this package, users can quickly and easily scan barcodes and QR codes with their device's camera. Using this package, several types of codes can be scanned, and it is simple to use.

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.

Getting started

Requirements

IOS

Open your project's Info.plist and add the following lines inside the outermost <dict> tag:

<key>NSCameraUsageDescription</key>
<string>Your message to user when the camera is accessed for the first time</string>

Open your project's Podfile and add enable the new architecture:

:fabric_enabled => true,

Run below command to enable the new architecture in IOS folder

bundle install && RCT_NEW_ARCH_ENABLED=1 bundle exec pod install

Android

Open your project's AndroidManifest.xml and add the following lines inside the <manifest> tag:

<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera.any" />

Open your project's gradle.properties and add enable the new architecture:

newArchEnabled=true

To install and start using @pushpendersingh/react-native-scanner

npm install @pushpendersingh/react-native-scanner

Supported Formats

| 1D product | 1D industrial | 2D | |:----------------------|:--------------|:---------------| | UPC-A | Code 39 | QR Code | | UPC-E | Code 93 | Data Matrix | | EAN-8 | Code 128 | Aztec | | EAN-13 | Codabar | PDF 417 | | | ITF | |

Usage

To use @pushpendersingh/react-native-scanner, import the @pushpendersingh/react-native-scanner module and use the <ReactNativeScannerView /> tag. More usage examples can be seen under the examples/ folder.

Here is an example of basic usage:

import React, { useEffect, useState } from 'react';
import {
  Alert,
  Platform,
  useWindowDimensions,
  Text,
  SafeAreaView
} from 'react-native';

import { request, PERMISSIONS, openSettings, RESULTS } from 'react-native-permissions';
import { ReactNativeScannerView } from "@pushpendersingh/react-native-scanner";

export default function App() {

  const { height, width } = useWindowDimensions();
  const [isCameraPermissionGranted, setIsCameraPermissionGranted] = useState(false);

  useEffect(() => {
    checkCameraPermission();
  }, []);

  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;
        }
      })
  };

  if (isCameraPermissionGranted) {
    return (
      <SafeAreaView style={{ flex: 1 }}>
        <ReactNativeScannerView
          style={{ height, width }}
          onQrScanned={(value: any) => {
            console.log(value.nativeEvent);
          }}
        />
      </SafeAreaView>
    );
  } else {
    return (
      <Text style={{ fontSize: 30, color: 'red' }}>
        You need to grant camera permission first
      </Text>
    );
  }
}

Flashlight Feature

To use the flashlight feature, add the following code to your project:

import React, {useEffect, useRef, useState} from 'react';
import {
  Alert,
  Platform,
  useWindowDimensions,
  Text,
  SafeAreaView,
  TouchableOpacity,
} from 'react-native';

import {
  request,
  PERMISSIONS,
  openSettings,
  RESULTS,
} from 'react-native-permissions';
import {
  ReactNativeScannerView,
  Commands,
} from '@pushpendersingh/react-native-scanner';

export default function App() {
  const {height, width} = useWindowDimensions();
  const [isCameraPermissionGranted, setIsCameraPermissionGranted] =
    useState(false);
  const cameraRef = useRef(null);

  useEffect(() => {
    checkCameraPermission();

    return () => {
      if(cameraRef.current) {
        Commands.releaseCamera(cameraRef.current);
      }
    };
  }, []);

  const enableFlashlight = () => {
    Commands.enableFlashlight(cameraRef.current);
  };

  const disableFlashlight = () => {
    Commands.disableFlashlight(cameraRef.current);
  };

  const checkCameraPermission = async () => {
    request(
      Platform.OS === 'ios'
        ? PERMISSIONS.IOS.CAMERA
        : PERMISSIONS.ANDROID.CAMERA,
    ).then(async (result: any) => {
      switch (result) {
        case RESULTS.UNAVAILABLE:
          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 (
      <SafeAreaView style={{flex: 1}}>
        <ReactNativeScannerView
          ref={ref => (cameraRef.current = ref)}
          style={{height, width}}
          onQrScanned={(value: any) => {
            console.log(value.nativeEvent);
          }}
        />

        <TouchableOpacity
          style={{
            position: 'absolute',
            bottom: 20,
            left: 20,
            padding: 10,
            backgroundColor: 'blue',
            borderRadius: 10,
          }}
          onPress={enableFlashlight}>
          <Text>Turn ON</Text>
        </TouchableOpacity>

        <TouchableOpacity
          style={{
            position: 'absolute',
            bottom: 20,
            right: 20,
            padding: 10,
            backgroundColor: 'blue',
            borderRadius: 10,
          }}
          onPress={disableFlashlight}>
          <Text>Turn OFF</Text>
        </TouchableOpacity>
      </SafeAreaView>
    );
  } else {
    return (
      <Text style={{fontSize: 30, color: 'red'}}>
        You need to grant camera permission first
      </Text>
    );
  }
}

Props

onQrScanned (required)

propType: func.isRequired default: (e) => (console.log('QR code scanned!', e))

In the event that a QR code or barcode is detected in the camera's view, this specified method will be called.

Native Commands

The @pushpendersingh/react-native-scanner package also includes a few native commands that can be used to control the camera and flashlight.

Commands

enableFlashlight

This command is used to turn on the flashlight.

if(cameraRef.current) {
  Commands.enableFlashlight(cameraRef.current);
}

disableFlashlight

This command is used to turn off the flashlight.

if(cameraRef.current) {
  Commands.disableFlashlight(cameraRef.current);
}

releaseCamera

This command is used to release the camera.

if(cameraRef.current) {
  Commands.releaseCamera(cameraRef.current);
}

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT