function receiveMessage(e) {
alert("received message");
if (e.data.success === "log") {
console.log(e.data.result);
return;
}
if (e.data.finished) {
workerCount--;
if (workerCount) {
if (resultArray.length == 0) {
DecodeWorker.postMessage({ ImageData: ctx.getImageData(0, 0, Canvas.width, Canvas.height).data, Width: Canvas.width, Height: Canvas.height, cmd: "flip", Decode: "Code39"});
} else {
workerCount--;
}
}
}
if (e.data.success) {
var tempArray = e.data.result;
for (var i = 0; i < tempArray.length; i++) {
if (resultArray.indexOf(tempArray[i]) == -1) {
resultArray.push(tempArray[i]);
}
}
alert(resultArray[0]);
} else {
if (resultArray.length === 0 && workerCount === 0) {
alert("Decoding failed.");
}
}
}
var DecodeWorker = new Worker("js/BarcodeScanner.js");
DecodeWorker.onmessage = receiveMessage;
// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64 encoded image data
// console.log(imageData);
smallPicture = document.getElementById('smallImage');
// Get image handle
//
// Unhide image elements
//
smallPicture.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallPicture.onload = function () {
ctx.drawImage(smallPicture, 0, 0, Canvas.width, Canvas.height);
resultArray = [];
workerCount = 2;
alert("Prepost");
barcodeNum("");
alert("Post");
DecodeWorker.postMessage({ ImageData: ctx.getImageData(0, 0, Canvas.width, Canvas.height).data, Width: Canvas.width, Height: Canvas.height, cmd: "normal", Decode: "Code39" });
};
smallPicture.src = "data:image/jpeg;base64," + imageData;
}
在我的decodeWorker上使用postMessage函数时,从不调用receiveMessage函数,我无法弄清楚我做错了究竟是什么,似乎最糟糕的情况我至少会看到收到消息'我设置的警报,但即使这样也不会发生。