未捕获的TypeError:无法使用Nude.js读取Null的属性“width”

时间:2015-01-02 05:34:01

标签: javascript jquery nude.js

我需要什么

  • 我需要扫描并检查图像是否裸色。

  • 我使用nude.js进行图像检测问题我在面对包含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 个答案:

答案 0 :(得分:0)

您需要致电nude.init();。我今天遇到了同样的问题。即使nude.js在将裸体对象注册到窗口后自称为nude.init(),但它似乎并未发生。 旁注:canvas本身为空。错误来自canvas,而不是来自img

包含jQuery并致电$(document).ready(...)并在其中执行nude.init();