1.0.15 • Published 9 months ago

@alzera/react-scanner v1.0.15

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

React QR Scanner

@alzera/react-scanner is a lightweight and simple-to-use React library for integrating QR code scanning functionality into your web applications. The primary goal of this library is to provide a hassle-free solution for developers who need a quick and efficient way to incorporate QR code scanning without the bloat.

Features

  • Barcode Formats: Support many different barcode formats, more on here.
  • Legacy: Support image input as camera fallback.
  • Low-level API: Expose low-level API to be able to create your own layout.
  • Flash/Torch: Support handling torch when ever possible.

Installation

Install @alzera/react-scanner using your preferred package manager:

npm install @alzera/react-scanner

Components

BarcodeScanner

Combined version of Scanner component and DropZone component, comes with a built-in button that allows users to switch between the scanning mode and the drop zone mode.

Basic Example

import React, { useState } from 'react';
import { BarcodeScanner } from '@alzera/react-scanner';

export default function MyComponent(){
  const [scannedData, setScannedData] = useState('');

  return (
    <div style={{ maxWidth: '500px' }}>
      <BarcodeScanner onScan={(d) => d && setScannedData(d)} />
      {scannedData && <p>Scanned Data: {scannedData}</p>}
    </div>
  );
};

Properties

NameTypeDescription
onScan*(value: string) => voidCallback function triggered when a QR code is scanned. Passes the scanned data as an argument.
onError(error: any) => voidCallback function triggered when an error occurs during scanning.
delaynumberSet the delay (in milliseconds) between scans.
aspectRatiostringSet the aspect ratio of the scanner window, using css aspect-ratio.
decoderOptionsBarcodeDetectorOptionsExposed BarcodeScanner config, more on here.
switchLabel(isScanner: boolean) => React.ReactNodeFunction to customize the label for the switch component.
dropChildrenReact.ReactNodeReact node to customize the content of the drop area.
styleReact.CSSPropertiesApply custom styles to the scanner component.
classNamestringAdd custom class names to the scanner component.

Scanner

Simple component wrapper for barcode-detector library.

Basic Example

import React, { useState } from 'react';
import { Scanner } from '@alzera/react-scanner';

export default function MyComponent(){
  const [scannedData, setScannedData] = useState('');

  return (
    <div style={{ maxWidth: '500px' }}>
      <Scanner onScan={(d) => d && setScannedData(d)} />
      {scannedData && <p>Scanned Data: {scannedData}</p>}
    </div>
  );
};

Properties

NameTypeDescription
onScan*(value: string) => voidCallback function triggered when a QR code is scanned. Passes the scanned data as an argument.
onError(error: any) => voidCallback function triggered when an error occurs during scanning.
delaynumberSet the delay (in milliseconds) between scans.
aspectRatiostringSet the aspect ratio of the scanner window, using css aspect-ratio.
decoderOptionsBarcodeDetectorOptionsExposed BarcodeScanner config, more on here.
styleReact.CSSPropertiesApply custom styles to the scanner component.
classNamestringAdd custom class names to the scanner component.

DropZone

Simple component wrapper for barcode-detector library.

Basic Example

import React, { useState } from 'react';
import { DropZone } from '@alzera/react-scanner';

export default function MyComponent(){
  const [scannedData, setScannedData] = useState('');

  return (
    <div style={{ maxWidth: '500px' }}>
      <DropZone onScan={(d) => d && setScannedData(d)} />
      {scannedData && <p>Scanned Data: {scannedData}</p>}
    </div>
  );
};

Properties

NameTypeDescription
onScan*(value: string) => voidCallback function triggered when a QR code is scanned. Passes the scanned data as an argument.
onError(error: any) => voidCallback function triggered when an error occurs during scanning.
childrenReact.ReactNodeReact node to customize the content of the drop area.
decoderOptionsBarcodeDetectorOptionsExposed BarcodeScanner config, more on here.
styleReact.CSSPropertiesApply custom styles to the scanner component.
classNamestringAdd custom class names to the scanner component.

API

useDecoder

The useDecoder hook is designed to facilitate barcode detection, returning a ref to a decoder function that can be used to decode barcodes from images.

Parameter

NameTypeDescription
optsBarcodeDetectorOptionsExposed BarcodeScanner config, more on here.

Return

TypeDescription
(imageData: ImageBitmapSourceWebCodecs) => Promise<string | null>Function that take CanvasImageSourceWebCodecs, Blob or ImageData to be processed and return the decoded string or null if failed

useCamera

The useCamera hook is designed to facilitate easy to use camera.

Parameter

NameTypeDescription
onError(error: any) => voidCallback function triggered when an error occurs during scanning.
useLastDeviceIdbooleanUse the last selected device id.
autoStartbooleanSet the initial camera state.
autoPausebooleanSet if the camera should be paused when the component is not visible.
constraints.audiobooleanSet if the camera should have audio enabled.
constraints.videobooleanSet if the camera should have video enabled.

Return

NameTypeDescription
videoReact.RefObject\Reference object for video element.
camera.capabilitiesMediaTrackCapabilities | undefinedSelected camera capabilities.
camera.stateCameraStateReflect the current camera state, starting, display, stopping, idle.
camera.torchbooleanState of torch.
device.listMediaDeviceInfo[]Reflect available devices options.
device.selectedstring | undefinedState of selected device.
device.lastSelectedstring | undefinedState of last selected device.

Contributing

We welcome contributions! Feel free to open issues, create pull requests, or provide feedback.

Happy scanning! 📷 🚀

1.0.15

9 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago