|
|
|
|
|
var maxCanvasWidth = 4096, maxCanvasHeight = 4096; |
|
|
|
var acceptedFileType = ['jpg', 'jpeg', 'png']; |
|
|
|
Compressor.setDefaults({ |
|
mimeType: 'image/png', |
|
maxWidth: 1050, |
|
maxHeight: 800, |
|
convertSize: 2000000, |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var croppieOpts = { |
|
viewport: { width: 340, height: 240 }, |
|
boundary: { width: screen.width, height: 525 }, |
|
showZoomer: false, |
|
enableOrientation: true, |
|
}; |
|
$('#img-preview').croppie(croppieOpts); |
|
window.addEventListener("flutterInAppWebViewPlatformReady", null); |
|
|
|
|
|
|
|
$('.file-capture, .file-upload').change(function() { |
|
var file = event.target.files[0] |
|
, isInitial = $('.croppie-container #text-preview').length == 0; |
|
|
|
if (file === undefined) modalAlert(gettext('Invalid Image'), gettext('No image file selected')); |
|
if (!validateFileType($(this).val())) modalAlert(gettext('Invalid Image'), gettext('Please upload valid identification in JPEG/PNG format')); |
|
new Compressor(file, { |
|
success: function(result) { |
|
blobToDataURL(result, function(dataURL) { |
|
$('#img-preview').croppie('bind', dataURL).then(function() { |
|
$('#img-preview').croppie('setZoom', 0); |
|
}); |
|
if (isInitial) { |
|
initCroppieComponents(); |
|
initBorderGuide(); |
|
} |
|
}); |
|
}, |
|
error: function(err) { |
|
console.error('Compressor() error:'+ err.message); |
|
modalAlert(gettext('Error'), gettext('Error capturing image')); |
|
}, |
|
}); |
|
}); |
|
|
|
|
|
|
|
$('#btn-next').click(function() { |
|
var $img = $('#img-preview') |
|
, $ocrFile = $('#id_ocr_file') |
|
, dataURL; |
|
|
|
|
|
$('.btn').attr('disabled', true).addClass('disabled'); |
|
|
|
$img.croppie('result', { |
|
'type': 'rawcanvas', |
|
'size': 'original', |
|
'format': 'png', |
|
}).then(function(canvas) { |
|
dataURL = canvas.toDataURL(); |
|
$ocrFile.val(dataURL.substring(22)); |
|
$('#form-ocr').submit(); |
|
}); |
|
}); |
|
|
|
|
|
$('#btn-back').click(function() { |
|
try { window.flutter_inappwebview.callHandler('backToMobile', 'true'); } catch(error) {} |
|
}); |
|
|
|
|
|
|
|
function validateFileType(fileName) { |
|
var splited = fileName.split('.') |
|
, fileType = (splited[splited.length - 1]).toLowerCase(); |
|
if ((!fileName) || (splited.length <= 1) || (acceptedFileType.indexOf(fileType) < 0)) return false; |
|
return true; |
|
} |
|
|
|
|
|
|
|
function blobToDataURL(blob, callback) { |
|
var reader = new FileReader(); |
|
reader.onload = function(e) { callback(e.target.result); } |
|
reader.readAsDataURL(blob); |
|
} |
|
|
|
|
|
|
|
function dataURLtoBlob(dataURL) { |
|
var arr = dataURL.split(','), mime = arr[0].match(/:(.*?);/)[1], |
|
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); |
|
while(n--){ |
|
u8arr[n] = bstr.charCodeAt(n); |
|
} |
|
return new Blob([u8arr], {type:mime}); |
|
} |
|
|
|
|
|
|
|
function initCroppieComponents() { |
|
var $timer = $('.timer-text').clone().removeClass('text-secondary').addClass('text-white') |
|
, $previewText = $('<div></div>').addClass('text-white').attr('id', 'text-preview').html(gettext('Drag, rotate, or pinch the image to make sure<br>that all information is within the box.')) |
|
, $iconRotate = $('<span></span>').addClass('material-icons-outlined').text('rotate_90_degrees_ccw') |
|
, $btnRotate = $('<button></button>').attr('type', 'button').addClass('btn btn-dark').attr('id', 'btn-rotate').data('degree', 90).html($iconRotate).click(function () { |
|
$('#img-preview').croppie('rotate', $(this).data('degree')); |
|
}) |
|
, $rotateContainer = $('<div></div>').attr('id', 'rotate-container').append($btnRotate); |
|
|
|
$('header, .page-header, .page-subheader, .default-container').hide(); |
|
$('main').addClass('no-header'); |
|
$('.preview-container').show(); |
|
$('.cr-boundary').append($timer, $previewText, $rotateContainer); |
|
} |
|
|
|
|
|
|
|
function initBorderGuide() { |
|
var guideTopRight1 = $('<div></div>').addClass('guide-top-right-1') |
|
, guideTopRight2 = $('<div></div>').addClass('guide-top-right-2') |
|
, guideBottomRight1 = $('<div></div>').addClass('guide-bottom-right-1') |
|
, guideBottomRight2 = $('<div></div>').addClass('guide-bottom-right-2') |
|
, guideTopLeft1 = $('<div></div>').addClass('guide-top-left-1') |
|
, guideTopLeft2 = $('<div></div>').addClass('guide-top-left-2') |
|
, guideBottomLeft1 = $('<div></div>').addClass('guide-bottom-left-1') |
|
, guideBottomLeft2 = $('<div></div>').addClass('guide-bottom-left-2') |
|
|
|
$('.cr-boundary .cr-viewport').append( |
|
guideTopRight1, |
|
guideTopRight2, |
|
guideBottomRight1, |
|
guideBottomRight2, |
|
guideTopLeft1, |
|
guideTopLeft2, |
|
guideBottomLeft1, |
|
guideBottomLeft2 |
|
); |
|
} |
|
|