Commit db287a77 authored by Romain Courteaud's avatar Romain Courteaud

erp5_document_scanner: reduce number of createImageBitmap calls

parent fa3f859e
...@@ -418,19 +418,36 @@ ...@@ -418,19 +418,36 @@
}); });
} }
function fixPhotoOrientation(blob) { function fixPhotoOrientationSizeColor(blob, original_width, original_height,
settings) {
var orientation; var orientation;
return getOrientation(blob) return getOrientation(blob)
.push(function (result) { .push(function (result) {
orientation = result; orientation = result;
var expected_width = 2000,
bitmap_options,
div;
// If orientation is correct, return the original blob // If orientation is correct, return the original blob
if ((orientation < 2) || (8 < orientation)) { // and size is small
// no color correction is expected
if (((orientation < 2) || (8 < orientation)) &&
(original_width < expected_width) &&
(!(settings.brightness || settings.contrast || settings.enable_greyscale))) {
return blob; return blob;
} }
if (expected_width < original_width) {
bitmap_options = {
resizeWidth: expected_width,
// resizeHeight: expected_height,
resizeQuality: 'high'
};
}
// Else, transform the image // Else, transform the image
return new RSVP.Queue(createImageBitmap(blob)) return new RSVP.Queue(createImageBitmap(blob, bitmap_options))
.push(function (bitmap) { .push(function (bitmap) {
var height = bitmap.height, var height = bitmap.height,
...@@ -438,6 +455,10 @@ ...@@ -438,6 +455,10 @@
canvas = domsugar('canvas'), canvas = domsugar('canvas'),
ctx; ctx;
// Caman expect the canvas to be in a container
// in order to replace it when resizing
div = domsugar('div', [canvas]);
if (4 < orientation && orientation < 9) { if (4 < orientation && orientation < 9) {
canvas.width = height; canvas.width = height;
canvas.height = width; canvas.height = width;
...@@ -475,57 +496,14 @@ ...@@ -475,57 +496,14 @@
break; break;
} }
ctx.drawImage(bitmap, 0, 0); ctx.drawImage(bitmap, 0, 0);
return promiseCanvasToBlob(canvas);
});
});
}
function fixPhotoColor(blob, settings) { if (settings.brightness || settings.contrast || settings.enable_greyscale) {
if (!(settings.brightness || settings.contrast || settings.enable_greyscale)) { return handleCaman(canvas, settings);
return blob; }
} })
.push(function () {
var div; return promiseCanvasToBlob(div.firstElementChild);
return new RSVP.Queue(createImageBitmap(blob)) });
.push(function (bitmap) {
var canvas = domsugar('canvas');
// Caman expect the canvas to be in a container
// in order to replace it when resizing
div = domsugar('div', [canvas]);
canvas.width = bitmap.width;
canvas.height = bitmap.height;
canvas.getContext('2d').drawImage(bitmap, 0, 0);
return handleCaman(canvas, settings);
})
.push(function () {
return promiseCanvasToBlob(div.firstElementChild);
});
}
function resizePhoto(blob, original_width, original_height) {
var expected_width = 2000;
if (original_width < expected_width) {
return blob;
}
// expected_height = parseInt((original_height * expected_width) / original_width);
// alert(expected_width + ' ' + expected_height);
return new RSVP.Queue(createImageBitmap(blob, {
resizeWidth: expected_width,
// resizeHeight: expected_height,
resizeQuality: 'high'
}))
.push(function (bitmap) {
var canvas = domsugar('canvas');
canvas.width = bitmap.width;
canvas.height = bitmap.height;
canvas.getContext('2d').drawImage(bitmap, 0, 0);
return promiseCanvasToBlob(canvas);
}); });
} }
...@@ -575,15 +553,9 @@ ...@@ -575,15 +553,9 @@
}) })
.push(function (blob) { .push(function (blob) {
gadget.detached_promise_dict.media_stream.cancel('Not needed anymore, as captured'); gadget.detached_promise_dict.media_stream.cancel('Not needed anymore, as captured');
return resizePhoto(blob, original_width, original_height); return fixPhotoOrientationSizeColor(blob, original_width,
}) original_height, settings);
.push(function (blob) {
return fixPhotoOrientation(blob);
}) })
.push(function (blob) {
return fixPhotoColor(blob, settings);
})
.push(function (blob) { .push(function (blob) {
blob_url = URL.createObjectURL(blob); blob_url = URL.createObjectURL(blob);
return RSVP.all([ return RSVP.all([
......
...@@ -244,7 +244,7 @@ ...@@ -244,7 +244,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>982.58354.63381.1604</string> </value> <value> <string>982.58483.10473.65433</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>1585650508.06</float> <float>1585658339.7</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