Giter Club home page Giter Club logo

react-qr-scanner's Introduction

npm version

Introduction

A React component for reading QR codes from the webcam. It uses the WebRTC standards for reading webcam data and jsQR is used for detecting QR codes in that data. To optimise the speed and experience, a web-worker is used to offload the heavy QR code algorithm on a separate process. The web worker is inlined and loaded on creation of the component. Thanks to initial repo https://github.com/JodusNodus/react-qr-reader

Demo

kybarg.github.io/react-qr-scanner/

Known Issues

  • Due to browser implementations the camera can only be accessed over https or localhost.
  • Not compatible with macOS/iOS Safari. Use legacyMode to support these platforms.
  • In Firefox a prompt will be shown to the user asking which camera to use, so facingMode will not affect it.

Install

npm install --save react-qr-scanner

Example

import React, { Component } from 'react'
import QrReader from 'react-qr-scanner'

class Test extends Component {
  constructor(props){
    super(props)
    this.state = {
      delay: 100,
      result: 'No result',
    }

    this.handleScan = this.handleScan.bind(this)
  }
  handleScan(data){
    this.setState({
      result: data,
    })
  }
  handleError(err){
    console.error(err)
  }
  render(){
    const previewStyle = {
      height: 240,
      width: 320,
    }

    return(
      <div>
        <QrReader
          delay={this.state.delay}
          style={previewStyle}
          onError={this.handleError}
          onScan={this.handleScan}
          />
        <p>{this.state.result}</p>
      </div>
    )
  }
}

Props

onScan

Type: function, Required, Argument: result

Scan event handler. Called every scan with the decoded value or null if no QR code was found.

onError

Type: function, Required, Argument: error

Function to call when an error occurs such as:

  • Not supported platform
  • The lack of available devices

onLoad

Type: function, Optional

Called when the component is ready for use.

onImageLoad

Type: function, Optional, Argument: React img onLoad event

Called when the image in legacyMode is loaded.

delay

Type: number, Optional, Default: 500

The delay between scans in milliseconds. To disable the interval pass in false.

facingMode

Type: string, Optional

Specify which camera direction should be used (if available). Options: front and rear.

legacyMode

Type: boolean, Optional, Default: false.

If the device does not allow camera access (e.g. IOS Browsers, Safari) you can enable legacyMode to allow the user to take a picture (On a mobile device) or use an existing one. To trigger the image dialog just call the method openImageDialog from the parent component. Warning You must call the method from a user action (eg. click event on some element).

maxImageSize

Type: number, Optional, Default: 1500.

If legacyMode is active then the image will be downscaled to the given value while keepings its aspect ratio. Allowing larger images will increase the accuracy but it will also slow down the processing time.

style

Type: object, Optional

Styling for the preview element. This will be a video or an img when legacymode is true. Warning The preview will keep its aspect ratio, to disable this set the CSS property objectFit to fill.

className

Type: string, Optional

ClassName for the container element.

chooseDeviceId

Type: function, Optional, Arguments: (1) video devices matching facingMode, (2) all video devices

Called when choosing which device to use for scanning. By default chooses the first video device matching facingMode, if no devices match the first video device found is choosen.

initialStream

Type: MediaStream, Optional

Existing MediaStream to use initially.

Dev

Install dependencies

npm install

Build

npm run build

Demo

npm run storybook

Test

npm test

Linting

npm run lint

Tested platforms

  • Chrome 56 and Firefox 53 on macOs 10.12
  • Chrome 55 and Firefox 50 on Android 6.0.1

License

The MIT License (MIT)

Copyright (c) 2017 Thomas Billiet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

react-qr-scanner's People

Contributors

danielcrisp avatar fjogeleit avatar kybarg avatar niraj-patel avatar pedropalhari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-qr-scanner's Issues

Update example to hooks

Upgrade both the example code in example.jsand the README to a modern React component with hooks.

Issue with TypeScript

Hey, I'm kind of new to TypeScript and I'm having trouble with the getting the types working.
I notice that the project has types defined but when I install the library with npm install --save react-qr-scanner the types are not getting picked up.

I've also tried npm i --save-dev @types/react-qr-scanner but there's no reason to do that since the types are already defined in the project.

I managed to get it working for the most part by manually moving the types defined here to my local development environment.
Is this a bug or am I just missing something here?

on a side note I have seen the types being generally defined under lib/ in the package.json's "types", I'm not sure if this helps or if it is in anyway related to the problem I'm facing.

pls elp, thanks.

This is a fork of JodusNodus / react-qr-reader. Why have you not credited the original author anywhere?

It's good practice and helps prevent library fragmentation in the NPM ecosystem:

  • If you are creating a new codebase with features not related to the original app, do so and include the library in your package.json

  • If you are augmenting or fixing bugs in the original library, fork it and create PRs back to the original repo.

