2020-10-16 13:50:05 +00:00
$ ( document ) . ready ( function ( ) {
2020-11-05 15:50:49 +00:00
2020-11-05 08:49:18 +00:00
Cropper . setDefaults ( {
viewMode : 1 ,
autoCropArea : 0.9
} )
2020-10-16 13:50:05 +00:00
// Grab elements, create settings, etc.
var video = document . getElementById ( 'video' ) ;
2020-11-05 15:50:49 +00:00
var snapCanvas = document . getElementById ( 'canvas' ) ;
var snapContext = snapCanvas . getContext ( '2d' ) ;
var simuCanvas = document . getElementById ( "simuCanvas" ) ;
var simuContext = simuCanvas . getContext ( "2d" ) ;
var simuLastPoint = { x : 0 , y : 0 , color : 0 } ;
var simuZoom = 1 ;
var binary _level = 100
var url = document . location . origin
var cropper = null
var snap = null
/ *
Helpers
* /
2020-10-16 13:50:05 +00:00
var errBack = function ( e ) {
console . log ( 'An error has occurred!' , e )
} ;
2020-11-05 15:50:49 +00:00
var Point = function ( l ) {
return {
"x" : l [ 0 ] ,
"y" : l [ 1 ] ,
"color" : l [ 2 ]
}
}
2020-10-31 21:54:55 +00:00
/ * *
Take a new snapshot from the camera and store it
* * /
2020-11-05 15:50:49 +00:00
function videoToSnap ( ) {
showSnap ( video , video . width , video . height )
}
function showSnap ( src , width , height ) {
2020-11-17 21:18:08 +00:00
canvas . height = height
2020-10-31 21:54:55 +00:00
cropper && cropper . destroy ( )
2020-11-05 15:50:49 +00:00
snapContext . drawImage ( src , 0 , 0 , width , height ) ;
snap = snapContext . createImageData ( width , height ) ;
snap . data . set ( snapContext . getImageData ( 0 , 0 , width , height ) . data )
binary ( snapContext )
$ ( ".result" ) . show ( )
cropper = new Cropper ( snapCanvas ) ;
2020-11-05 08:49:18 +00:00
// Animation removed: it was causing problems with the send button. Fix.
2020-11-01 22:09:28 +00:00
//$('html, body').animate({scrollTop: $("#result").offset().top}, 2000);
2020-10-31 21:54:55 +00:00
}
function doContrast ( ) {
2020-11-05 08:49:18 +00:00
let cropperData = { }
if ( cropper ) {
2020-11-05 15:50:49 +00:00
cropperData = cropper . getData ( )
cropper . destroy ( )
2020-11-05 08:49:18 +00:00
}
2020-11-05 15:50:49 +00:00
snapContext . putImageData ( snap , 0 , 0 )
binary ( snapContext )
cropper = new Cropper ( snapCanvas ) ;
2020-11-05 08:49:18 +00:00
setTimeout ( ( ) => { cropper . setData ( cropperData ) ; } , 30 )
2020-10-16 13:50:05 +00:00
}
2020-11-05 15:50:49 +00:00
/ *
To convert image into binary , we will threshold it .
Based upon the threshold value
thresh _red , thresh _blue , thresh _green == > are the respective red , blue and green color threshold values . Any thing above this threshold value will be denoted by white color and anything below will be black
* /
function binary ( context ) {
2020-10-16 13:50:05 +00:00
var image = context . getImageData ( 0 , 0 , context . canvas . width , context . canvas . height ) ;
var channels = image . data . length / 4 ;
for ( var i = 0 ; i < channels ; i ++ ) {
var r = image . data [ i * 4 + 0 ] ;
var g = image . data [ i * 4 + 1 ] ;
var b = image . data [ i * 4 + 2 ] ;
2020-11-05 15:50:49 +00:00
if ( r >= binary _level ) {
2020-10-16 13:50:05 +00:00
image . data [ i * 4 + 0 ] = 255 ;
image . data [ i * 4 + 1 ] = 255 ;
image . data [ i * 4 + 2 ] = 255 ;
} else {
image . data [ i * 4 + 0 ] = 0 ;
image . data [ i * 4 + 1 ] = 0 ;
image . data [ i * 4 + 2 ] = 0 ;
}
}
context . putImageData ( image , 0 , 0 ) ;
}
2020-10-21 12:39:48 +00:00
2020-11-05 15:50:49 +00:00
function processFiles ( files ) {
2020-11-17 21:18:08 +00:00
var file , img ;
var _URL = window . URL || window . webkitURL ;
if ( ( file = files [ 0 ] ) ) {
img = new Image ( ) ;
var objectUrl = _URL . createObjectURL ( file ) ;
img . onload = function ( ) {
let w = 640
let h = 640 * this . height / this . width
showSnap ( this , w , h )
} ;
img . src = objectUrl ;
}
2020-11-05 15:50:49 +00:00
if ( files && typeof FileReader !== "undefined" ) {
for ( var i = 0 ; i < files . length ; i ++ ) {
2020-11-17 21:18:08 +00:00
// fileToSnap(files[i]);
2020-11-05 15:50:49 +00:00
}
2020-11-01 22:09:28 +00:00
}
}
2020-11-05 15:50:49 +00:00
var fileToSnap = function ( file ) {
if ( ( /image/i ) . test ( file . type ) ) {
var reader = new FileReader ( ) ;
reader . onload = function ( e ) {
let base _image = new Image ( ) ;
base _image . src = e . target . result ;
2020-11-17 21:18:08 +00:00
console . log (
base _image . width
)
2020-11-05 15:50:49 +00:00
base _image . width = 640 ;
base _image . onload = function ( ) {
showSnap ( base _image , 640 , 480 )
}
} ;
reader . readAsDataURL ( file ) ;
}
}
2020-11-01 22:09:28 +00:00
// Draws every segment received, except black colored target ones
var drawSimu = function ( ) {
pointsList = document . pointsList
if ( pointsList . length > 0 )
{
2020-11-05 15:50:49 +00:00
simuContext . clearRect ( 0 , 0 , simuCanvas . width , simuCanvas . height ) ;
simuLastPoint = new Point ( pointsList [ 0 ] )
2020-11-01 22:09:28 +00:00
for ( var i = 0 ; i < pointsList . length ; i += 1 )
{
point = new Point ( pointsList [ i ] )
// if the target is black, skip drawing
if ( point . color != 0 ) {
2020-11-05 15:50:49 +00:00
simuContext . beginPath ( )
simuContext . shadowBlur = 5 ;
simuContext . shadowColor = 'rgba(255, 255, 255, 1)' ;
simuContext . lineWidth = 2 ;
simuContext . strokeStyle = "#" + ( point . color + Math . pow ( 16 , 6 ) ) . toString ( 16 ) . slice ( - 6 ) ;
simuContext . moveTo ( simuLastPoint . x * simuZoom , simuLastPoint . y * simuZoom ) ;
simuContext . lineTo ( point . x * simuZoom , point . y * simuZoom ) ;
simuContext . stroke ( ) ;
simuContext . closePath ( )
2020-11-01 22:09:28 +00:00
}
2020-11-05 15:50:49 +00:00
simuLastPoint = point
2020-11-01 22:09:28 +00:00
}
}
}
2020-11-05 15:50:49 +00:00
2020-11-01 22:09:28 +00:00
function refreshImageList ( ) {
var imageList = localStorage . getItem ( "imageList" ) ? JSON . parse ( localStorage . getItem ( "imageList" ) ) : [ ]
2020-11-01 22:28:34 +00:00
if ( 0 == imageList . length ) {
$ ( "#preview" ) . hide ( )
return
}
$ ( "#preview" ) . show ( )
2020-11-01 22:09:28 +00:00
html = ''
2020-11-05 08:49:18 +00:00
for ( id in imageList . reverse ( ) ) {
2020-11-01 22:09:28 +00:00
hash _name = imageList [ id ]
2020-11-05 08:49:18 +00:00
html += ` <li class="list-group-item"><a href="# ${ hash _name } " class="hash_name btn-sm btn btn-secondary" rel=" ${ hash _name } "> ${ hash _name } </a></li> `
2020-11-01 22:09:28 +00:00
}
2020-11-05 08:49:18 +00:00
$ ( "#imageList" ) . html ( ` <ul class="list-group"> ${ html } </ul> ` )
2020-11-01 22:09:28 +00:00
}
2020-11-05 15:50:49 +00:00
2020-11-01 22:09:28 +00:00
2020-11-05 08:49:18 +00:00
var showImage = function ( e ) {
var el = $ ( e . target )
if ( ! el . hasClass ( "hash_name" ) ) { return ; }
var hash _name = el . attr ( "rel" )
2020-11-01 22:09:28 +00:00
var u = ` ${ url } /hash_name/ ${ hash _name } `
$ . ajax ( {
2020-11-05 08:49:18 +00:00
timeout : 3000 ,
2020-11-01 22:09:28 +00:00
url : ` ${ url } /hash_name/ ${ hash _name } ` ,
dataType : "json" ,
} ) . done ( function ( d , m , j ) {
if ( d . errors ) {
msg = d . errors . join ( " / " )
$ ( "#messages" ) . html ( ` <div class="alert alert-danger" role="alert">Something went wrong: ${ msg } </div> ` )
return
}
$ ( "#messages" ) . html ( "" )
document . pointsList = d . points _list
drawSimu ( )
} )
. fail ( function ( d , m , jq ) {
$ ( "#messages" ) . html ( ` <div class="alert alert-danger" role="alert">Ooops... Something went wrong: ${ jq } </div> ` )
} )
2020-11-05 08:49:18 +00:00
}
2020-11-01 22:09:28 +00:00
2020-11-05 15:50:49 +00:00
// Trigger photo take
$ ( '.snap' ) . on ( 'click' , function ( ) {
videoToSnap ( )
} ) ;
$ ( "#slider" ) . slider ( {
range : false ,
value : 100 ,
animate : 300 ,
min : 0 ,
max : 255 ,
change : function ( event , ui ) {
binary _level = ui . value
doContrast ( )
}
} ) ;
2020-11-05 08:49:18 +00:00
$ ( ".contrast" ) . on ( "click" , function ( el ) {
let btn = el . target
let val = Number ( $ ( btn ) . attr ( "rel" ) )
let new _binary _level = binary _level + val ;
if ( new _binary _level >= 0 && new _binary _level <= 255 ) {
2020-11-05 15:50:49 +00:00
binary _level = new _binary _level
$ ( "#slider" ) . slider ( "option" , "value" , binary _level )
2020-11-05 08:49:18 +00:00
}
} )
2020-11-01 22:09:28 +00:00
2020-11-05 15:50:49 +00:00
$ ( "#imageList" ) . on ( "click" , showImage )
$ ( "#messages" ) . on ( "click" , showImage )
$ ( "#send" ) . on ( "click" , function ( ) {
var cropCanvas = cropper . getCroppedCanvas ( ) ;
image _data = cropCanvas . toDataURL ( ) ;
$ ( "#messages" ) . html ( '<div class="alert alert-primary" role="alert">Sending...</div>' )
$ . ajax ( {
timeout : 3000 ,
url : url + "/image" ,
method : "POST" ,
dataType : "json" ,
data : {
image _data : image _data ,
text : $ ( "#text" ) . val ( )
} ,
} ) . done ( function ( d , m , j ) {
if ( d . errors ) {
msg = d . errors . join ( " / " )
$ ( "#messages" ) . html ( ` <div class="alert alert-danger" role="alert">Something went wrong: ${ msg } </div> ` )
return
}
$ ( "#messages" ) . html ( ` <div class="alert alert-success" role="alert">OK, your image will be available in a few seconds as <a href="# ${ d . hash _name } " class="hash_name btn btn-sm btn-secondary" rel=" ${ d . hash _name } "> ${ d . hash _name } </a></div> ` )
imageList = localStorage . getItem ( "imageList" ) ? JSON . parse ( localStorage . getItem ( "imageList" ) ) : [ ]
imageList . push ( d . hash _name )
localStorage . setItem ( "imageList" , JSON . stringify ( imageList ) )
refreshImageList ( )
} )
. fail ( function ( d , m , jq ) {
$ ( "#messages" ) . html ( ` <div class="alert alert-danger" role="alert">Ooops... Something went wrong: ${ jq } </div> ` )
} )
} )
$ ( '#uploadBtn' ) . on ( 'click' , function ( e ) {
e . stopPropagation ( ) ;
e . preventDefault ( ) ;
//trigger default file upload button
uploadInput . click ( ) ;
} ) ;
$ ( '#uploadInput' ) . on ( 'change' , function ( ) {
//retrieve selected uploaded files data
var files = $ ( this ) [ 0 ] . files ;
processFiles ( files ) ;
return false ;
} ) ;
refreshImageList ( )
2020-10-16 13:50:05 +00:00
} )