2018-12-19 13:27:45 +00:00
/**
* @author j433866 [j433866@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
import { generateQrCode } from "../lib/QRCode.mjs" ;
import { toBase64 } from "../lib/Base64.mjs" ;
import { isImage } from "../lib/FileType.mjs" ;
import Utils from "../Utils.mjs" ;
2018-12-19 13:27:45 +00:00
/**
* Generate QR Code operation
*/
class GenerateQRCode extends Operation {
/**
* GenerateQRCode constructor
*/
constructor () {
super ();
this . name = "Generate QR Code" ;
2018-12-20 14:42:12 +00:00
this . module = "Image" ;
2018-12-25 21:54:38 +00:00
this . description = "Generates a Quick Response (QR) code from the input text.<br><br>A QR code is a type of matrix barcode (or two-dimensional barcode) first designed in 1994 for the automotive industry in Japan. A barcode is a machine-readable optical label that contains information about the item to which it is attached." ;
2018-12-20 14:42:12 +00:00
this . infoURL = "https://wikipedia.org/wiki/QR_code" ;
2018-12-19 13:27:45 +00:00
this . inputType = "string" ;
2019-04-01 14:11:37 +01:00
this . outputType = "ArrayBuffer" ;
2018-12-19 14:43:31 +00:00
this . presentType = "html" ;
this . args = [
{
"name" : "Image Format" ,
"type" : "option" ,
2018-12-25 21:54:38 +00:00
"value" : [ "PNG" , "SVG" , "EPS" , "PDF" ]
2018-12-19 14:43:31 +00:00
},
{
2018-12-25 21:54:38 +00:00
"name" : "Module size (px)" ,
2018-12-19 14:43:31 +00:00
"type" : "number" ,
2019-03-08 13:38:59 +00:00
"value" : 5 ,
"min" : 1
2018-12-19 14:43:31 +00:00
},
{
2018-12-25 21:54:38 +00:00
"name" : "Margin (num modules)" ,
2018-12-19 14:43:31 +00:00
"type" : "number" ,
2019-03-08 13:38:59 +00:00
"value" : 2 ,
"min" : 0
2018-12-25 21:54:38 +00:00
},
{
"name" : "Error correction" ,
"type" : "option" ,
"value" : [ "Low" , "Medium" , "Quartile" , "High" ],
"defaultIndex" : 1
2018-12-19 14:43:31 +00:00
}
];
2018-12-19 13:27:45 +00:00
}
/**
* @param {string} input
* @param {Object[]} args
2019-04-01 14:11:37 +01:00
* @returns {ArrayBuffer}
2018-12-19 13:27:45 +00:00
*/
run ( input , args ) {
2018-12-25 21:54:38 +00:00
const [ format , size , margin , errorCorrection ] = args ;
2019-03-07 13:21:26 +00:00
return generateQrCode ( input , format , size , margin , errorCorrection );
2018-12-19 14:43:31 +00:00
}
/**
* Displays the QR image using HTML for web apps
2018-12-25 21:54:38 +00:00
*
2019-04-01 14:11:37 +01:00
* @param {ArrayBuffer} data
2018-12-19 14:43:31 +00:00
* @returns {html}
*/
present ( data , args ) {
2019-04-01 14:11:37 +01:00
if ( ! data . byteLength && ! data . length ) return "" ;
const dataArray = new Uint8Array ( data ),
[ format ] = args ;
2018-12-25 21:54:38 +00:00
if ( format === "PNG" ) {
2019-04-01 14:11:37 +01:00
const type = isImage ( dataArray );
if ( ! type ) {
throw new OperationError ( "Invalid file type." );
2018-12-19 14:43:31 +00:00
}
2019-04-01 14:11:37 +01:00
return `<img src="data: ${ type } ;base64, ${ toBase64 ( dataArray ) } ">` ;
2018-12-19 13:27:45 +00:00
}
2018-12-25 21:54:38 +00:00
2019-04-01 14:11:37 +01:00
return Utils . arrayBufferToStr ( data );
2018-12-19 13:27:45 +00:00
}
}
export default GenerateQRCode ;