Created
February 15, 2016 14:20
-
-
Save paulklinkenberg/d7b9c6b5f2a609352bc3 to your computer and use it in GitHub Desktop.
barcode4j example in CFML / Lucee
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfset jarsPath = expandPath('/java/barcode4j/') /> | |
<!--- Path where the necessary jar files are located (downloadable from https://www.dropbox.com/s/skl9fwky2n7mnjr/java-files-barcode4j.zip?dl=0) ---> | |
<cfset libraryList = arrayToList( directoryList(jarsPath, false, 'array', '*.jar') ) /> | |
<cfset configBuilder = createObject('java', 'org.apache.avalon.framework.configuration.DefaultConfigurationBuilder', libraryList) /> | |
<cfset barcodeUtil = createObject('java', 'org.krysalis.barcode4j.BarcodeUtil', libraryList) /> | |
<cfset grayImageType = createObject('java', 'java.awt.image.BufferedImage').TYPE_BYTE_GRAY /> | |
<!--- create custom config. See http://barcode4j.sourceforge.net/2.1/symbol-ean-13.html ---> | |
<cfset configXml = '<barcode> | |
<ean-13> | |
<height>10mm</height> | |
<quiet-zone enabled="false">1mm</quiet-zone> | |
<checksum>ignore</checksum> | |
<human-readable> | |
<placement>none</placement> | |
</human-readable> | |
</ean-13> | |
</barcode>' /> | |
<!--- config needs to come from file, it seems. So writing it to tempDir first ---> | |
<cfset fileWrite(getTempDirectory() & "barcodeconfig.xml", configXml) /> | |
<cfset barcodeConfig = configBuilder.buildFromFile( createObject('java','java.io.File').init("#getTempDirectory()#barcodeconfig.xml") ) /> | |
<!--- the barcode generator object ---> | |
<cfset gen = barcodeUtil.getInstance().createBarcodeGenerator( barcodeConfig ) /> | |
<!--- path to save the barcode images to ---> | |
<cfset barcodedirpath = expandPath("/barcodefiles/") /> | |
<cfset barcode = "0075678164125" /> | |
<!--- create barcode image ---> | |
<cfset imgPath = barcodedirpath & "#barcode#.png" /> | |
<cfset outStream = createObject('java', 'java.io.FileOutputStream').init( createObject('java','java.io.File').init(imgPath) ) /> | |
<cfset provider = createObject('java', 'org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider', libraryList) | |
.init(outStream, "image/x-png", 300, grayImageType, true, 0) /> | |
<cfset gen.generateBarcode(provider, barcode) /> | |
<cfset provider.finish() /> | |
<cfset outStream.close() /> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very helpful. Thank you so much for sharing, Paul!