import Plugin from './Plugin.js'
import {
BASE_URL,
DATAURL_JPEG,
DEFAULT_CAPTURE_OPT,
blobToBuffer
} from './Common.js'
/**
* CapturePlugin
* @classdesc Class for working with a plugin that acquires high resolution
* images from a device.
* @extends Plugin
* @see {@link Camera}
* @see {@link SignatureTablet}
* @see {@link Scanner}
*/
class CapturePlugin extends Plugin {
/**
* Instantiate a CapturePlugin
* @constructor
* @param {string} [baseURL] - Protocol, domain, and port for the service.
* @param {string} pluginId - The Id of the desired plugin to use for this
* camera instance. The plugin must support the `start_feed`, `stop_feed`,
* `devices`, and `capture` methods.
* @example
* const capture = new CapturePlugin('capture_camera_canon')
*/
constructor (plugin, baseUrl = BASE_URL) {
super(plugin, baseUrl)
}
/**
* Take a full capture
* @param {CaptureOptions} [captureOpt] - Additional options for capturing an
* image.
* @param {string|object} [deviceOpt] - Take the capture using either a
* specific Device ID or a Device Object. The default is the first available
* device.
*
* @async
* @example
* // Capture an image as an ArrayBuffer and add it to an img tag appended to
* // the document's body.
* const img = document.createElement('img')
* img.src = await capture.fullCapture()
* document.body.appendChild(img)
*/
async fullCapture (captureOpt = {}, deviceOpt) {
const device = await this.setupDevice(deviceOpt)
const mergedOpt = Object.assign({}, DEFAULT_CAPTURE_OPT, captureOpt)
switch (mergedOpt.kind) {
case 'objecturl':
return URL.createObjectURL(await device.captureAsBlob(mergedOpt))
case 'blob':
return await device.captureAsBlob(mergedOpt)
case 'buffer':
return await blobToBuffer(await device.captureAsBlob(mergedOpt))
case 'base64':
return await device.captureAsBase64(mergedOpt)
case 'dataurl':
return DATAURL_JPEG + (await device.captureAsBase64(mergedOpt))
}
throw new Error(`Unknown image response kind: ${mergedOpt.kind}`)
}
}
export default CapturePlugin