Publishing another users' code to NPM just makes things more complicated for everyone. At the very least, give credit to the original author.

react-qr-reader

Deprecated React lifecycle methods

Hello,

When using your components with React 16.11, we got the following warning:

Warning: componentWillReceiveProps has been renamed, and is not recommended for use. See https://fb.me/react-unsafe-component-lifecycles for details.

* Move data fetching code or side effects to componentDidUpdate.
* If you're updating state whenever props change, refactor your code to use memoization techniques or move it to static getDerivedStateFromProps. Learn more at: https://fb.me/react-derived-state
* Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps to suppress this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run `npx react-codemod rename-unsafe-lifecycles` in your project source folder.

Please update the following components: Reader

More information : https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#migrating-from-legacy-lifecycles

Thank you.

Certain values don't scan

We have 5000 cards with QR codes in my company, but we have discovered that that these 2 codes are not recognized by the scanner.

U2FsdGVkX1+T7M5JW2JP:K:sU4xEcGzKu5RS7a:dY6U=
U2FsdGVkX1+T7M5JW2JP/K/sU4xEcGzKu5RS7a/dY6U=

U2FsdGVkX19AD0SQUSYG7RLiVIiaESvFHOKJY+o7cNk=
U2FsdGVkX19AD0SQUSYG7RLiVIiaESvFHOKJY+o7cNk=

Strangly, they work with other scanners like Instascan

FacingMode "rear" not working on latest versions iOS (14.4.2) and Safari (14.0) for me?

I've come on a project where they have used your library and there's some last issues to fix, but one where it's specifically for your library, so I hope to find some help here.

The problem we're facing is that iOS users complain that the "rear" camera (facingMode) isn't working. I've looked in the code, and it's pretty straightforward: they're just using a bool check on "facingMode" to determine which camera to show ("front" or "rear"). Users on Android (and myself) however can do this in Google Chrome, so the functionality/code seems correct.

I've received notice from the other people (not devs, but PM/analists etc.) on my project that this was tested, months ago, and was found to be working on iOS devices they tested on (i have no clue which versions etc).

Can anyone help me trying to figure out how to find the issue with this problem and how to fix it? Do others also have this problem?

I've also checked out the demo link here: https://kybarg.github.io/react-qr-scanner/?path=/story/qr-reader--choose-camera , on n iOS 14.4.2 device, latest versions etc and in this demo I however CAN change my camera, but in our app using "facingMode" it seems to not work? Is the source code of this demo example available?

Alt image tag

When running an accessibility scan on an app I am working on using your scanner I got dinged for an tag without an alt description.

I propose that either a default alt tag is included, or it can become a prop to let people do it however they'd like.

If you would like I would be happy to do a quick PR for this, just let me know.

Console Error when no camera device detected

@kybarg,
Case:
When there is no camera device attached to a system or either has been disabled manually by user.

Description:
It shows console error that TypeError: NoVideoInputDevicesError is not a constructor. I am attaching screenshot for you reference. Please fix it ASAP.
Console Error Screenshot:
Screenshot from 2020-08-30 11-23-05

Issues with Next.js

Hello - I think this fork does SSR, right?

When I import it like so:

import QrReader from 'react-qr-scanner';

I get this issue: SyntaxError: Cannot use import statement outside a module pointing to > 1 | module.exports = require("react-qr-scanner");

I looked through the source and can't seem to find why it would think it's cjs. Any ideas?

It does work if I use next.js dynamic import, but then that that annoy as it re-imports it when it re-renders.

const QrReader = dynamic(() => import('react-qr-scanner'));

facingMode problem

Hello and thank you for giving this repo a new boost.
It's quite working good except the
facingMode property isn't working through various devices. It's always the front camera which is active despite the prop is set to rear. Any suggestion?

<QrReader
     facingMode="rear"
     delay={500}
     onError={handleError}
     onScan={handleScan}
     style={{ width: '100%' }}
/>

Support for SSR?

I am using Gatsby which uses SSR.

I have used a similar library react-qr-reader in which they have listed in known issues that it won't support SSR.

Since known issues of this library doesn't mention anything about it. I was wondering if it actually supports SSR.

ROADMAP v1.0

This modules popularity is growing, and while currently it satisfies need of my use case, I can see that some people struggle to integrate this into their project. So I want to define goals for V1, where everyone could be happy😊
Work can be tracked at next branch

Progress

  • Minimize dependencies, WebRTC-adapter is optional #34
  • Turn camera off when unmounting component #34
  • Add ability to pass plain constraints to component #34
  • Camera scanner as separate component #34
  • Legacy image scanner as separate component
  • Enable/disable processing swithch (?)
  • Full mobile devices support (?)
  • TypeScript typings
  • Demo and documentation
  • React 16 support

Support React 17

Hi there,

it would be great to add React 17 to peer dependencies, should be painlees as there should not be relevant breaking changes.

Thanks for this work

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.