2018-12-19 13:27:45 +00:00
/**
* @author j433866 [j433866@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation" ;
import OperationError from "../errors/OperationError" ;
import qr from "qr-image" ;
2018-12-19 14:43:31 +00:00
import { toBase64 } from "../lib/Base64" ;
2018-12-26 18:40:27 +00:00
import { isImage } from "../lib/FileType" ;
2018-12-25 21:54:38 +00:00
import Utils from "../Utils" ;
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" ;
this . outputType = "byteArray" ;
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" ,
"value" : 5
},
{
2018-12-25 21:54:38 +00:00
"name" : "Margin (num modules)" ,
2018-12-19 14:43:31 +00:00
"type" : "number" ,
"value" : 2
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
2018-12-25 21:54:38 +00:00
* @returns {byteArray}
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 ;
2018-12-19 14:43:31 +00:00
// Create new QR image from the input data, and convert it to a buffer
2018-12-25 21:54:38 +00:00
const qrImage = qr . imageSync ( input , {
type : format ,
size : size ,
margin : margin ,
"ec_level" : errorCorrection . charAt ( 0 ). toUpperCase ()
});
2018-12-19 13:27:45 +00:00
if ( qrImage == null ) {
2018-12-19 14:43:31 +00:00
throw new OperationError ( "Error generating QR code." );
}
2018-12-25 21:54:38 +00:00
switch ( format ) {
case "SVG" :
case "EPS" :
case "PDF" :
return [... Buffer . from ( qrImage )];
case "PNG" :
// Return the QR image buffer as a byte array
return [... qrImage ];
default :
throw new OperationError ( "Unsupported QR code format." );
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
*
2018-12-19 14:43:31 +00:00
* @param {byteArray} data
* @returns {html}
*/
present ( data , args ) {
if ( ! data . length ) return "" ;
const [ format ] = args ;
2018-12-25 21:54:38 +00:00
if ( format === "PNG" ) {
2018-12-19 14:43:31 +00:00
let dataURI = "data:" ;
2018-12-26 18:40:27 +00:00
const mime = isImage ( data );
if ( mime ){
dataURI += mime + ";" ;
2018-12-19 14:43:31 +00:00
} else {
2018-12-25 21:54:38 +00:00
throw new OperationError ( "Invalid PNG file generated by QR image" );
2018-12-19 14:43:31 +00:00
}
dataURI += "base64," + toBase64 ( data );
2018-12-25 21:54:38 +00:00
return `<img src=" ${ dataURI } ">` ;
2018-12-19 13:27:45 +00:00
}
2018-12-25 21:54:38 +00:00
return Utils . byteArrayToChars ( data );
2018-12-19 13:27:45 +00:00
}
}
export default GenerateQRCode ;