Skip to content

Navigation Menu

Sign in
Sign up

QR Code generation in OIE using ZXing #135

NicoPiel started this conversation in Show and tell
Discussion options

Dependencies are ZXing core and the JSE module.

/**
 Encodes a string as QR code and writes directly to PNG file

 @param {String} data - input data (not base64)
 @param {String} path - relative or absolute path to write to (uses FileUtil)
 @param {String} width - width in pixels (e.g. 512)
*/
function qrAsImage(data, path, width) {
 var writer = new com.google.zxing.qrcode.QRCodeWriter();
 
 // encode url
 var matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, width, width);
 
 // create byte array
 var baos = new java.io.ByteArrayOutputStream();
 
 try {
 com.google.zxing.client.j2se.MatrixToImageWriter.writeToStream(matrix, 'png', baos)
 } catch (e) {
 logger.error(e)
 } finally {
 baos.flush();
 baos.close();
 }
 
 var imageBytes = baos.toByteArray();
 
 // write byte array to file
 FileUtil.write(path, false, imageBytes)
} 
You must be logged in to vote

Replies: 1 comment

Comment options

Great contribution! Many thanks for that!

I refined the function a little bit in order to support also other 1D & 2D barcode formats and to also return the image as base64-encoded byte array:
image
Code:

/**
 Encodes a string as a 1D or 2D barcode image (PNG-format)

 @param {String} payload - The actual data that should be transformed to a barcode (not base64)
 @param {String} imageWidth - Width of the barcode image in pixels . (<i>optional</i>) (<b>default:</b> 128)
 @param {String} format - Defines which QR-code format is used. (<i>optional</i>) (<b>default:</b> QR_CODE)<br>
 					 The following formats are available:
 					 <ul>
	 					 <li><b>AZTEC</b> - A 2D barcode barcode often used in transport tickets (e.g., airline boarding passes, train tickets). No quiet zone required.</li>
	 					 <li><b>CODABAR</b> - An older barcode type used in libraries, blood banks, photo labs. Limited symbol set.</li>
	 					 <li><b>CODE_39</b> - A simple alphanumeric barcode used in automotive, defense, and general inventory.</li>
	 					 <li><b>CODE_93</b> - A more compact and reliable version of Code 39, used in logistics.</li>
	 					 <li><b>CODE_128</b> - A very dense and flexible barcode supporting the full ASCII set; widely used in shipping, packaging, GS1 standards.</li>
	 					 <li><b>DATA_MATRIX</b> - A small 2D code used widely in logistics, electronics, and healthcare (e.g., medical instruments, tiny components).</li>
	 					 <li><b>EAN_8</b> - Short version of EAN-13, used on small packages (e.g., chewing gum, cigarettes).</li>
	 					 <li><b>EAN_13</b> - International retail barcode standard (13 digits) seen on almost all consumer goods.</li>
	 					 <li><b>ITF</b> - Numeric-only barcode, common in distribution and warehouse tracking (cartons, pallets).</li>
	 					 <li><b>PDF_417</b> - A stacked linear 2D barcode, often used for identification cards, transport, and government documents (e.g., driver’s licenses).</li>
	 					 <li><b>QR_CODE</b> - The well-known square barcode barcode used everywhere (URLs, payments, marketing, logistics).</li>
	 					 <li><b>UPC_A</b> - Standard 12-digit Universal Product Code used in the US/Canada retail system.</li>
	 					 <li><b>UPC_E</b> - Compressed version of UPC-A for smaller packages with limited space.</li>
	 					 <li><b>UPC_EAN_EXTENSION</b> - 2- or 5-digit add-on codes often used with UPC/EAN to encode supplemental data (like magazine issue number, book pricing).</li>
 					 </ul>
 @param {String} path - The path to which the QR-code PNG image should be written (<i>optional</i>)
 @return {byte[]} The QR-code PNG image as a base64-encoded byte array

*/
function generateBarcode(payload, imageWidth, format, path) {
	// define which barcodes are 2D
	const twoD = <><![CDATA[
AZTEC
DATA_MATRIX
MAXICODE
PDF_417
QR_CODE
]]></>.toString().trim().replace(/\r\n|\n|\r/g, ',');
	// set 128 as default width
	imageWidth = imageWidth || 128;
	
	// create a square image for 2d and a rectangular image for 1D barcodes
	imageHeight = (!format || (twoD.indexOf(format.toUpperCase()) != -1)) ? imageWidth : imageWidth / 2;
	// set QR_CODE as default format
	format = Packages.com.google.zxing.BarcodeFormat.valueOf(format && format.toUpperCase() || 'QR_CODE');
	// instatiate the writer
	var writer = new Packages.com.google.zxing.MultiFormatWriter();
	// generate the barcode
	var barcode = writer.encode(payload, format, imageWidth, imageHeight);	
	// create a byte array stream for writing the image to 
	var baos = new java.io.ByteArrayOutputStream();
	
	try {
		// and generate the image
		Packages.com.google.zxing.client.j2se.MatrixToImageWriter.writeToStream(barcode, 'png', baos);
	} finally {
		// make sure, the resources are freed up
		baos.flush();
		baos.close();
	}
	// get the image data as byte array
	var imageBytes = baos.toByteArray();
	
	// if a path was provided
	if(path){
	 // write the image also to the filesystem
	 FileUtil.write(path, false, imageBytes);
	}
	// return the image in base64 format
	return FileUtil.encode(imageBytes);
} 

The underlying library also supports a few other formats (MAXICODE, RSS_14, and RSS_EXPANDED) but only as read only at the point of writing.

(The function is compatible to the current versions of Open Integration Engine, BridgeLink, and Mirth Connect)

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /