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 函数,而且我无法弄清楚我做错了什么,似乎最坏的情况我至少会看到“收到的消息”警报我设置的,但即使这样也不会发生。