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

@ericblade/quagga2-redux-middleware

v4.0.2

Published

Redux Middleware to make it easier to use @ericblade/quagga2

Downloads

25

Readme

quagga2-redux-middleware

Join the chat at
https://gitter.im/quaggaJS/Lobby

So, you want to use Quagga2 with Redux, perhaps in your React application? This might help you out.

This README file will hopefully get more detailed as necessary, if necessary.

What does it do?

This is a Redux middleware that provides an action that you can dispatch to asynchronously retrieve a list of video camera devices potentially suitable for use with Quagga, and an action that you can listen to in your reducers to store the device information.

Installing

npm install --save @ericblade/quagga2-redux-middleware

Adding the Middleware to your store

I'm going to be the first to admit that I'm not an expert in Redux middleware, but this is what works for me:

store.js:

import { createStore, applyMiddleware, combineReducers } from 'redux';
import QuaggaMiddleware from '@ericblade/quagga2-redux-middleware';
... import / create all your reducers ...
const rootReducer = combineReducers({ uiReducer, dataReducer, settingsReducer }); // names here are just samples
const store = createStore(rootReducer, applyMiddleware(QuaggaMiddleware));
export store;

Dispatching a request for video device information

If you're using react-redux, you should be able to do something a bit like

import { useDispatch } from 'react-redux';
import { enumerateVideoDevices } from '@ericblade/quagga2-redux-middleware';
...
const dispatch = useDispatch();
dispatch(enumerateVideoDevices());

You can also create your own middleware that dispatches (one or many) action(s) at application load/start.

Receiving the video devices in your reducer

Personally, I store the camera information in a location that is not persisted between runs of my application, in this case it's my "uiReducer". The section responsible for storing this information looks like:

import { ActionTypes as CameraActions } from '@ericblade/quagga2-redux-middleware';
...
const uiReducer = (state = initialUiState, action) => {
    switch (action.type) {
        case CameraActions.RECEIVE_VIDEO_DEVICES:
            return { ...state, cameraList: action.payload };
        ....
    }
};

export default uiReducer;

or with redux-toolkit:

const reducer = createReducer(initialUiState, builder => {
    builder.addCase(receiveVideoDevices, (state, { payload }) => { state.cameraList = payload; })
});

Your reducer will receive an array of MediaDeviceInfo objects, which is exported as type VideoDevices in this library.

Getting camera permissions

On most mobile devices and PC browsers, you must supply permission for using the Camera before actually accessing the camera. This usually occurs the first time your application requests to use the camera. BUT, on Android for Chrome, and probably other browsers, you have to have permission to use the camera before enumerating which ones are available, and the enumeration request does NOT ask permission like it does on desktop! Therefore, we provide this convenience method for doing a quick on-off cycle of the camera to prompt the user for permission. You should use this if you need to enumerate the available camera devices before a user has specifically requested to use the camera.

import { useDispatch } from 'react-redux';
import { requestCameraPermission } from '@ericblade/quagga2-redux-middleware';
...
const dispatch = useDispatch();
dispatch(requestCameraPermission());

You should eventually receive back a cameraPermissionSuccessful action or a cameraNoPermission action. If no camera capabilities are available, this may return cameraNoPermission, so you probably want to check that first.

In my project, I have a user-interface that allows the user to select a camera device based on it's label, and then that passes the associated deviceId to Quagga2's init() function, as well as storing the user's selection of deviceId into semi-permanent storage for re-use on the next application run.