2015-01-02 105 views
0

我需要什麼遺漏的類型錯誤:使用Nude.js

  • 我需要掃描和檢查圖像裸體與否不能看空的特性「寬度」。

  • 我使用nude.js進行圖像檢測問題im包含nude.js後加載頁面時,錯誤出現在控制檯中。

nude.js

(function(){ 

var nude = (function(){ 
    // private var definition 
    var canvas = null, 
    ctx = null, 
    resultFn = null, 
    // private functions 
    initCanvas = function(){ 
     canvas = document.createElement("canvas"); 
     // the canvas should not be visible 
     canvas.style.display = "none"; 
     var b = document.getElementsByTagName("body")[0]; 
     b.appendChild(canvas); 
     ctx = canvas.getContext("2d"); 
    }, 
    loadImageById = function(id){ 
     // get the image 
     var img = document.getElementById(id); 
     // apply the width and height to the canvas element 
     canvas.width = img.width; 
     canvas.height = img.height; 
     // reset the result function 
     resultFn = null; 
     // draw the image into the canvas element 
     ctx.drawImage(img, 0, 0); 

    }, 
    scanImage = function(){ 
     // get the image data 
     var image = ctx.getImageData(0, 0, canvas.width, canvas.height), 
     imageData = image.data; 

     var myWorker = new Worker('worker.nude.js'), 
     message = [imageData, canvas.width, canvas.height]; 
     myWorker.postMessage(message); 
     myWorker.onmessage = function(event){ 
      resultHandler(event.data); 
     } 
    }, 

abc.html

<script> 
$(document).ready(function(){ 
$("img").load(function(){ 

nude.load('testImage'); 
nude.scan(function(result) 
{ 

if(!result) alert("no nude"); 
else 
alert("nude)"); }); 
}); 
}); 

錯誤

Uncaught TypeError: Cannot read property 'width' of null 
    canvas.width = img.width; // line 28 
+1

這意味着'document.getElementById(id)'沒有找到任何東西。 – Barmar

+0

你的代碼調用'nude.load()',這個問題沒有在問題中顯示的代碼中定義。假設它在'loadImageById()'方法中結束,那麼該方法實際上不會測試指定的元素是否存在。你確定你有正確的元素ID嗎? – nnnnnn

+0

未捕獲SecurityError:未能在'CanvasRenderingContext2D'上執行'getImageData':畫布已被交叉源數據污染。 – user2818060

回答

0

ÿ您需要撥打nude.init();。我今天面臨同樣的問題。即使nude.js在將裸體對象註冊到窗口後自稱爲nude.init() - 但它似乎不會發生。 邊注:canvas本身爲空。錯誤來自canvas,而不是來自img

包括jQuery和調用$(document).ready(...)並在那裏做一個nude.init();