BrowserCodeReader.d.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import BinaryBitmap from '../core/BinaryBitmap';
  2. import DecodeHintType from '../core/DecodeHintType';
  3. import Reader from '../core/Reader';
  4. import Result from '../core/Result';
  5. import { DecodeContinuouslyCallback } from './DecodeContinuouslyCallback';
  6. import { HTMLVisualMediaElement } from './HTMLVisualMediaElement';
  7. import { VideoInputDevice } from './VideoInputDevice';
  8. /**
  9. * @deprecated Moving to @zxing/browser
  10. *
  11. * Base class for browser code reader.
  12. */
  13. export declare class BrowserCodeReader {
  14. protected readonly reader: Reader;
  15. protected timeBetweenScansMillis: number;
  16. protected _hints?: Map<DecodeHintType, any>;
  17. /**
  18. * If navigator is present.
  19. */
  20. get hasNavigator(): boolean;
  21. /**
  22. * If mediaDevices under navigator is supported.
  23. */
  24. get isMediaDevicesSuported(): boolean;
  25. /**
  26. * If enumerateDevices under navigator is supported.
  27. */
  28. get canEnumerateDevices(): boolean;
  29. /**
  30. * This will break the loop.
  31. */
  32. private _stopContinuousDecode;
  33. /**
  34. * This will break the loop.
  35. */
  36. private _stopAsyncDecode;
  37. /**
  38. * Delay time between decode attempts made by the scanner.
  39. */
  40. protected _timeBetweenDecodingAttempts: number;
  41. /** Time between two decoding tries in milli seconds. */
  42. get timeBetweenDecodingAttempts(): number;
  43. /**
  44. * Change the time span the decoder waits between two decoding tries.
  45. *
  46. * @param {number} millis Time between two decoding tries in milli seconds.
  47. */
  48. set timeBetweenDecodingAttempts(millis: number);
  49. /**
  50. * The HTML canvas element, used to draw the video or image's frame for decoding.
  51. */
  52. protected captureCanvas: HTMLCanvasElement;
  53. /**
  54. * The HTML canvas element context.
  55. */
  56. protected captureCanvasContext: CanvasRenderingContext2D;
  57. /**
  58. * The HTML image element, used as a fallback for the video element when decoding.
  59. */
  60. protected imageElement: HTMLImageElement;
  61. /**
  62. * Should contain the current registered listener for image loading,
  63. * used to unregister that listener when needed.
  64. */
  65. protected imageLoadedListener: EventListener;
  66. /**
  67. * The stream output from camera.
  68. */
  69. protected stream: MediaStream;
  70. /**
  71. * The HTML video element, used to display the camera stream.
  72. */
  73. protected videoElement: HTMLVideoElement;
  74. /**
  75. * Should contain the current registered listener for video loaded-metadata,
  76. * used to unregister that listener when needed.
  77. */
  78. protected videoCanPlayListener: EventListener;
  79. /**
  80. * Should contain the current registered listener for video play-ended,
  81. * used to unregister that listener when needed.
  82. */
  83. protected videoEndedListener: EventListener;
  84. /**
  85. * Should contain the current registered listener for video playing,
  86. * used to unregister that listener when needed.
  87. */
  88. protected videoPlayingEventListener: EventListener;
  89. /**
  90. * Sets the hints.
  91. */
  92. set hints(hints: Map<DecodeHintType, any>);
  93. /**
  94. * Sets the hints.
  95. */
  96. get hints(): Map<DecodeHintType, any>;
  97. /**
  98. * Creates an instance of BrowserCodeReader.
  99. * @param {Reader} reader The reader instance to decode the barcode
  100. * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent successful decode tries
  101. *
  102. * @memberOf BrowserCodeReader
  103. */
  104. constructor(reader: Reader, timeBetweenScansMillis?: number, _hints?: Map<DecodeHintType, any>);
  105. /**
  106. * Lists all the available video input devices.
  107. */
  108. listVideoInputDevices(): Promise<MediaDeviceInfo[]>;
  109. /**
  110. * Obtain the list of available devices with type 'videoinput'.
  111. *
  112. * @returns {Promise<VideoInputDevice[]>} an array of available video input devices
  113. *
  114. * @memberOf BrowserCodeReader
  115. *
  116. * @deprecated Use `listVideoInputDevices` instead.
  117. */
  118. getVideoInputDevices(): Promise<VideoInputDevice[]>;
  119. /**
  120. * Let's you find a device using it's Id.
  121. */
  122. findDeviceById(deviceId: string): Promise<MediaDeviceInfo>;
  123. /**
  124. * Decodes the barcode from the device specified by deviceId while showing the video in the specified video element.
  125. *
  126. * @param deviceId the id of one of the devices obtained after calling getVideoInputDevices. Can be undefined, in this case it will decode from one of the available devices, preffering the main camera (environment facing) if available.
  127. * @param video the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  128. * @returns The decoding result.
  129. *
  130. * @memberOf BrowserCodeReader
  131. *
  132. * @deprecated Use `decodeOnceFromVideoDevice` instead.
  133. */
  134. decodeFromInputVideoDevice(deviceId?: string, videoSource?: string | HTMLVideoElement): Promise<Result>;
  135. /**
  136. * In one attempt, tries to decode the barcode from the device specified by deviceId while showing the video in the specified video element.
  137. *
  138. * @param deviceId the id of one of the devices obtained after calling getVideoInputDevices. Can be undefined, in this case it will decode from one of the available devices, preffering the main camera (environment facing) if available.
  139. * @param video the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  140. * @returns The decoding result.
  141. *
  142. * @memberOf BrowserCodeReader
  143. */
  144. decodeOnceFromVideoDevice(deviceId?: string, videoSource?: string | HTMLVideoElement): Promise<Result>;
  145. /**
  146. * In one attempt, tries to decode the barcode from a stream obtained from the given constraints while showing the video in the specified video element.
  147. *
  148. * @param constraints the media stream constraints to get s valid media stream to decode from
  149. * @param video the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  150. * @returns The decoding result.
  151. *
  152. * @memberOf BrowserCodeReader
  153. */
  154. decodeOnceFromConstraints(constraints: MediaStreamConstraints, videoSource?: string | HTMLVideoElement): Promise<Result>;
  155. /**
  156. * In one attempt, tries to decode the barcode from a stream obtained from the given constraints while showing the video in the specified video element.
  157. *
  158. * @param {MediaStream} [constraints] the media stream constraints to get s valid media stream to decode from
  159. * @param {string|HTMLVideoElement} [video] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  160. * @returns {Promise<Result>} The decoding result.
  161. *
  162. * @memberOf BrowserCodeReader
  163. */
  164. decodeOnceFromStream(stream: MediaStream, videoSource?: string | HTMLVideoElement): Promise<Result>;
  165. /**
  166. * Continuously decodes the barcode from the device specified by device while showing the video in the specified video element.
  167. *
  168. * @param {string|null} [deviceId] the id of one of the devices obtained after calling getVideoInputDevices. Can be undefined, in this case it will decode from one of the available devices, preffering the main camera (environment facing) if available.
  169. * @param {string|HTMLVideoElement|null} [video] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  170. * @returns {Promise<void>}
  171. *
  172. * @memberOf BrowserCodeReader
  173. *
  174. * @deprecated Use `decodeFromVideoDevice` instead.
  175. */
  176. decodeFromInputVideoDeviceContinuously(deviceId: string | null, videoSource: string | HTMLVideoElement | null, callbackFn: DecodeContinuouslyCallback): Promise<void>;
  177. /**
  178. * Continuously tries to decode the barcode from the device specified by device while showing the video in the specified video element.
  179. *
  180. * @param {string|null} [deviceId] the id of one of the devices obtained after calling getVideoInputDevices. Can be undefined, in this case it will decode from one of the available devices, preffering the main camera (environment facing) if available.
  181. * @param {string|HTMLVideoElement|null} [video] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  182. * @returns {Promise<void>}
  183. *
  184. * @memberOf BrowserCodeReader
  185. */
  186. decodeFromVideoDevice(deviceId: string | null, videoSource: string | HTMLVideoElement | null, callbackFn: DecodeContinuouslyCallback): Promise<void>;
  187. /**
  188. * Continuously tries to decode the barcode from a stream obtained from the given constraints while showing the video in the specified video element.
  189. *
  190. * @param {MediaStream} [constraints] the media stream constraints to get s valid media stream to decode from
  191. * @param {string|HTMLVideoElement} [video] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  192. * @returns {Promise<Result>} The decoding result.
  193. *
  194. * @memberOf BrowserCodeReader
  195. */
  196. decodeFromConstraints(constraints: MediaStreamConstraints, videoSource: string | HTMLVideoElement, callbackFn: DecodeContinuouslyCallback): Promise<void>;
  197. /**
  198. * In one attempt, tries to decode the barcode from a stream obtained from the given constraints while showing the video in the specified video element.
  199. *
  200. * @param {MediaStream} [constraints] the media stream constraints to get s valid media stream to decode from
  201. * @param {string|HTMLVideoElement} [video] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  202. * @returns {Promise<Result>} The decoding result.
  203. *
  204. * @memberOf BrowserCodeReader
  205. */
  206. decodeFromStream(stream: MediaStream, videoSource: string | HTMLVideoElement, callbackFn: DecodeContinuouslyCallback): Promise<void>;
  207. /**
  208. * Breaks the decoding loop.
  209. */
  210. stopAsyncDecode(): void;
  211. /**
  212. * Breaks the decoding loop.
  213. */
  214. stopContinuousDecode(): void;
  215. /**
  216. * Sets the new stream and request a new decoding-with-delay.
  217. *
  218. * @param stream The stream to be shown in the video element.
  219. * @param decodeFn A callback for the decode method.
  220. */
  221. protected attachStreamToVideo(stream: MediaStream, videoSource: string | HTMLVideoElement): Promise<HTMLVideoElement>;
  222. /**
  223. *
  224. * @param videoElement
  225. */
  226. protected playVideoOnLoadAsync(videoElement: HTMLVideoElement): Promise<void>;
  227. /**
  228. * Binds listeners and callbacks to the videoElement.
  229. *
  230. * @param element
  231. * @param callbackFn
  232. */
  233. protected playVideoOnLoad(element: HTMLVideoElement, callbackFn: EventListener): void;
  234. /**
  235. * Checks if the given video element is currently playing.
  236. */
  237. isVideoPlaying(video: HTMLVideoElement): boolean;
  238. /**
  239. * Just tries to play the video and logs any errors.
  240. * The play call is only made is the video is not already playing.
  241. */
  242. tryPlayVideo(videoElement: HTMLVideoElement): Promise<void>;
  243. /**
  244. * Searches and validates a media element.
  245. */
  246. getMediaElement(mediaElementId: string, type: string): HTMLVisualMediaElement;
  247. /**
  248. * Decodes the barcode from an image.
  249. *
  250. * @param {(string|HTMLImageElement)} [source] The image element that can be either an element id or the element itself. Can be undefined in which case the decoding will be done from the imageUrl parameter.
  251. * @param {string} [url]
  252. * @returns {Promise<Result>} The decoding result.
  253. *
  254. * @memberOf BrowserCodeReader
  255. */
  256. decodeFromImage(source?: string | HTMLImageElement, url?: string): Promise<Result>;
  257. /**
  258. * Decodes the barcode from a video.
  259. *
  260. * @param {(string|HTMLImageElement)} [source] The image element that can be either an element id or the element itself. Can be undefined in which case the decoding will be done from the imageUrl parameter.
  261. * @param {string} [url]
  262. * @returns {Promise<Result>} The decoding result.
  263. *
  264. * @memberOf BrowserCodeReader
  265. */
  266. decodeFromVideo(source?: string | HTMLVideoElement, url?: string): Promise<Result>;
  267. /**
  268. * Decodes continuously the barcode from a video.
  269. *
  270. * @param {(string|HTMLImageElement)} [source] The image element that can be either an element id or the element itself. Can be undefined in which case the decoding will be done from the imageUrl parameter.
  271. * @param {string} [url]
  272. * @returns {Promise<Result>} The decoding result.
  273. *
  274. * @memberOf BrowserCodeReader
  275. *
  276. * @experimental
  277. */
  278. decodeFromVideoContinuously(source: string | HTMLVideoElement | null, url: string | null, callbackFn: DecodeContinuouslyCallback): Promise<void>;
  279. /**
  280. * Decodes something from an image HTML element.
  281. */
  282. decodeFromImageElement(source: string | HTMLImageElement): Promise<Result>;
  283. /**
  284. * Decodes something from an image HTML element.
  285. */
  286. decodeFromVideoElement(source: string | HTMLVideoElement): Promise<Result>;
  287. /**
  288. * Decodes something from an image HTML element.
  289. */
  290. decodeFromVideoElementContinuously(source: string | HTMLVideoElement, callbackFn: DecodeContinuouslyCallback): Promise<void>;
  291. /**
  292. * Sets up the video source so it can be decoded when loaded.
  293. *
  294. * @param source The video source element.
  295. */
  296. private _decodeFromVideoElementSetup;
  297. /**
  298. * Decodes an image from a URL.
  299. */
  300. decodeFromImageUrl(url?: string): Promise<Result>;
  301. /**
  302. * Decodes an image from a URL.
  303. */
  304. decodeFromVideoUrl(url: string): Promise<Result>;
  305. /**
  306. * Decodes an image from a URL.
  307. *
  308. * @experimental
  309. */
  310. decodeFromVideoUrlContinuously(url: string, callbackFn: DecodeContinuouslyCallback): Promise<void>;
  311. private _decodeOnLoadImage;
  312. private _decodeOnLoadVideo;
  313. private _decodeOnLoadVideoContinuously;
  314. isImageLoaded(img: HTMLImageElement): boolean;
  315. prepareImageElement(imageSource?: HTMLImageElement | string): HTMLImageElement;
  316. /**
  317. * Sets a HTMLVideoElement for scanning or creates a new one.
  318. *
  319. * @param videoSource The HTMLVideoElement to be set.
  320. */
  321. prepareVideoElement(videoSource?: HTMLVideoElement | string): HTMLVideoElement;
  322. /**
  323. * Tries to decode from the video input until it finds some value.
  324. */
  325. decodeOnce(element: HTMLVisualMediaElement, retryIfNotFound?: boolean, retryIfChecksumOrFormatError?: boolean): Promise<Result>;
  326. /**
  327. * Continuously decodes from video input.
  328. */
  329. decodeContinuously(element: HTMLVideoElement, callbackFn: DecodeContinuouslyCallback): void;
  330. /**
  331. * Gets the BinaryBitmap for ya! (and decodes it)
  332. */
  333. decode(element: HTMLVisualMediaElement): Result;
  334. /**
  335. * Creates a binaryBitmap based in some image source.
  336. *
  337. * @param mediaElement HTML element containing drawable image source.
  338. */
  339. createBinaryBitmap(mediaElement: HTMLVisualMediaElement): BinaryBitmap;
  340. /**
  341. *
  342. */
  343. protected getCaptureCanvasContext(mediaElement?: HTMLVisualMediaElement): CanvasRenderingContext2D;
  344. /**
  345. *
  346. */
  347. protected getCaptureCanvas(mediaElement?: HTMLVisualMediaElement): HTMLCanvasElement;
  348. /**
  349. * Overwriting this allows you to manipulate the next frame in anyway you want before decode.
  350. */
  351. drawFrameOnCanvas(srcElement: HTMLVideoElement, dimensions?: {
  352. sx: number;
  353. sy: number;
  354. sWidth: number;
  355. sHeight: number;
  356. dx: number;
  357. dy: number;
  358. dWidth: number;
  359. dHeight: number;
  360. }, canvasElementContext?: CanvasRenderingContext2D): void;
  361. /**
  362. * Ovewriting this allows you to manipulate the snapshot image in anyway you want before decode.
  363. */
  364. drawImageOnCanvas(srcElement: HTMLImageElement, dimensions?: {
  365. sx: number;
  366. sy: number;
  367. sWidth: number;
  368. sHeight: number;
  369. dx: number;
  370. dy: number;
  371. dWidth: number;
  372. dHeight: number;
  373. }, canvasElementContext?: CanvasRenderingContext2D): void;
  374. /**
  375. * Call the encapsulated readers decode
  376. */
  377. decodeBitmap(binaryBitmap: BinaryBitmap): Result;
  378. /**
  379. * 🖌 Prepares the canvas for capture and scan frames.
  380. */
  381. createCaptureCanvas(mediaElement?: HTMLVisualMediaElement): HTMLCanvasElement;
  382. /**
  383. * Stops the continuous scan and cleans the stream.
  384. */
  385. protected stopStreams(): void;
  386. /**
  387. * Resets the code reader to the initial state. Cancels any ongoing barcode scanning from video or camera.
  388. *
  389. * @memberOf BrowserCodeReader
  390. */
  391. reset(): void;
  392. private _destroyVideoElement;
  393. private _destroyImageElement;
  394. /**
  395. * Cleans canvas references 🖌
  396. */
  397. private _destroyCaptureCanvas;
  398. /**
  399. * Defines what the videoElement src will be.
  400. *
  401. * @param videoElement
  402. * @param stream
  403. */
  404. addVideoSource(videoElement: HTMLVideoElement, stream: MediaStream): void;
  405. /**
  406. * Unbinds a HTML video src property.
  407. *
  408. * @param videoElement
  409. */
  410. private cleanVideoSource;
  411. }