2020-10-16 13:50:05 +00:00
$ ( document ) . ready ( function ( ) {
var binary _level = 100
2020-11-01 22:09:28 +00:00
var url = document . location . origin
2020-10-31 21:54:55 +00:00
var cropper = null
var snap = null
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 canvas = document . getElementById ( 'canvas' ) ;
var context = canvas . getContext ( '2d' ) ;
var video = document . getElementById ( 'video' ) ;
var mediaConfig = { video : true } ;
var errBack = function ( e ) {
console . log ( 'An error has occurred!' , e )
} ;
2020-10-31 21:54:55 +00:00
/ * *
Take a new snapshot from the camera and store it
* * /
function doSnap ( ) {
cropper && cropper . destroy ( )
2020-11-05 08:49:18 +00:00
let width = video . width
let height = video . height
2020-10-31 21:54:55 +00:00
context . drawImage ( video , 0 , 0 , width , height ) ;
snap = context . createImageData ( width , height ) ;
snap . data . set ( context . getImageData ( 0 , 0 , width , height ) . data )
2020-10-16 13:50:05 +00:00
binary ( context )
$ ( "#result" ) . show ( )
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-11-05 08:49:18 +00:00
cropper = new Cropper ( canvas ) ;
2020-10-31 21:54:55 +00:00
}
function doContrast ( ) {
2020-11-05 08:49:18 +00:00
let cropperData = { }
if ( cropper ) {
cropperData = cropper . getData ( )
cropper . destroy ( )
}
2020-10-31 21:54:55 +00:00
context . putImageData ( snap , 0 , 0 )
binary ( context )
2020-11-05 08:49:18 +00:00
cropper = new Cropper ( canvas ) ;
setTimeout ( ( ) => { cropper . setData ( cropperData ) ; } , 30 )
2020-10-16 13:50:05 +00:00
}
// Trigger photo take
2020-10-21 12:39:48 +00:00
$ ( '.snap' ) . on ( 'click' , function ( ) {
2020-10-31 21:54:55 +00:00
doSnap ( )
2020-10-16 13:50:05 +00:00
} ) ;
function binary ( context ) {
/ *
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
* /
var image = context . getImageData ( 0 , 0 , context . canvas . width , context . canvas . height ) ;
var thresh _red = binary _level ;
var thresh _green = binary _level ;
var thresh _blue = binary _level ;
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 ] ;
if ( r >= thresh _red && g >= thresh _green && b >= thresh _blue ) {
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 ) ;
}
$ ( "#result" ) . hide ( )
2020-11-05 08:49:18 +00:00
var slider = $ ( "#slider" ) . slider ( {
2020-10-16 13:50:05 +00:00
range : false ,
2020-11-05 08:49:18 +00:00
value : 100 ,
animate : 300 ,
2020-10-16 13:50:05 +00:00
min : 0 ,
max : 255 ,
change : function ( event , ui ) {
binary _level = ui . value
2020-11-05 08:49:18 +00:00
console . log ( ` binary_level: ${ binary _level } ` )
2020-10-31 21:54:55 +00:00
doContrast ( )
2020-10-16 13:50:05 +00:00
}
} ) ;
$ ( "#send" ) . on ( "click" , function ( ) {
2020-10-31 21:54:55 +00:00
var cropCanvas = cropper . getCroppedCanvas ( ) ;
image _data = cropCanvas . toDataURL ( ) ;
2020-10-21 12:39:48 +00:00
$ ( "#messages" ) . html ( '<div class="alert alert-primary" role="alert">Sending...</div>' )
$ . ajax ( {
2020-11-05 08:49:18 +00:00
timeout : 3000 ,
2020-10-21 12:39:48 +00:00
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
}
2020-11-05 08:49:18 +00:00
$ ( "#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"> ${ d . hash _name } </a></div> ` )
2020-10-21 12:39:48 +00:00
imageList = localStorage . getItem ( "imageList" ) ? JSON . parse ( localStorage . getItem ( "imageList" ) ) : [ ]
imageList . push ( d . hash _name )
localStorage . setItem ( "imageList" , JSON . stringify ( imageList ) )
2020-11-01 22:09:28 +00:00
refreshImageList ( )
2020-10-21 12:39:48 +00:00
} )
. fail ( function ( d , m , jq ) {
$ ( "#messages" ) . html ( ` <div class="alert alert-danger" role="alert">Ooops... Something went wrong: ${ jq } </div> ` )
} )
2020-10-16 13:50:05 +00:00
} )
2020-10-31 21:54:55 +00:00
2020-11-01 22:09:28 +00:00
var simuCanvas = document . getElementById ( "simuCanvas" ) ;
var ctx = simuCanvas . getContext ( "2d" ) ;
var lastpoint = { x : 0 , y : 0 , color : 0 } ;
2020-11-05 08:49:18 +00:00
var zoom = 1 ;
2020-11-01 22:09:28 +00:00
//ctx.save
Point = function ( l ) {
return {
"x" : l [ 0 ] ,
"y" : l [ 1 ] ,
"color" : l [ 2 ]
}
}
// Draws every segment received, except black colored target ones
var drawSimu = function ( ) {
pointsList = document . pointsList
if ( pointsList . length > 0 )
{
2020-11-05 08:49:18 +00:00
ctx . clearRect ( 0 , 0 , simuCanvas . width , simuCanvas . height ) ;
2020-11-01 22:09:28 +00:00
lastpoint = new Point ( pointsList [ 0 ] )
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 ) {
ctx . beginPath ( )
ctx . shadowBlur = 5 ;
ctx . shadowColor = 'rgba(255, 255, 255, 1)' ;
ctx . lineWidth = 2 ;
ctx . strokeStyle = "#" + ( point . color + Math . pow ( 16 , 6 ) ) . toString ( 16 ) . slice ( - 6 ) ;
ctx . moveTo ( lastpoint . x * zoom , lastpoint . y * zoom ) ;
ctx . lineTo ( point . x * zoom , point . y * zoom ) ;
ctx . stroke ( ) ;
ctx . closePath ( )
}
lastpoint = point
}
}
}
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
}
refreshImageList ( )
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 08:49:18 +00:00
$ ( "#imageList" ) . on ( "click" , showImage )
$ ( "#messages" ) . on ( "click" , showImage )
$ ( ".contrast" ) . on ( "click" , function ( el ) {
let btn = el . target
let val = Number ( $ ( btn ) . attr ( "rel" ) )
let new _binary _level = binary _level + val ;
console . log ( ` new_binary_level: ${ new _binary _level } ` )
if ( new _binary _level >= 0 && new _binary _level <= 255 ) {
binary _level = new _binary _level
$ ( "#slider" ) . slider ( "option" , "value" , binary _level )
}
} )
2020-11-01 22:09:28 +00:00
2020-10-16 13:50:05 +00:00
} )