Commit 1454d77f authored by Gabriel Monnerat's avatar Gabriel Monnerat

erp5_document_scanner: Introduce Caman to manipulate image before crop

parent fcb69b03
/*jslint indent: 2, unparam: true */ /*jslint indent: 2, unparam: true */
/*global rJS, RSVP, window, document, navigator, Cropper, Promise, JSON, jIO, promiseEventListener, domsugar, createImageBitmap, FormData*/ /*global rJS, RSVP, window, document, navigator, Cropper, Promise, JSON, jIO, promiseEventListener, domsugar, createImageBitmap, FormData, Caman*/
(function (rJS, RSVP, window, document, navigator, Cropper, Promise, JSON, jIO, promiseEventListener, domsugar, createImageBitmap, FormData) { (function (rJS, RSVP, window, document, navigator, Cropper, Promise, JSON, jIO, promiseEventListener, domsugar, createImageBitmap, FormData, Caman) {
"use strict"; "use strict";
////////////////////////////////////////////////// //////////////////////////////////////////////////
...@@ -50,13 +50,60 @@ ...@@ -50,13 +50,60 @@
return new RSVP.Promise(waitForStream, canceller); return new RSVP.Promise(waitForStream, canceller);
} }
function handleCropper(element, data, callback) { function handleCropper(gadget, element, data, settings, callback) {
var cropper; var cropper,
queue = new RSVP.Queue(),
img = domsugar("img"),
canvas = domsugar('canvas', {'class': 'canvas'});
function canceller() { function canceller() {
cropper.destroy(); cropper.destroy();
} }
if (settings.brightness || settings.contrast || settings.enable_greyscale || settings.compression) {
return new RSVP.Queue()
.push(function () {
return new Promise(function (resolve) {
Caman(canvas, element, function () {
if (settings.brightness !== 0) {
this.brightness(data.brightness);
}
if (settings.contrast !== 0) {
this.contrast(data.contrast);
}
if (settings.enable_greyscale) {
this.greyscale();
}
this.render(function () {
resolve(canvas.toDataURL("image/jpeg", settings.compression));
});
});
});
})
.push(function (data_str) {
img.setAttribute("src", data_str);
// creating Cropper is asynchronous
return new RSVP.Promise(function (resolve, reject) {
cropper = new Cropper(img, {
data: data,
ready: function () {
return new RSVP.Queue()
.push(function () {
return callback(cropper);
})
.push(undefined, function (error) {
if (!(error instanceof RSVP.CancellationError)) {
canceller();
reject(error);
}
});
}
});
}, canceller);
});
}
// creating Cropper is asynchronous // creating Cropper is asynchronous
return new RSVP.Promise(function (resolve, reject) { return new RSVP.Promise(function (resolve, reject) {
cropper = new Cropper(element, { cropper = new Cropper(element, {
...@@ -294,6 +341,7 @@ ...@@ -294,6 +341,7 @@
gadget.element.querySelector('video').srcObject.getVideoTracks()[0] gadget.element.querySelector('video').srcObject.getVideoTracks()[0]
), ),
div; div;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return image_capture.getPhotoCapabilities(); return image_capture.getPhotoCapabilities();
...@@ -345,8 +393,10 @@ ...@@ -345,8 +393,10 @@
gadget.element.replaceChild(div, gadget.element.firstElementChild); gadget.element.replaceChild(div, gadget.element.firstElementChild);
addDetachedPromise(gadget, 'cropper', addDetachedPromise(gadget, 'cropper',
handleCropper(canvas, handleCropper(gadget,
canvas,
gadget.state.preferred_cropped_canvas_data, gadget.state.preferred_cropped_canvas_data,
gadget.state.preferred_image_settings_data,
defer.resolve)); defer.resolve));
return defer.promise; return defer.promise;
}) })
...@@ -445,6 +495,7 @@ ...@@ -445,6 +495,7 @@
active_process: default_value.active_process, active_process: default_value.active_process,
image_list: default_value.image_list, image_list: default_value.image_list,
preferred_cropped_canvas_data: JSON.parse(options.preferred_cropped_canvas_data), preferred_cropped_canvas_data: JSON.parse(options.preferred_cropped_canvas_data),
preferred_image_settings_data: JSON.parse(options.preferred_image_settings_data),
device_id: device_id, device_id: device_id,
key: options.key, key: options.key,
first_render: true first_render: true
...@@ -661,4 +712,4 @@ ...@@ -661,4 +712,4 @@
.declareAcquiredMethod("getTranslationList", "getTranslationList"); .declareAcquiredMethod("getTranslationList", "getTranslationList");
}(rJS, RSVP, window, document, navigator, Cropper, Promise, JSON, jIO, promiseEventListener, domsugar, createImageBitmap, FormData)); }(rJS, RSVP, window, document, navigator, Cropper, Promise, JSON, jIO, promiseEventListener, domsugar, createImageBitmap, FormData, Caman));
\ No newline at end of file \ No newline at end of file
...@@ -244,7 +244,7 @@ ...@@ -244,7 +244,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>981.43882.8130.35481</string> </value> <value> <string>981.51308.5350.25326</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -262,7 +262,7 @@ ...@@ -262,7 +262,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1581025205.74</float> <float>1581468361.42</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment