@sec-ant/barcode-detector
Advanced tools
Comparing version 1.0.2 to 1.1.0
/// <reference types="dom-webcodecs" /> | ||
import { setZXingModuleOverrides } from "@sec-ant/zxing-wasm/reader"; | ||
export declare const BARCODE_DETECTOR_FORMATS: readonly ["aztec", "code_128", "code_39", "code_93", "codabar", "data_matrix", "ean_13", "ean_8", "itf", "pdf417", "qr_code", "upc_a", "upc_e", "unknown"]; | ||
import { BARCODE_DETECTOR_FORMATS } from "./utils.js"; | ||
export type BarcodeFormat = (typeof BARCODE_DETECTOR_FORMATS)[number]; | ||
@@ -8,3 +8,3 @@ export interface BarcodeDetectorOptions { | ||
} | ||
type Point2D = { | ||
export type Point2D = { | ||
x: number; | ||
@@ -26,21 +26,1 @@ y: number; | ||
export { setZXingModuleOverrides }; | ||
declare global { | ||
var BarcodeDetector: { | ||
readonly prototype: BarcodeDetector; | ||
new (barcodeDectorOptions?: BarcodeDetectorOptions): BarcodeDetector; | ||
getSupportedFormats(): Promise<readonly BarcodeFormat[]>; | ||
}; | ||
interface BarcodeDetector { | ||
detect(image: ImageBitmapSourceWebCodecs): Promise<DetectedBarcode[]>; | ||
} | ||
type BarcodeFormat = (typeof BARCODE_DETECTOR_FORMATS)[number]; | ||
interface BarcodeDetectorOptions { | ||
formats?: BarcodeFormat[]; | ||
} | ||
interface DetectedBarcode { | ||
boundingBox: DOMRectReadOnly; | ||
rawValue: string; | ||
format: BarcodeFormat; | ||
cornerPoints: [Point2D, Point2D, Point2D, Point2D]; | ||
} | ||
} |
@@ -1,1 +0,2 @@ | ||
export * from "./BarcodeDetector.js"; | ||
import "./side-effects.js"; | ||
export * from "./pure.js"; |
/// <reference types="dom-webcodecs" /> | ||
export declare const BARCODE_DETECTOR_FORMATS: readonly ["aztec", "code_128", "code_39", "code_93", "codabar", "data_matrix", "ean_13", "ean_8", "itf", "pdf417", "qr_code", "upc_a", "upc_e", "unknown"]; | ||
export declare function getIntrinsicDimensionsOfCanvasImageSource(image: CanvasImageSourceWebCodecs): { | ||
@@ -3,0 +4,0 @@ width: number; |
{ | ||
"name": "@sec-ant/barcode-detector", | ||
"private": false, | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"type": "module", | ||
@@ -11,3 +11,5 @@ "files": [ | ||
"exports": { | ||
".": "./dist/index.js" | ||
".": "./dist/index.js", | ||
"./pure": "./dist/pure.js", | ||
"./side-effects": "./dist/side-effects.js" | ||
}, | ||
@@ -14,0 +16,0 @@ "config": { |
@@ -13,8 +13,64 @@ # @sec-ant/barcode-detector | ||
You can use this package in 3 ways: | ||
### Pure Module | ||
```ts | ||
// import as side effects | ||
import "@sec-ant/barcode-detector"; | ||
// or import explicitly | ||
// import { BarcodeDetector } from "@sec-ant/barcode-detector"; | ||
import { BarcodeDetector } from "@sec-ant/barcode-detector/pure"; | ||
``` | ||
or rename the export to prevent possible namespace collisions: | ||
```ts | ||
import { BarcodeDetector as BarcodeDetectorPolyfill } from "@sec-ant/barcode-detector/pure"; | ||
``` | ||
This is useful when you don't want to pollute `globalThis`: | ||
- You just want to use a package to detect barcodes. | ||
- The runtime you're using has already provided an implementation of the Barcode Detection API but you still want this package to work. | ||
### Side Effects | ||
```ts | ||
import "@sec-ant/barcode-detector/side-effects"; | ||
``` | ||
This is useful when you just need a drop-in polyfill. | ||
If there's already an implementation of Barcode Detection API on `globalThis`, this won't take effect. Please instead use [pure module](#pure-module). | ||
### Both | ||
```ts | ||
import { BarcodeDetector } from "@sec-ant/barcode-detector"; | ||
``` | ||
This is the combination of [pure module](#pure-module) and [side effects](#side-effects). | ||
## `setZXingModuleOverrides` | ||
Apart from `BarcodeDetector`, there's also an exported function called `setZXingModuleOverrides`. This package uses [Sec-ant/zxing-wasm](https://github.com/Sec-ant/zxing-wasm) to provide the core function of reading barcodes. So there will be a `.wasm` binary file to load. By default, the path of the `.wasm` binary file is: | ||
``` | ||
https://cdn.jsdelivr.net/npm/@sec-ant/zxing-wasm@{version}/dist/reader/zxing_reader.wasm | ||
``` | ||
`setZXingModuleOverrides` is useful when you want to take control of the location where the `.wasm` binary file will be served, so you can use this package in a local network or you want to choose another CDN. You have to use this function prior to `new BarcodeDetector()` for it to take effect. For more information on how to use it, please check [the notes here](https://github.com/Sec-ant/zxing-wasm#notes). | ||
This function is also exported from the [side effects](#side-effects) subpath. | ||
```ts | ||
import { setZXingModuleOverrides } from "@sec-ant/barcode-detector/side-effects"; | ||
``` | ||
## API | ||
Please check the [spec](https://wicg.github.io/shape-detection-api/#barcode-detection-api) and [MDN doc](https://developer.mozilla.org/docs/Web/API/Barcode_Detection_API) for more information. | ||
## Example | ||
```ts | ||
import "@sec-ant/barcode-detector/side-effects"; | ||
const barcodeDetector: BarcodeDetector = new BarcodeDetector({ | ||
@@ -28,12 +84,3 @@ formats: ["qr_code"], | ||
const imageData = await createImageBitmap(imageFile).then((imageBitmap) => { | ||
const { width, height } = imageBitmap; | ||
const context = new OffscreenCanvas(width, height).getContext( | ||
"2d" | ||
) as OffscreenCanvasRenderingContext2D; | ||
context.drawImage(imageBitmap, 0, 0, width, height); | ||
return context.getImageData(0, 0, width, height); | ||
}); | ||
barcodeDetector.detect(imageData).then(console.log); | ||
barcodeDetector.detect(imageFile).then(console.log); | ||
``` |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75635
11
2110
85
5