browser-barcodescanner
Advanced tools
Comparing version 1.0.2 to 1.0.3
'use strict'; | ||
async function BrowserBarcodeScannerFile(formats, file, callback) { | ||
function BarcodeScanner(callback, formats, object) { | ||
const barcodeDetector = new window.BarcodeDetector({ | ||
formats | ||
}); | ||
barcodeDetector.detect(object).then(barcodes => { | ||
callback({ | ||
res: barcodes[0].rawValue | ||
}); | ||
}); | ||
} | ||
async function BrowserBarcodeScannerFile(formats, object, callback) { | ||
// Check if the browser supports the BarcodeDetector API | ||
@@ -11,22 +21,17 @@ if (!('BarcodeDetector' in window)) throw new Error('BarcodeDetector not supported'); | ||
if (unsupportedFormats.length > 0) throw new Error(`The following formats are not supported: ${unsupportedFormats.join(', ')}`); | ||
// Convert the File using the FileReader API | ||
const reader = new FileReader(); | ||
reader.addEventListener('load', () => { | ||
const tempImage = new Image(); | ||
tempImage.src = reader.result; | ||
tempImage.onload = () => { | ||
const barcodeDetector = new window.BarcodeDetector({ | ||
formats | ||
}); | ||
barcodeDetector.detect(tempImage).then(barcodes => { | ||
callback({ | ||
res: barcodes[0].rawValue | ||
}); | ||
}); | ||
}; | ||
}); | ||
reader.readAsDataURL(file); | ||
if (object instanceof File) { | ||
/* If the object is a file, we need to convert it to a data URL */ | ||
const reader = new FileReader(); | ||
reader.addEventListener('load', () => { | ||
const tempImage = new Image(); | ||
tempImage.src = reader.result; | ||
tempImage.onload = () => BarcodeScanner(callback, formats, tempImage); | ||
}); | ||
reader.readAsDataURL(object); | ||
} else if (object instanceof HTMLVideoElement || object instanceof HTMLImageElement || object instanceof HTMLCanvasElement) { | ||
/* If the object is a video, image or canvas, we can use it directly */ | ||
BarcodeScanner(callback, formats, object); | ||
} | ||
} | ||
exports.BrowserBarcodeScannerFile = BrowserBarcodeScannerFile; |
@@ -1,2 +0,12 @@ | ||
async function BrowserBarcodeScannerFile(formats, file, callback) { | ||
function BarcodeScanner(callback, formats, object) { | ||
const barcodeDetector = new window.BarcodeDetector({ | ||
formats | ||
}); | ||
barcodeDetector.detect(object).then(barcodes => { | ||
callback({ | ||
res: barcodes[0].rawValue | ||
}); | ||
}); | ||
} | ||
async function BrowserBarcodeScannerFile(formats, object, callback) { | ||
// Check if the browser supports the BarcodeDetector API | ||
@@ -9,22 +19,17 @@ if (!('BarcodeDetector' in window)) throw new Error('BarcodeDetector not supported'); | ||
if (unsupportedFormats.length > 0) throw new Error(`The following formats are not supported: ${unsupportedFormats.join(', ')}`); | ||
// Convert the File using the FileReader API | ||
const reader = new FileReader(); | ||
reader.addEventListener('load', () => { | ||
const tempImage = new Image(); | ||
tempImage.src = reader.result; | ||
tempImage.onload = () => { | ||
const barcodeDetector = new window.BarcodeDetector({ | ||
formats | ||
}); | ||
barcodeDetector.detect(tempImage).then(barcodes => { | ||
callback({ | ||
res: barcodes[0].rawValue | ||
}); | ||
}); | ||
}; | ||
}); | ||
reader.readAsDataURL(file); | ||
if (object instanceof File) { | ||
/* If the object is a file, we need to convert it to a data URL */ | ||
const reader = new FileReader(); | ||
reader.addEventListener('load', () => { | ||
const tempImage = new Image(); | ||
tempImage.src = reader.result; | ||
tempImage.onload = () => BarcodeScanner(callback, formats, tempImage); | ||
}); | ||
reader.readAsDataURL(object); | ||
} else if (object instanceof HTMLVideoElement || object instanceof HTMLImageElement || object instanceof HTMLCanvasElement) { | ||
/* If the object is a video, image or canvas, we can use it directly */ | ||
BarcodeScanner(callback, formats, object); | ||
} | ||
} | ||
export { BrowserBarcodeScannerFile }; |
@@ -6,4 +6,4 @@ declare global { | ||
} | ||
export declare function BrowserBarcodeScannerFile(formats: string[], file: File, callback: (result: { | ||
export declare function BrowserBarcodeScannerFile(formats: string[], object: File | HTMLVideoElement | HTMLImageElement | HTMLCanvasElement, callback: (result: { | ||
res: string; | ||
}) => void): Promise<void>; |
{ | ||
"name": "browser-barcodescanner", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A browser API based barcode scanner", | ||
@@ -16,5 +16,3 @@ "main": "dist/index.cjs.js", | ||
"dist", | ||
"README.md", | ||
"logo.png", | ||
"LICENSE" | ||
"README.md" | ||
], | ||
@@ -21,0 +19,0 @@ "license": "MIT", |
@@ -6,3 +6,3 @@ <div align="center"> | ||
<div align="center"> | ||
<img alt="logo" src="./logo.png" width="200px" /> | ||
<img alt="logo" src="https://raw.githubusercontent.com/HyunseungLee-Travis/browser-barcodescanner/main/logo.png" width="200px" /> | ||
</div> | ||
@@ -21,2 +21,3 @@ | ||
npm install browser-barcodescanner | ||
yarn add browser-barcodescanner | ||
``` | ||
@@ -42,4 +43,19 @@ | ||
For specific case example, see [example]() folder. | ||
```js | ||
import { BrowserBarcodeScannerFile } from 'browser-barcodescanner' | ||
const scanButton = document.querySelector('#scan') | ||
const video = document.querySelector('#video') | ||
scanButton.addEventListener('click', () => { | ||
navigator.mediaDevices.getUserMedia({ video: true }).then(stream => { | ||
video.srcObject = stream | ||
}) | ||
BrowserBarcodeScannerFile(['ean_13'], video, result => { | ||
// Do something with result.res | ||
}) | ||
}) | ||
``` | ||
# API | ||
@@ -46,0 +62,0 @@ |
73
71
7144
6