@capturebridge/sdk 0.7.0 → 0.8.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/CapturePlugin.js CHANGED
@@ -1,79 +1,68 @@
1
- import Plugin from './Plugin.js'
2
- import { BASE_URL, DATAURL_JPEG, blobToBuffer } from './Common.js'
3
-
4
- /**
5
- * CapturePlugin
6
- * @classdesc Class for working with a plugin that acquires high resolution
7
- * images from a device.
8
- *
9
- * @see {@link Camera}
10
- * @see {@link SignatureTablet}
11
- * @see {@link Scanner}
12
- */
13
- class CapturePlugin extends Plugin {
14
- /**
15
- * Instantiate a CapturePlugin
16
- * @constructor
17
- * @param {string} [baseURL] - Protocol, domain, and port for the service.
18
- * @param {string} pluginId - The Id of the desired plugin to use for this
19
- * camera instance. The plugin must support the "start_feed", "stop_feed",
20
- * "devices", and "capture" methods.
21
- * @example
22
- * const capture = new CapturePlugin('capture_camera_canon')
23
- */
24
- constructor (plugin, baseUrl = BASE_URL) {
25
- super(plugin, baseUrl)
26
- }
27
-
28
- /**
29
- * Take a full capture
30
- * @param {string} [kind=buffer] - Specify how to return the captured image. Valid
31
- * values are 'blob', 'buffer', 'base64', or 'dataurl'.
32
- *
33
- * The default value of 'buffer' will load the image into an
34
- * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer|ArrayBuffer}
35
- * that can be set directly on an img tag's src attribute.
36
- *
37
- * The value of 'blob' will return a
38
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/Blob|Blob} that can
39
- * be used with a
40
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/FileReader|FileReader}.
41
- *
42
- * The value of 'base64' will return the image contents as a
43
- * {@link https://en.wikipedia.org/wiki/Base64|Base64} encoded string that is
44
- * suitable for use with JSON serialization.
45
- *
46
- * The value of 'dataurl' will return a JPEG
47
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URL}
48
- * that can be set on an img tag's src attribute or loaded as a document
49
- * location.
50
- *
51
- * @param {string|object} [deviceOpt] - Take the capture using either a
52
- * specific Device ID or a Device Object. The default is the first available
53
- * device.
54
- *
55
- * @async
56
- * @example
57
- * // Capture an image as an ArrayBuffer and add it to an img tag appended to
58
- * // the document's body.
59
- * const img = document.createElement('img')
60
- * img.src = await capture.fullCapture()
61
- * document.body.appendChild(img)
62
- */
63
- async fullCapture (kind = 'buffer', deviceOpt) {
64
- const device = await this.setupDevice(deviceOpt)
65
- switch (kind) {
66
- case 'blob':
67
- return await device.captureAsBlob()
68
- case 'buffer':
69
- return await blobToBuffer(await device.captureAsBlob())
70
- case 'base64':
71
- return await device.captureAsBase64()
72
- case 'dataurl':
73
- return DATAURL_JPEG + (await device.captureAsBase64())
74
- }
75
- throw new Error(`Unknown image response type: ${kind}`)
76
- }
77
- }
78
-
79
- export default CapturePlugin
1
+ import Plugin from './Plugin.js'
2
+ import {
3
+ BASE_URL,
4
+ DATAURL_JPEG,
5
+ DEFAULT_CAPTURE_OPT,
6
+ blobToBuffer
7
+ } from './Common.js'
8
+
9
+ /**
10
+ * CapturePlugin
11
+ * @classdesc Class for working with a plugin that acquires high resolution
12
+ * images from a device.
13
+ * @extends Plugin
14
+ * @see {@link Camera}
15
+ * @see {@link SignatureTablet}
16
+ * @see {@link Scanner}
17
+ */
18
+ class CapturePlugin extends Plugin {
19
+ /**
20
+ * Instantiate a CapturePlugin
21
+ * @constructor
22
+ * @param {string} [baseURL] - Protocol, domain, and port for the service.
23
+ * @param {string} pluginId - The Id of the desired plugin to use for this
24
+ * camera instance. The plugin must support the `start_feed`, `stop_feed`,
25
+ * `devices`, and `capture` methods.
26
+ * @example
27
+ * const capture = new CapturePlugin('capture_camera_canon')
28
+ */
29
+ constructor (plugin, baseUrl = BASE_URL) {
30
+ super(plugin, baseUrl)
31
+ }
32
+
33
+ /**
34
+ * Take a full capture
35
+ * @param {CaptureOptions} [captureOpt] - Additional options for capturing an
36
+ * image.
37
+ * @param {string|object} [deviceOpt] - Take the capture using either a
38
+ * specific Device ID or a Device Object. The default is the first available
39
+ * device.
40
+ *
41
+ * @async
42
+ * @example
43
+ * // Capture an image as an ArrayBuffer and add it to an img tag appended to
44
+ * // the document's body.
45
+ * const img = document.createElement('img')
46
+ * img.src = await capture.fullCapture()
47
+ * document.body.appendChild(img)
48
+ */
49
+ async fullCapture (captureOpt = {}, deviceOpt) {
50
+ const device = await this.setupDevice(deviceOpt)
51
+ const mergedOpt = Object.assign({}, DEFAULT_CAPTURE_OPT, captureOpt)
52
+ switch (mergedOpt.kind) {
53
+ case 'objecturl':
54
+ return URL.createObjectURL(await device.captureAsBlob(mergedOpt))
55
+ case 'blob':
56
+ return await device.captureAsBlob(mergedOpt)
57
+ case 'buffer':
58
+ return await blobToBuffer(await device.captureAsBlob(mergedOpt))
59
+ case 'base64':
60
+ return await device.captureAsBase64(mergedOpt)
61
+ case 'dataurl':
62
+ return DATAURL_JPEG + (await device.captureAsBase64(mergedOpt))
63
+ }
64
+ throw new Error(`Unknown image response kind: ${mergedOpt.kind}`)
65
+ }
66
+ }
67
+
68
+ export default CapturePlugin
package/Common.js CHANGED
@@ -1,98 +1,182 @@
1
- /**
2
- * Default base URL for making API requests.
3
- * @constant
4
- * @type {string}
5
- * @default https://capture.local.valididcloud.com:9001
6
- */
7
- export const BASE_URL = 'https://capture.local.valididcloud.com:9001'
8
-
9
- /**
10
- * Common {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API|Fetch}
11
- * options for making
12
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST|POST}
13
- * requests to the API.
14
- * @constant
15
- * @type {string}
16
- */
17
- export const POST = {
18
- mode: 'cors',
19
- method: 'post'
20
- }
21
-
22
- /**
23
- * Common {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API|Fetch}
24
- * options for making
25
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE|DELETE}
26
- * requests to the API.
27
- * @constant
28
- * @type {string}
29
- */
30
- export const DELETE = {
31
- mode: 'cors',
32
- method: 'delete'
33
- }
34
-
35
- /**
36
- * `data:` prefix for JPEG
37
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URLs}
38
- * @constant
39
- * @type {string}
40
- */
41
- export const DATAURL_JPEG = 'data:image/jpeg;base64,'
42
-
43
- /**
44
- * `data:` prefix for PNG
45
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URLs}
46
- * @constant
47
- * @type {string}
48
- */
49
- export const DATAURL_PNG = 'data:image/png;base64,'
50
-
51
- /**
52
- * An Async/Await version of
53
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/setTimeout|setTimeout}
54
- * @private
55
- */
56
- export const timeout = millis => {
57
- return new Promise(resolve => setTimeout(resolve, millis))
58
- }
59
-
60
- /**
61
- * Convert a Blob to a Buffer
62
- * @private
63
- */
64
- export const blobToBuffer = blob => {
65
- return new Promise(resolve => {
66
- const reader = new window.FileReader()
67
- reader.onload = event => resolve(event.target.result)
68
- reader.readAsArrayBuffer(blob)
69
- })
70
- }
71
-
72
- /**
73
- * Convert a Blob to a Base64 String
74
- * @private
75
- */
76
- export const blobToBase64 = blob => {
77
- return new Promise(resolve => {
78
- const reader = new window.FileReader()
79
- reader.onload = event => resolve(event.target.result.split(',')[1])
80
- reader.readAsDataURL(blob)
81
- })
82
- }
83
-
84
- /**
85
- * Helper function for classes to easily add callback style support to their
86
- * async/await methods.
87
- * @private
88
- */
89
- export const asyncToCallback = (thisValue, asyncFn, callback, ...params) => {
90
- (async () => {
91
- try {
92
- const result = await asyncFn.call(thisValue, ...(params.length ? params : []))
93
- callback(null, result)
94
- } catch (ex) {
95
- callback(ex)
96
- }
97
- })()
98
- }
1
+ /**
2
+ * @summary Default base URL for making API requests.
3
+ * @constant
4
+ * @type {string}
5
+ * @default {@link https://local.capturebridge.net:9001}
6
+ */
7
+ export const BASE_URL = 'https://local.capturebridge.net:9001'
8
+
9
+ /**
10
+ * Common {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API|Fetch}
11
+ * options for making
12
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST|POST}
13
+ * requests to the API.
14
+ * @constant
15
+ * @type {object}
16
+ */
17
+ export const POST = {
18
+ mode: 'cors',
19
+ method: 'post'
20
+ }
21
+
22
+ /**
23
+ * @summary
24
+ * Common {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API|Fetch}
25
+ * options for making
26
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE|DELETE}
27
+ * requests to the API.
28
+ * @constant
29
+ * @type {object}
30
+ */
31
+ export const DELETE = {
32
+ mode: 'cors',
33
+ method: 'delete'
34
+ }
35
+
36
+ /**
37
+ * @summary Default options for stream operations
38
+ * @constant
39
+ * @property {number} rotate - Angle, in degrees to rotate the stream. Must be
40
+ * a value from -359 to 359. A value outside of that range, or a 0 will perform
41
+ * no rotation.
42
+ * @typedef {object} StreamOptions
43
+ */
44
+ export const DEFAULT_STREAM_OPT = {
45
+ rotate: 0
46
+ }
47
+
48
+ /**
49
+ * @typedef {string} CaptureResponseKind
50
+ * @summary Specifies the desired return type for a captured object. Valid
51
+ * values are `objecturl`, `blob`, `buffer`, `base64`, or `dataurl`.
52
+ *
53
+ * - `objecturl` will load the captured object into an
54
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static|ObjectURL}
55
+ * that can be set directly on an img tag's src attribute. Note that these URLs
56
+ * should be {@link https://developer.mozilla.org/en-US/docs/Web/API/URL/revokeObjectURL_static|revoked}
57
+ * when they are no longer needed.
58
+ *
59
+ * - `buffer` will load the image into an
60
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer|ArrayBuffer}
61
+ *
62
+ * - `blob` will return a
63
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/Blob|Blob} that can
64
+ * be used with a
65
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/FileReader|FileReader}.
66
+ *
67
+ * - `base64` will return the image contents as a
68
+ * {@link https://en.wikipedia.org/wiki/Base64|Base64} encoded string that is
69
+ * suitable for use with JSON serialization.
70
+ *
71
+ * - `dataurl` will return a
72
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URL}
73
+ * that can be set on an img tag's src attribute or loaded as a document
74
+ * location.
75
+ *
76
+ */
77
+
78
+ /**
79
+ * @summary Default options for capture operations.
80
+ * @constant
81
+ * @property {number?} rotate - Angle, in degrees to rotate the captured object.
82
+ * Must be a value from -359 to 359. A value of 0 will perform no rotation.
83
+ * @property {boolean?} base64 - Return the captured object as a base64 encoded
84
+ * string. The default behavior is to return the image as binary data.
85
+ * @property {CaptureResponseKind} kind - Specifies how to return the captured
86
+ * object. Some SDK methods that are designed to only return a particular type,
87
+ * such as {@link Device#frameAsBase64}, ignore this parameter. This parameter
88
+ * is also not sent in API requests, it is used internally by the SDK for
89
+ * constructing the appropriate response type.
90
+ * @typedef {object} CaptureOptions
91
+ */
92
+ export const DEFAULT_CAPTURE_OPT = {
93
+ rotate: 0,
94
+ base64: false,
95
+ kind: 'objecturl'
96
+ }
97
+
98
+ /**
99
+ * @summary `data:` prefix for JPEG
100
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URLs}
101
+ * @constant
102
+ * @type {string}
103
+ */
104
+ export const DATAURL_JPEG = 'data:image/jpeg;base64,'
105
+
106
+ /**
107
+ * @summary `data:` prefix for PNG
108
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URLs}
109
+ * @constant
110
+ * @type {string}
111
+ */
112
+ export const DATAURL_PNG = 'data:image/png;base64,'
113
+
114
+ /**
115
+ * An Async/Await version of
116
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/setTimeout|setTimeout}
117
+ * @private
118
+ */
119
+ export const timeout = millis => {
120
+ return new Promise(resolve => setTimeout(resolve, millis))
121
+ }
122
+
123
+ /**
124
+ * Convert a Blob to a Buffer
125
+ * @private
126
+ */
127
+ export const blobToBuffer = blob => {
128
+ return new Promise(resolve => {
129
+ const reader = new window.FileReader()
130
+ reader.onload = event => resolve(event.target.result)
131
+ reader.readAsArrayBuffer(blob)
132
+ })
133
+ }
134
+
135
+ /**
136
+ * Convert a Blob to a Base64 String
137
+ * @private
138
+ */
139
+ export const blobToBase64 = blob => {
140
+ return new Promise(resolve => {
141
+ const reader = new window.FileReader()
142
+ reader.onload = event => resolve(event.target.result.split(',')[1])
143
+ reader.readAsDataURL(blob)
144
+ })
145
+ }
146
+
147
+ /**
148
+ * Helper function for classes to easily add callback style support to their
149
+ * async/await methods.
150
+ * @private
151
+ */
152
+ export const asyncToCallback = (thisValue, asyncFn, callback, ...params) => {
153
+ (async () => {
154
+ try {
155
+ const result = await asyncFn.call(thisValue, ...(params.length ? params : []))
156
+ callback(null, result)
157
+ } catch (ex) {
158
+ callback(ex)
159
+ }
160
+ })()
161
+ }
162
+
163
+ /**
164
+ * Helper function to throw if an http response was a error.
165
+ * @private
166
+ */
167
+ export const checkResponse = async (response) => {
168
+ if (!response.ok) {
169
+ const error = await response.json()
170
+ const msg = error.message || error.summary || error.code || 'Unexpected error'
171
+ const ex = new Error(msg)
172
+ ex.code = error.code || 500
173
+ ex.summary = error.summary || 'Unexpected error'
174
+ throw ex
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Allows passing in a string for the response type of capture methods
180
+ * @private
181
+ */
182
+ export const DEPRECATION_01 = true