svelte-barcode-scanner
Advanced tools
Comparing version 0.1.2 to 0.1.3
import type { BarcodeDetector, DetectedBarcode } from 'barcode-detector/pure'; | ||
import type { ROI, Track } from './types.js'; | ||
export declare function startCamera(video: HTMLVideoElement, stream: MediaStream, constraints: MediaTrackConstraints): Promise<{ | ||
/** | ||
* Starts the camera and streams the video to the specified HTMLVideoElement. | ||
* | ||
* @param video - The HTMLVideoElement to display the camera stream. | ||
* @param stream - The MediaStream object representing the camera stream. | ||
* @param constraints - The MediaTrackConstraints to apply to the camera stream. | ||
* @returns The MediaStream object representing the camera stream. | ||
* @throws Error if camera access is not available in a secure context or if the Stream API is not supported. | ||
*/ | ||
export declare function startCamera(video: HTMLVideoElement, stream: MediaStream, constraints: MediaTrackConstraints): Promise<MediaStream>; | ||
/** | ||
* Retrieves the capabilities of the video track from the given MediaStream. | ||
* | ||
* @param stream The MediaStream containing the video track. | ||
* @returns An object containing the capabilities of the video track. | ||
*/ | ||
export declare function getCapabilities(stream: MediaStream): { | ||
capabilities: MediaTrackCapabilities; | ||
}>; | ||
export declare function stopCamera(stream: MediaStream): void; | ||
}; | ||
/** | ||
* Sets up the canvas for barcode scanning. | ||
* | ||
* @param video - The HTMLVideoElement representing the video stream. | ||
* @param camera - The HTMLCanvasElement used for rendering the camera feed. | ||
* @param overlay - The HTMLCanvasElement used for rendering the barcode overlay. | ||
* @returns An object containing the CanvasRenderingContext2D for the camera and overlay canvases. | ||
*/ | ||
export declare function setupCanvas(video: HTMLVideoElement, camera: HTMLCanvasElement, overlay: HTMLCanvasElement): { | ||
@@ -11,3 +34,23 @@ ctxCamera: CanvasRenderingContext2D; | ||
}; | ||
/** | ||
* Draws the video onto the canvas (with object-fit: cover transformation). | ||
* | ||
* @param ctxCamera - The 2D rendering context of the camera canvas. | ||
* @param camera - The HTML canvas element representing the camera. | ||
* @param video - The HTML video element containing the video feed. | ||
*/ | ||
export declare function drawVideo(ctxCamera: CanvasRenderingContext2D, camera: HTMLCanvasElement, video: HTMLVideoElement): void; | ||
/** | ||
* Detects barcodes within the specified regions of interest (ROIs) in a canvas. | ||
* | ||
* @param detector - The barcode detector instance used for barcode detection. | ||
* @param ctxCamera - The 2D rendering context of the camera canvas. | ||
* @param camera - The HTML canvas element representing the camera feed. | ||
* @param ctxOverlay - The 2D rendering context of the overlay canvas. | ||
* @param overlay - The HTML canvas element representing the overlay. | ||
* @param rois - An array of regions of interest (ROIs) where barcodes will be detected. | ||
* @param showROIS - A boolean indicating whether to display the ROIs on the overlay. | ||
* @param track - An optional callback function for tracking detected barcodes. | ||
* @returns A promise that resolves to an array of detected barcodes. | ||
*/ | ||
export declare function detect(detector: BarcodeDetector, ctxCamera: CanvasRenderingContext2D, camera: HTMLCanvasElement, ctxOverlay: CanvasRenderingContext2D, overlay: HTMLCanvasElement, rois: ROI[], showROIS: boolean, track: Track | undefined): Promise<DetectedBarcode[]>; |
@@ -0,1 +1,10 @@ | ||
/** | ||
* Starts the camera and streams the video to the specified HTMLVideoElement. | ||
* | ||
* @param video - The HTMLVideoElement to display the camera stream. | ||
* @param stream - The MediaStream object representing the camera stream. | ||
* @param constraints - The MediaTrackConstraints to apply to the camera stream. | ||
* @returns The MediaStream object representing the camera stream. | ||
* @throws Error if camera access is not available in a secure context or if the Stream API is not supported. | ||
*/ | ||
export async function startCamera(video, stream, constraints) { | ||
@@ -23,2 +32,11 @@ if (window.isSecureContext !== true) { | ||
}); | ||
return stream; | ||
} | ||
/** | ||
* Retrieves the capabilities of the video track from the given MediaStream. | ||
* | ||
* @param stream The MediaStream containing the video track. | ||
* @returns An object containing the capabilities of the video track. | ||
*/ | ||
export function getCapabilities(stream) { | ||
const [track] = stream.getVideoTracks(); | ||
@@ -28,8 +46,2 @@ const capabilities = track.getCapabilities(); | ||
} | ||
export function stopCamera(stream) { | ||
for (const track of stream.getTracks()) { | ||
stream.removeTrack(track); | ||
track.stop(); | ||
} | ||
} | ||
function objectFitVideoTransform(video) { | ||
@@ -55,2 +67,10 @@ const matrix = new DOMMatrix(); | ||
} | ||
/** | ||
* Sets up the canvas for barcode scanning. | ||
* | ||
* @param video - The HTMLVideoElement representing the video stream. | ||
* @param camera - The HTMLCanvasElement used for rendering the camera feed. | ||
* @param overlay - The HTMLCanvasElement used for rendering the barcode overlay. | ||
* @returns An object containing the CanvasRenderingContext2D for the camera and overlay canvases. | ||
*/ | ||
export function setupCanvas(video, camera, overlay) { | ||
@@ -72,2 +92,9 @@ const ctxCamera = camera.getContext('2d', { | ||
} | ||
/** | ||
* Draws the video onto the canvas (with object-fit: cover transformation). | ||
* | ||
* @param ctxCamera - The 2D rendering context of the camera canvas. | ||
* @param camera - The HTML canvas element representing the camera. | ||
* @param video - The HTML video element containing the video feed. | ||
*/ | ||
export function drawVideo(ctxCamera, camera, video) { | ||
@@ -78,2 +105,15 @@ const { x, y, width, height } = objectFitVideoTransform(video); | ||
} | ||
/** | ||
* Detects barcodes within the specified regions of interest (ROIs) in a canvas. | ||
* | ||
* @param detector - The barcode detector instance used for barcode detection. | ||
* @param ctxCamera - The 2D rendering context of the camera canvas. | ||
* @param camera - The HTML canvas element representing the camera feed. | ||
* @param ctxOverlay - The 2D rendering context of the overlay canvas. | ||
* @param overlay - The HTML canvas element representing the overlay. | ||
* @param rois - An array of regions of interest (ROIs) where barcodes will be detected. | ||
* @param showROIS - A boolean indicating whether to display the ROIs on the overlay. | ||
* @param track - An optional callback function for tracking detected barcodes. | ||
* @returns A promise that resolves to an array of detected barcodes. | ||
*/ | ||
export async function detect(detector, ctxCamera, camera, ctxOverlay, overlay, rois, showROIS, track) { | ||
@@ -80,0 +120,0 @@ ctxOverlay.clearRect(0, 0, overlay.width, overlay.height); |
@@ -7,7 +7,27 @@ import { SvelteComponent } from "svelte"; | ||
props: { | ||
formats?: BarcodeFormat[] | undefined; | ||
constraints?: MediaTrackConstraints | undefined; | ||
ROIs?: ROI[] | undefined; | ||
showROIs?: boolean | undefined; | ||
track?: Track | undefined; | ||
/** | ||
* Formats to detect. | ||
* | ||
* @default undefined (all formats) | ||
*/ formats?: BarcodeFormat[] | undefined; | ||
/** | ||
* MediaTrackConstraints to use when requesting the camera. | ||
* | ||
* @default { facingMode: 'environment' } (rear camera) | ||
*/ constraints?: MediaTrackConstraints | undefined; | ||
/** | ||
* Regions of interests (ROIs) to scan for barcodes. | ||
* | ||
* @default [{ x: 0, y: 0, width: 1, height: 1 }] (full frame) | ||
*/ ROIs?: ROI[] | undefined; | ||
/** | ||
* Whether to show the ROIs on the camera feed. | ||
* | ||
* @default false | ||
*/ showROIs?: boolean | undefined; | ||
/** | ||
* Whether and how to track barcodes on the camera overlay. | ||
* | ||
* @default undefined (no tracking) | ||
*/ track?: Track | undefined; | ||
}; | ||
@@ -14,0 +34,0 @@ events: { |
import type { DetectedBarcode } from 'barcode-detector/pure'; | ||
/** | ||
* Represents a region of interest (ROI) with its position and dimensions. | ||
*/ | ||
export type ROI = { | ||
@@ -8,6 +11,17 @@ x: number; | ||
}; | ||
/** | ||
* Represents the events emitted by the barcode scanner. | ||
*/ | ||
export type Events = { | ||
/** | ||
* Event emitted when one or more barcodes are detected. | ||
* @param detections - The detected barcodes. | ||
*/ | ||
detect: { | ||
detections: DetectedBarcode[]; | ||
}; | ||
/** | ||
* Event emitted when the barcode scanner is initialized. | ||
* @param capabilities - The media track capabilities. | ||
*/ | ||
init: { | ||
@@ -17,2 +31,7 @@ capabilities: MediaTrackCapabilities; | ||
}; | ||
/** | ||
* Represents a function that tracks detected barcodes and renders them on the overlay canvas. | ||
* @param detections - An array of detected barcodes. | ||
* @param ctxOverlay - The rendering context of the canvas. | ||
*/ | ||
export type Track = (detections: DetectedBarcode[], ctxOverlay: CanvasRenderingContext2D) => void; |
{ | ||
"name": "svelte-barcode-scanner", | ||
"repository": "github:ollema/svelte-barcode-scanner", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"exports": { | ||
@@ -6,0 +6,0 @@ ".": { |
Sorry, the diff of this file is not supported yet
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
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
19319
289