Commit dd04c06e authored by Gabriel Monnerat's avatar Gabriel Monnerat

erp5_document_scanner: make jslint happy

parent d9ff6c56
(function (rJS, RSVP, window, document, navigator, Cropper, console, alert, FileReader, URL, jIO) { /*jslint indent: 2 */
/*global rJS, RSVP, window, document, navigator, Cropper, alert, FileReader, jIO */
(function (rJS, RSVP, window, document, navigator, Cropper, alert, FileReader, jIO) {
"use strict"; "use strict";
var imageWidth, var imageWidth,
...@@ -18,24 +20,92 @@ ...@@ -18,24 +20,92 @@
return imageCapture.getPhotoCapabilities(); return imageCapture.getPhotoCapabilities();
} }
function takePicture(gadget) {
imageCapture.takePhoto({imageWidth: imageWidth})
.then(function (blob) {
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function () {
photoInput.setAttribute("value", reader.result.split(",")[1]);
photo.setAttribute("src", reader.result);
photo.setAttribute("width", imageWidth);
photo.setAttribute("height", imageHeight);
return drawCanvas(gadget, photo);
};
});
}
function drawCanvas(gadget, img) {
var ratio, x, y;
canvas.width = imageWidth;
canvas.height = imageHeight;
ratio = Math.min(canvas.width / img.width, canvas.height / img.height);
x = (canvas.width - img.width * ratio) / 2;
y = (canvas.height - img.height * ratio) / 2;
document.querySelector("textarea[name='field_your_description']").value += "\nImage size " + img.width + "x" + img.height;
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height, x, y, img.width * ratio, img.height * ratio);
//grayscale(canvas, canvas);
//contrastImage(canvas, canvas, 10);
gadget.querySelector(".camera-output").style.display = "";
if (cropper) {
cropper.destroy();
}
return RSVP.Queue()
.push(function () {
return storage.get("settings");
})
.push(function (data) {
cropper = new Cropper(gadget.querySelector('.photo'), {data: data});
gadget.querySelector(".crop-button").addEventListener("click", function (evt) {
var canvasData;
evt.preventDefault();
canvasData = cropper.getCanvasData();
storage.put("settings", cropper.getData());
cropper.getCroppedCanvas().toBlob(function (blob) {
var reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function () {
var base64data = reader.result,
block = base64data.split(";"),
realData = block[1].split(",")[1];
photo.style.width = canvasData.width + "px";
photo.style.height = canvasData.height + "px";
photo.src = base64data;
photoInput.value = realData;
cropper.destroy();
};
});
});
});
}
function contrastImage(input, output, contrast) { function contrastImage(input, output, contrast) {
var outputContext, var i,
outputContext,
inputContext = input.getContext("2d"), inputContext = input.getContext("2d"),
imageData = inputContext.getImageData(0, 0, input.width, input.height), imageData = inputContext.getImageData(0, 0, input.width, input.height),
data = imageData.data, data = imageData.data,
factor = (259 * (contrast + 255)) / (255 * (259 - contrast)); factor = (259 * (contrast + 255)) / (255 * (259 - contrast));
for (var i=0;i<data.length;i+=4) { for (i = 0; i < data.length; i += 4) {
data[i] = factor * (data[i] - 128) + 128; data[i] = factor * (data[i] - 128) + 128;
data[i+1] = factor * (data[i+1] - 128) + 128; data[i + 1] = factor * (data[i + 1] - 128) + 128;
data[i+2] = factor * (data[i+2] - 128) + 128; data[i + 2] = factor * (data[i + 2] - 128) + 128;
} }
outputContext = output.getContext("2d"); outputContext = output.getContext("2d");
outputContext.putImageData(imageData, 0, 0); outputContext.putImageData(imageData, 0, 0);
} }
function grayscale (input, output) { function grayscale(input, output) {
var gray, var i,
gray,
outputContext, outputContext,
inputContext = input.getContext("2d"), inputContext = input.getContext("2d"),
imageData = inputContext.getImageData(0, 0, input.width, input.height), imageData = inputContext.getImageData(0, 0, input.width, input.height),
...@@ -44,18 +114,17 @@ ...@@ -44,18 +114,17 @@
//gray = 0.3*R + 0.59*G + 0.11*B //gray = 0.3*R + 0.59*G + 0.11*B
// http://www.tannerhelland.com/3643/grayscale-image-algorithm-vb6/ // http://www.tannerhelland.com/3643/grayscale-image-algorithm-vb6/
for (var i=arraylength-1; i>0;i-=4) { for (i = arraylength - 1; i > 0; i -= 4) {
gray = 0.3 * data[i-3] + 0.59 * data[i-2] + 0.11 * data[i-1]; gray = 0.3 * data[i - 3] + 0.59 * data[i - 2] + 0.11 * data[i - 1];
data[i-3] = gray; data[i - 3] = gray;
data[i-2] = gray; data[i - 2] = gray;
data[i-1] = gray; data[i - 1] = gray;
} }
outputContext = output.getContext("2d"); outputContext = output.getContext("2d");
outputContext.putImageData(imageData, 0, 0); outputContext.putImageData(imageData, 0, 0);
} }
function startup(gadget, device_id) { function startup(gadget, device_id) {
var media, mediaList = [];
video = gadget.querySelector(".video"); video = gadget.querySelector(".video");
canvas = gadget.querySelector(".canvas"); canvas = gadget.querySelector(".canvas");
photo = gadget.querySelector(".photo"); photo = gadget.querySelector(".photo");
...@@ -63,28 +132,27 @@ ...@@ -63,28 +132,27 @@
startbutton = gadget.querySelector(".startbutton"); startbutton = gadget.querySelector(".startbutton");
return RSVP.Queue() return RSVP.Queue()
.push(function() { .push(function () {
return navigator.mediaDevices.getUserMedia({video: {deviceId: {exact: device_id}}}); return navigator.mediaDevices.getUserMedia({video: {deviceId: {exact: device_id}}});
}) })
.push(gotStream) .push(gotStream)
.push(function(photoCapabilities) { .push(function (photoCapabilities) {
imageWidth = photoCapabilities.imageWidth.max; imageWidth = photoCapabilities.imageWidth.max;
imageHeight = photoCapabilities.imageHeight.max; imageHeight = photoCapabilities.imageHeight.max;
document.querySelector("textarea[name='field_your_description']").value = "Max => " + imageWidth + "x" + imageHeight; document.querySelector("textarea[name='field_your_description']").value = "Max => " + imageWidth + "x" + imageHeight;
video.play(); video.play();
}) })
.push(function(){ .push(function () {
/* /*
XXX remove addEventListener. Instead, use renderJS declareService / onEvent, XXX remove addEventListener. Instead, use renderJS declareService / onEvent,
which will handle unloading the listener and correctly catching errors which will handle unloading the listener and correctly catching errors
Remove soon Remove soon
*/ */
startbutton.addEventListener("click", function(evt){ startbutton.addEventListener("click", function (evt) {
evt.preventDefault(); evt.preventDefault();
takePicture(gadget); takePicture(gadget);
}, false); }, false);
}); });
} }
function clearphoto() { function clearphoto() {
...@@ -95,75 +163,8 @@ ...@@ -95,75 +163,8 @@
photo.setAttribute("src", data); photo.setAttribute("src", data);
} }
function takePicture(gadget) {
imageCapture.takePhoto({imageWidth: imageWidth})
.then(function(blob){
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
photoInput.setAttribute("value", reader.result.split(",")[1]);
photo.setAttribute("src", reader.result);
photo.setAttribute("width", imageWidth);
photo.setAttribute("height", imageHeight);
return drawCanvas(gadget, photo);
};
});
}
function drawCanvas(gadget, img) {
var ratio, x, y, data;
canvas.width = imageWidth;
canvas.height = imageHeight;
ratio = Math.min(canvas.width / img.width, canvas.height / img.height);
x = (canvas.width - img.width * ratio) / 2;
y = (canvas.height - img.height * ratio) / 2;
document.querySelector("textarea[name='field_your_description']").value += "\nImage size " + img.width + "x" + img.height;
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height, x, y, img.width * ratio, img.height * ratio);
//grayscale(canvas, canvas);
//contrastImage(canvas, canvas, 10);
gadget.querySelector(".camera-output").style.display = "";
if (cropper) {
cropper.destroy();
}
return RSVP.Queue()
.push(function () {
return storage.get("settings");
})
.push(function (data) {
cropper = new Cropper(gadget.querySelector('.photo'), {data: data});
gadget.querySelector(".crop-button").addEventListener("click", function(evt) {
var canvasData;
evt.preventDefault();
canvasData = cropper.getCanvasData();
storage.put("settings", cropper.getData());
cropper.getCroppedCanvas().toBlob(function(blob){
var reader = new window.FileReader(),
photo = gadget.querySelector(".photo");
reader.readAsDataURL(blob);
reader.onloadend = function () {
var base64data = reader.result,
block = base64data.split(";"),
contentType = block[0].split(":")[1],
realData = block[1].split(",")[1];
photo.style.width = canvasData.width + "px";
photo.style.height = canvasData.height + "px";
photo.src = base64data;
photoInput.value = realData;
cropper.destroy();
};
});
});
});
}
rJS(window) rJS(window)
.ready(function (g) { .ready(function () {
return RSVP.Queue() return RSVP.Queue()
.push(function () { .push(function () {
return jIO.createJIO({ return jIO.createJIO({
...@@ -175,11 +176,11 @@ ...@@ -175,11 +176,11 @@
storage = proxy; storage = proxy;
return storage.get("settings"); return storage.get("settings");
}) })
.then(undefined, function (error) { .then(undefined, function () {
return storage.put("settings"); return storage.put("settings");
}); });
}) })
.declareMethod('render', function (options) { .declareMethod('render', function () {
var el, var el,
root, root,
selector; selector;
...@@ -188,7 +189,7 @@ ...@@ -188,7 +189,7 @@
root = element; root = element;
selector = element.querySelector("select"); selector = element.querySelector("select");
if (!navigator.mediaDevices) { if (!navigator.mediaDevices) {
throw("mediaDevices is not supported"); throw ("mediaDevices is not supported");
} }
return navigator.mediaDevices.enumerateDevices(); return navigator.mediaDevices.enumerateDevices();
}) })
...@@ -206,8 +207,8 @@ ...@@ -206,8 +207,8 @@
} }
} }
}) })
.push(function() { .push(function () {
selector.addEventListener("change", function(evt) { selector.addEventListener("change", function (evt) {
if (video) { if (video) {
video.pause(); video.pause();
} }
...@@ -216,7 +217,7 @@ ...@@ -216,7 +217,7 @@
} }
}); });
}) })
.then(undefined, function(e) { .then(undefined, function (e) {
alert(e); alert(e);
}); });
}) })
...@@ -227,4 +228,4 @@ ...@@ -227,4 +228,4 @@
return result; return result;
}); });
}(rJS, RSVP, window, document, navigator, Cropper, console, alert, FileReader, URL, jIO)); }(rJS, RSVP, window, document, navigator, Cropper, alert, FileReader, jIO));
\ No newline at end of file \ No newline at end of file
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