BarqodeDropzone
Click to upload images, drag & drop or use your camera to scan.
This component functions as a file input with the capture
attribute set to environment
which allows users to take a picture with their camera. You can also drag-and-drop image files from your desktop or images embedded into other web pages anywhere in the area the component occupies. The images are directly scanned and positive results are indicated by the onDetect
callback.
Demo
Click to upload or drop an image here
Usage
Props
formats
Type: BarcodeFormat[]
Default: ["qr_code"]
Configure the barcode formats to detect. By default, only QR codes are detected.
If you want to detect multiple formats, pass an array of formats:
Under the hood, the standard BarcodeDetector API is used. Support varies across devices, operating systems and browsers. All components will prefer to use the native implementation if available and otherwise falls back to a polyfill implementation.
Note that even if the native implementation is available, the component still might use the polyfill. For example, if the native implementation only supports the format 'qr_code'
but the you select the formats ['qr_code', 'aztec']
.
onDetect
Type: (detectedCodes: DetectedBarcode[]) => void
Callback function that is called when a barcode is detected.
It receives an array of detected barcodes, one callback per image.
If not barcode is detected, the array will be empty.
onDragover
Type: (isDraggingOver: boolean) => void
Callback function that is called when a file is dragged over the drop zone.
onError
Type: (error: Error) => void
Callback function that is called when an error occurs.
TODO: insert link to errors.
Other props
The BarqodeDropzone
component accepts all attributes that a standard input
element accepts.
By default, the following attributes are set:
type="file"
. This is required to make the input a file input. You should not change this.name="image"
. This is the name of the file input.accept="image/*"
. This restricts the file types that can be uploaded to images.capture="environment"
. This tells the browser to open the camera when the input is clicked on mobile devices. You can choose betweenuser
andenvironment
, which opens the front and back camera respectively. You can also disable this functionality by setting it tonull
.multiple
. This allows the user to upload multiple files at once. You can disable this by settings this tofalse
.
Browser Support
This component depends on the File Reader API which is widely supported in modern browsers.