Advertisement
Guest User

NudeCheck

a guest
Jan 15th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Nude.js - Nudity detection with Javascript and HTMLCanvas
  3.  *
  4.  * Author: Patrick Wied ( http://www.patrick-wied.at )
  5.  * Version: 0.1  (2010-11-21)
  6.  * License: MIT License
  7.  */
  8. (function(){
  9.  
  10.     var nude = (function(){
  11.         // private var definition
  12.         var canvas = null,
  13.         ctx = null,
  14.         resultFn = null,
  15.         // private functions
  16.         initCanvas = function(){
  17.             canvas = document.createElement("canvas");
  18.             // the canvas should not be visible
  19.             canvas.style.display = "none";
  20.             var b = document.getElementsByTagName("body")[0];
  21.             b.appendChild(canvas);
  22.             ctx = canvas.getContext("2d");
  23.         },
  24.         loadImageById = function(id){
  25.             // get the image
  26.             var img = document.getElementById(id);
  27.             // apply the width and height to the canvas element
  28.             canvas.width = img.width;
  29.             canvas.height = img.height;
  30.             // reset the result function
  31.             resultFn = null;
  32.             // draw the image into the canvas element
  33.             ctx.drawImage(img, 0, 0);
  34.  
  35.         },
  36.         scanImage = function(){
  37.             // get the image data
  38.             var image = ctx.getImageData(0, 0, canvas.width, canvas.height),
  39.             imageData = image.data;
  40.  
  41.             var myWorker = new Worker('worker.nude.js'),
  42.             message = [imageData, canvas.width, canvas.height];
  43.             myWorker.postMessage(message);
  44.             myWorker.onmessage = function(event){
  45.                 resultHandler(event.data);
  46.             }
  47.         },
  48.         // the result handler will be executed when the analysing process is done
  49.         // the result contains true (it is nude) or false (it is not nude)
  50.         // if the user passed an result function to the scan function, the result function will be executed
  51.         resultHandler = function(result){
  52.            
  53.             if(resultFn){
  54.                 resultFn(result);
  55.             }else{
  56.                 if(result)
  57.                     console.log("the picture contains nudity");
  58.             }
  59.            
  60.         }
  61.         // public interface
  62.         return {
  63.             init: function(){
  64.                 initCanvas();
  65.                 // if web worker are not supported, append the noworker script
  66.                 if(!!!window.Worker){
  67.                     document.write(unescape("%3Cscript src='noworker.nude.js' type='text/javascript'%3E%3C/script%3E"));
  68.                 }
  69.                    
  70.             },
  71.             load: function(param){
  72.                 loadImageById(param);
  73.             },
  74.             scan: function(fn){
  75.                 if(arguments.length>0 && typeof(arguments[0]) == "function"){
  76.                     resultFn = fn;
  77.                 }
  78.                 scanImage();
  79.             }
  80.         };
  81.     })();
  82.     // register nude at window object
  83.     if(!window.nude)
  84.         window.nude = nude;
  85.     // and initialize it
  86.     nude.init();
  87. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement