@capturebridge/sdk 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Scanner.js ADDED
@@ -0,0 +1,64 @@
1
+ import CapturePlugin from './CapturePlugin.js'
2
+ import { BASE_URL } from './Common.js'
3
+
4
+ /**
5
+ * Scanner
6
+ * @classdesc Class for invoking methods on a Scanner
7
+ *
8
+ * @see {@link CanonScanner}
9
+ */
10
+ class Scanner extends CapturePlugin {
11
+ /**
12
+ * Instantiate a Generic Scanner
13
+ * @constructor
14
+ * @param {string} [baseURL] - Protocol, domain, and port for the service.
15
+ * @param {string} pluginId - The Id of the desired plugin to use for this
16
+ * scanner instance. The plugin must support the "devices", and "capture"
17
+ * methods.
18
+ * @example
19
+ * const scanner = new Scanner('capture_scanner_windows')
20
+ */
21
+ constructor (plugin, baseUrl = BASE_URL) {
22
+ super(plugin, baseUrl)
23
+ }
24
+
25
+ /**
26
+ * Scan a document
27
+ * @param {string} [kind] - Specify how to return the scanned document. Valid
28
+ * values are 'blob', 'buffer', 'base64', or 'dataurl'.
29
+ *
30
+ * The default value of 'buffer' will load the image into an
31
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer|ArrayBuffer}
32
+ * that can be set directly on an img tag's src attribute.
33
+ *
34
+ * The value of 'blob' will return a
35
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/Blob|Blob} that can
36
+ * be used with a
37
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/FileReader|FileReader}.
38
+ *
39
+ * The value of 'base64' will return the image contents as a
40
+ * {@link https://en.wikipedia.org/wiki/Base64|Base64} encoded string that is
41
+ * suitable for use with JSON serialization.
42
+ *
43
+ * The value of 'dataurl' will return a JPEG
44
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URL}
45
+ * that can be set on an img tag's src attribute or loaded as a document
46
+ * location.
47
+ *
48
+ * @param {string|object} [deviceOpt] - Can a document using either a specific
49
+ * Device ID or a Device Object. The default is the first available device.
50
+ *
51
+ * @async
52
+ * @example
53
+ * // Scan document as an ArrayBuffer and add it to an img tag appended to
54
+ * // the document's body.
55
+ * const img = document.createElement('img')
56
+ * img.src = await scanner.scan()
57
+ * document.body.appendChild(img)
58
+ */
59
+ async scan (kind = 'buffer', deviceOpt) {
60
+ return await this.fullCapture(kind, deviceOpt)
61
+ }
62
+ }
63
+
64
+ export default Scanner
@@ -0,0 +1,109 @@
1
+ import StreamingCapturePlugin from './StreamingCapturePlugin.js'
2
+ import { BASE_URL } from './Common.js'
3
+
4
+ class SignatureTablet extends StreamingCapturePlugin {
5
+ #captureBridge
6
+ #scene = {}
7
+
8
+ constructor (plugin, baseUrl = BASE_URL) {
9
+ super(plugin, baseUrl)
10
+ }
11
+
12
+ /**
13
+ * Clear the tablet's display.
14
+ *
15
+ * @description This method will set the tablet's LCD screen to an empty white
16
+ * background. The blacklight will also be enabled if it is not already.
17
+ *
18
+ * @param {boolean} [backlight] If provided and set to true, will enable
19
+ * backlight. If false, it will be disabled. If unspecified no action will be
20
+ * taken on the backlight.
21
+ *
22
+ * @param {string|object} [deviceOpt] - Clear the display of a either a
23
+ * specific Device ID or a Device Object. The default is the first available
24
+ * device.
25
+ *
26
+ * @async
27
+ * @method
28
+ * @returns {object}
29
+ * @example
30
+ * const img = document.createElement('img')
31
+ * img.src = await camera.streamUrl()
32
+ * document.body.appendChild(img)
33
+ */
34
+ async clear (backlight, deviceOpt) {
35
+ const device = await this.setupDevice(deviceOpt)
36
+ return device.clear(backlight)
37
+ }
38
+
39
+ /**
40
+ * Display an Array of objects on the display.
41
+ *
42
+ * @description If any objects are Button types, the method will wait for one
43
+ * to be clicked and will then return the ID of the clicked object.
44
+ *
45
+ * @param {object[]} objects An array of Signature Tablet Widgets to display.
46
+ *
47
+ * @param {boolean} [clear] If true (the default), the display will be cleared
48
+ * before adding the objects.
49
+ *
50
+ * @param {string|object} [deviceOpt] - Display the objects on either a
51
+ * specific Device ID or a Device Object. The default is the first available
52
+ * device.
53
+ *
54
+ * @async
55
+ * @method
56
+ * @returns {string?} ID of the clicked object if any
57
+ * @example
58
+ * const result = await tablet.displayObjects([
59
+ * new Button("OK", 20, 200),
60
+ * new Button("Cancel", 80, 200)
61
+ * ])
62
+ *
63
+ * console.log(`${result} was clicked`)
64
+ */
65
+ async displayObjects (objects, clear = true, deviceOpt) {
66
+ const device = await this.setupDevice(deviceOpt)
67
+ return device.displayObjects(objects, clear)
68
+ }
69
+
70
+ /**
71
+ * Capture a high resolution signature
72
+ * @param {string} [kind] - Specify how to return the captured photo. Valid
73
+ * values are 'blob', 'buffer', 'base64', or 'dataurl'.
74
+ *
75
+ * The default value of 'buffer' will load the image into an
76
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer|ArrayBuffer}
77
+ * that can be set directly on an img tag's src attribute.
78
+ *
79
+ * The value of 'blob' will return a
80
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/Blob|Blob} that can
81
+ * be used with a
82
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/FileReader|FileReader}.
83
+ *
84
+ * The value of 'base64' will return the image contents as a
85
+ * {@link https://en.wikipedia.org/wiki/Base64|Base64} encoded string that is
86
+ * suitable for use with JSON serialization.
87
+ *
88
+ * The value of 'dataurl' will return a JPEG
89
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URL}
90
+ * that can be set on an img tag's src attribute or loaded as a document
91
+ * location.
92
+ *
93
+ * @param {string|object} [deviceOpt] - Capture the signature using either a
94
+ * specific Device ID or a Device Object. The default is the first available device.
95
+ *
96
+ * @async
97
+ * @example
98
+ * // Capture signature as an ArrayBuffer and add it to an img tag appended to
99
+ * // the document's body.
100
+ * const img = document.createElement('img')
101
+ * img.src = await tablet.signature()
102
+ * document.body.appendChild(img)
103
+ */
104
+ async signature (kind = 'buffer', deviceOpt) {
105
+ return await this.fullCapture(kind, deviceOpt)
106
+ }
107
+ }
108
+
109
+ export default SignatureTablet
@@ -0,0 +1,153 @@
1
+ import { blobToBase64 } from './Common.js'
2
+
3
+ export class DisplayWidget {
4
+ x
5
+ y
6
+ constructor (x, y) {
7
+ this.x = x
8
+ this.y = y
9
+ }
10
+ }
11
+
12
+ /**
13
+ * TextButton
14
+ * @classdesc A button for displaying clickable text on a tablet
15
+ *
16
+ * @see {@link SignatureTablet}
17
+ */
18
+ export class TextButton extends DisplayWidget {
19
+ text
20
+ fontFace
21
+ fontSize
22
+ id
23
+ /**
24
+ * Instantiate a Tablet Text Button
25
+ * @constructor
26
+ * @param {string} text - Text to display on the button
27
+ * @param {number} x - X position to draw the button
28
+ * @param {number} y - Y position to draw the button
29
+ * @param {text} [id] - ID of the button that will be returned when it is
30
+ * clicked. If not set one will attempt to be constructed from the button
31
+ * text but there is no guarantee it will be unique.
32
+ * @param {string} [fontFace] - Font face for the text on the button.
33
+ * Defaults to 'Arial'
34
+ * @param {number} [fontSize] - Size of the font for the text on the button.
35
+ * Defaults to 40.
36
+ * @example
37
+ * const new TextButton("OK", 20, 200)
38
+ */
39
+ constructor (text, x, y, id, fontFace = 'Arial', fontSize = 40) {
40
+ super(x, y)
41
+ this.text = text
42
+ this.fontFace = fontFace
43
+ this.fontSize = fontSize
44
+ if (id) {
45
+ this.id = id
46
+ } else {
47
+ this.id = text.length > 16 ? text.slice(0, 16) : text
48
+ }
49
+ }
50
+
51
+ toJSON () {
52
+ return {
53
+ type: 'button',
54
+ text: this.text,
55
+ font_face: this.fontFace,
56
+ font_size: this.fontSize,
57
+ x: this.x,
58
+ y: this.y,
59
+ id: this.id
60
+ }
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Text
66
+ * @classdesc Text to be displayed on a tablet
67
+ *
68
+ * @see {@link SignatureTablet}
69
+ */
70
+ export class Text extends DisplayWidget {
71
+ text
72
+ fontFace
73
+ fontSize
74
+ /**
75
+ * Instantiate a Text object
76
+ * @constructor
77
+ * @param {string} text - Text to display on the button
78
+ * @param {number} x - X position to draw the button
79
+ * @param {number} y - Y position to draw the button
80
+ * @param {string} [fontFace] - Font face for the text on the button.
81
+ * Defaults to 'Arial'
82
+ * @param {number} [fontSize] - Size of the font for the text on the button.
83
+ * Defaults to 40.
84
+ * @example
85
+ * const new Text("Do you agree?", 20, 200)
86
+ */
87
+ constructor (text, x, y, fontFace = 'Arial', fontSize = 40) {
88
+ super(x, y)
89
+ this.text = text
90
+ this.fontFace = fontFace
91
+ this.fontSize = fontSize
92
+ }
93
+
94
+ toJSON () {
95
+ return {
96
+ type: 'text',
97
+ text: this.text,
98
+ font_face: this.fontFace,
99
+ font_size: this.fontSize,
100
+ x: this.x,
101
+ y: this.y
102
+ }
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Image
108
+ * @classdesc Image to be displayed on a tablet
109
+ *
110
+ * @see {@link SignatureTablet}
111
+ */
112
+ export class Image extends DisplayWidget {
113
+ url
114
+ imageData
115
+ delay
116
+ /**
117
+ * Instantiate an Image object
118
+ * @constructor
119
+ * @param {string} url - URL of the image.
120
+ * @param {number} [x] - X position to draw the button
121
+ * @param {number} [y] - Y position to draw the button
122
+ * @param {number} [delay] - Milliseconds to sleep after displaying the image
123
+ * @example
124
+ * const new Image("/img/logo.tif", 20, 200)
125
+ */
126
+ constructor (url, x = 0, y = 0, delay = 0) {
127
+ super(x, y)
128
+ this.url = url
129
+ this.delay = delay
130
+ }
131
+
132
+ /**
133
+ * Fetch the image data and base64 encode it
134
+ * @async
135
+ * @example
136
+ * const new Image("/img/logo.tif", 20, 200)
137
+ */
138
+ async data () {
139
+ const response = await fetch(this.url)
140
+ const blob = await response.blob()
141
+ this.imageData = await blobToBase64(blob)
142
+ }
143
+
144
+ toJSON () {
145
+ return {
146
+ type: 'image',
147
+ data: this.imageData,
148
+ delay: this.delay,
149
+ x: this.x,
150
+ y: this.y
151
+ }
152
+ }
153
+ }
@@ -0,0 +1,121 @@
1
+ import CapturePlugin from './CapturePlugin.js'
2
+ import { BASE_URL, DATAURL_JPEG, blobToBuffer } from './Common.js'
3
+
4
+ class StreamingCapturePlugin extends CapturePlugin {
5
+ constructor (plugin, baseUrl = BASE_URL) {
6
+ super(plugin, baseUrl)
7
+ }
8
+
9
+ /**
10
+ * Get the CapturePlugin's stream endpoint URL.
11
+ *
12
+ * @description The URL returned from this endpoint can be attached to an
13
+ * img tag's src attribute. The camera's live stream will be started and
14
+ * begin streaming to the img tag as a
15
+ * {@link https://en.wikipedia.org/wiki/Motion_JPEG|Motion JPEG} stream.
16
+ * If the src is changed, the img removed from the DOM, or client disconnected
17
+ * for any reason, the live feed will automatically be stopped.
18
+ *
19
+ * @param {string|object} [deviceOpt] - Get the stream URL from either a
20
+ * specific Device ID or a Device Object. The default is the first available
21
+ * device.
22
+ *
23
+ * @async
24
+ * @method
25
+ * @returns {string} stream endpoint URL
26
+ * @example
27
+ * const img = document.createElement('img')
28
+ * img.src = await camera.streamUrl()
29
+ * document.body.appendChild(img)
30
+ */
31
+ async streamUrl (deviceOpt) {
32
+ const device = await this.setupDevice(deviceOpt)
33
+ return device.streamUrl()
34
+ }
35
+
36
+ /**
37
+ * Get the most recent frame from a device's live feed.
38
+ *
39
+ * @description This method will startup the device's live
40
+ * feed if necessary and wait for it to initialize before returning a frame.
41
+ * Subsequent calls will return the next available frame.
42
+ * You must manually stop the live feed if you are using this method.
43
+ *
44
+ * If implementing a real-time preview, it is highly recommended to use the
45
+ * stream endpoint which will stream a Motion JPEG.
46
+ *
47
+ * @see {@link CapturePlugin#streamUrl}
48
+ * @see {@link CapturePlugin#stopFeed}
49
+ *
50
+ * @param {string} [kind=buffer] - Specify how to return the captured image. Valid
51
+ * values are 'blob', 'buffer', 'base64', or 'dataurl'.
52
+ *
53
+ * The default value of 'buffer' will load the image into an
54
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer|ArrayBuffer}
55
+ * that can be set directly on an img tag's src attribute.
56
+ *
57
+ * The value of 'blob' will return a
58
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/Blob|Blob} that can
59
+ * be used with a
60
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/FileReader|FileReader}.
61
+ *
62
+ * The value of 'base64' will return the image contents as a
63
+ * {@link https://en.wikipedia.org/wiki/Base64|Base64} encoded string that is
64
+ * suitable for use with JSON serialization.
65
+ *
66
+ * The value of 'dataurl' will return a JPEG
67
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URL}
68
+ * that can be set on an img tag's src attribute or loaded as a document
69
+ * location.
70
+ *
71
+ * @param {string|object} [deviceOpt] - Return the frame from either a
72
+ * specific Device ID or a Device Object. The default, if not supplied, is the
73
+ * first available device.
74
+ *
75
+ * @async
76
+ * @method
77
+ * @example
78
+ * // Get the most recent frame from the camera's live feed as an ArrayBuffer
79
+ * // and add it to an img tag appended to the document's body.
80
+ * const img = document.createElement('img')
81
+ * img.src = await camera.getMostRecentFrame()
82
+ * document.body.appendChild(img)
83
+ *
84
+ * // Stop the live feed when done getting frames
85
+ * await camera.stopFeed()
86
+ */
87
+ async getMostRecentFrame (kind = 'buffer', deviceOpt) {
88
+ const device = await this.setupDevice(deviceOpt)
89
+ switch (kind) {
90
+ case 'blob':
91
+ return await device.frameAsBlob()
92
+ case 'buffer':
93
+ return await blobToBuffer(await device.frameAsBlob())
94
+ case 'base64':
95
+ return await device.frameAsBase64()
96
+ case 'dataurl':
97
+ return DATAURL_JPEG + (await device.frameAsBase64())
98
+ }
99
+ throw new Error(`Unknown image response type: ${kind}`)
100
+ }
101
+
102
+ /**
103
+ * Stop the camera's live feed if it is running.
104
+ *
105
+ * @method
106
+ * @async
107
+ * @returns {object} Status of the stop operation
108
+ * @example
109
+ * // CapturePlugin is now running it's live feed in a background thread
110
+ * const frame = await camera.getMostRecentFrame()
111
+ * // Do some stuff with frame...
112
+ * // Stop the live feed
113
+ * console.log(await camera.stopFeed())
114
+ */
115
+ async stopFeed (deviceOpt) {
116
+ const device = await this.setupDevice(deviceOpt)
117
+ return await device.stopFeed()
118
+ }
119
+ }
120
+
121
+ export default StreamingCapturePlugin
package/TopazSigGem.js ADDED
@@ -0,0 +1,25 @@
1
+ import SignatureTablet from './SignatureTablet.js'
2
+ import { BASE_URL } from './Common.js'
3
+
4
+ /**
5
+ * TopazSigGem
6
+ * @classdesc Convience class for instantiating a Topaz SigGem Signature Tablet
7
+ * Inherits all methods on the {@link SignatureTablet} class.
8
+ *
9
+ * @see {@link SignatureTablet}
10
+ */
11
+ class TopazSigGem extends SignatureTablet {
12
+ /**
13
+ * Instantiate a Topaz SigGem Signature Tablet
14
+ * @constructor
15
+ * @param {string} [baseURL] - Protocol, domain, and port for the service.
16
+ * @example
17
+ * const camera = new TopazSigGem()
18
+ *
19
+ */
20
+ constructor (baseUrl = BASE_URL) {
21
+ super('capture_signature_topaz', baseUrl)
22
+ }
23
+ }
24
+
25
+ export default TopazSigGem
package/package.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@capturebridge/sdk",
3
+ "description": "Capture Bridge JavaScript SDK for web applications",
4
+ "type": "module",
5
+ "license": "Apache-2.0",
6
+ "version": "0.7.0"
7
+ }