Commit db287a77 authored by Romain Courteaud's avatar Romain Courteaud

erp5_document_scanner: reduce number of createImageBitmap calls

parent fa3f859e
......@@ -418,19 +418,36 @@
});
}
function fixPhotoOrientation(blob) {
function fixPhotoOrientationSizeColor(blob, original_width, original_height,
settings) {
var orientation;
return getOrientation(blob)
.push(function (result) {
orientation = result;
var expected_width = 2000,
bitmap_options,
div;
// 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;
}
if (expected_width < original_width) {
bitmap_options = {
resizeWidth: expected_width,
// resizeHeight: expected_height,
resizeQuality: 'high'
};
}
// Else, transform the image
return new RSVP.Queue(createImageBitmap(blob))
return new RSVP.Queue(createImageBitmap(blob, bitmap_options))
.push(function (bitmap) {
var height = bitmap.height,
......@@ -438,6 +455,10 @@
canvas = domsugar('canvas'),
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) {
canvas.width = height;
canvas.height = width;
......@@ -475,57 +496,14 @@
break;
}
ctx.drawImage(bitmap, 0, 0);
return promiseCanvasToBlob(canvas);
});
});
}
function fixPhotoColor(blob, settings) {
if (!(settings.brightness || settings.contrast || settings.enable_greyscale)) {
return blob;
}
var div;
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);
if (settings.brightness || settings.contrast || settings.enable_greyscale) {
return handleCaman(canvas, settings);
}
})
.push(function () {
return promiseCanvasToBlob(div.firstElementChild);
});
});
}
......@@ -575,15 +553,9 @@
})
.push(function (blob) {
gadget.detached_promise_dict.media_stream.cancel('Not needed anymore, as captured');
return resizePhoto(blob, original_width, original_height);
})
.push(function (blob) {
return fixPhotoOrientation(blob);
return fixPhotoOrientationSizeColor(blob, original_width,
original_height, settings);
})
.push(function (blob) {
return fixPhotoColor(blob, settings);
})
.push(function (blob) {
blob_url = URL.createObjectURL(blob);
return RSVP.all([
......
......@@ -244,7 +244,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>982.58354.63381.1604</string> </value>
<value> <string>982.58483.10473.65433</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -262,7 +262,7 @@
</tuple>
<state>
<tuple>
<float>1585650508.06</float>
<float>1585658339.7</float>
<string>UTC</string>
</tuple>
</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