===Input Variables===
barcode_contents=07g129091pe01
type=datamatrixrectangle
size=10
===Jar Files===
Barcode4j.jar (268.1k) Barcodes <http://sourceforge.net/projects/barcode4j/files/barcode4j/>
===Notes===
===Script===
import org.krysalis.barcode4j.impl.datamatrix.DataMatrixBean
import org.krysalis.barcode4j.impl.code128.Code128Bean
import org.krysalis.barcode4j.impl.pdf417.PDF417Bean
import org.krysalis.barcode4j.output.java2d.Java2DCanvasProvider
import org.krysalis.barcode4j.impl.datamatrix.SymbolShapeHint
import java.awt.*
import java.awt.image.*
if (type == 'datamatrixrectangle'){
DataMatrixBean bean = new DataMatrixBean();
bean.setModuleWidth(Integer.parseInt(size));
bean.setShape(SymbolShapeHint.FORCE_RECTANGLE )
s = bean.calcDimensions(barcode_contents);
ss = Math.floor(s.getWidthPlusQuiet()).toInteger();
image = new BufferedImage(ss, ss, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, image.getWidth(), image.getHeight());
g2.setColor(Color.BLACK);
Java2DCanvasProvider cc = new Java2DCanvasProvider(g2, 0);
bean.generateBarcode(cc, barcode_contents);
}
if (type == 'datamatrix'){
DataMatrixBean bean = new DataMatrixBean();
bean.setModuleWidth(Integer.parseInt(size));
bean.setShape(SymbolShapeHint.FORCE_SQUARE )
s = bean.calcDimensions(barcode_contents);
ss = Math.floor(s.getWidthPlusQuiet()).toInteger();
image = new BufferedImage(ss, ss, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, image.getWidth(), image.getHeight());
g2.setColor(Color.BLACK);
Java2DCanvasProvider cc = new Java2DCanvasProvider(g2, 0);
bean.generateBarcode(cc, barcode_contents);
}
if (type == 'code128'){
Code128Bean bean = new Code128Bean()
bean.setModuleWidth(2);
bean.doQuietZone(false)
bb = Integer.parseInt(size)
bean.setBarHeight(bb)
s = bean.calcDimensions(barcode_contents)
zz = Math.ceil(s.getWidthPlusQuiet()).toInteger()
ss = Math.floor(s.getWidthPlusQuiet()).toInteger()
image = new BufferedImage(ss , bb, BufferedImage.TYPE_INT_RGB)
Graphics2D g2 = image.getGraphics()
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, image.getWidth(), image.getHeight());
g2.setColor(Color.BLACK);
Java2DCanvasProvider cc = new Java2DCanvasProvider(g2, 0)
bean.generateBarcode(cc, barcode_contents)
}
if (type == 'pdf417'){
PDF417Bean bean = new PDF417Bean();
bean.setModuleWidth(2);
bean.doQuietZone(false)
bean.setBarHeight(Integer.parseInt(size));
s = bean.calcDimensions(barcode_contents)
zz = Math.ceil(s.getWidthPlusQuiet()).toInteger()
ss = Math.floor(s.getWidthPlusQuiet()).toInteger()
image = new BufferedImage(ss , ss, BufferedImage.TYPE_INT_RGB)
Graphics2D g2 = image.getGraphics()
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, image.getWidth(), image.getHeight());
g2.setColor(Color.BLACK);
Java2DCanvasProvider cc = new Java2DCanvasProvider(g2, 0)
bean.generateBarcode(cc, barcode_contents)
}
return image;
Barcode generation for datamatrix, datamatrixrectangle, code128, pdf417
Started by Jesse Barnum, Jul 15 2011 08:39 AM
9 replies to this topic
#1 OFFLINE master
Posted 15 July 2011 - 08:39 AM
This is a ScriptMaster module sent in by Benjamin Brittain. It generates bar codes using the barcode4j library from Krysalis. This only demonstrates 4 bar code types, but there are many more supported by this library.
#2 OFFLINE enthusiast, with a bit of geek
Posted 18 July 2011 - 06:41 AM
You probably want to add the copyright notice to that as it is copied straight out of the example file (including the double space after the equals after the zz)
and here is the code to do a Code39 in addition, but with the addition of control over bar height & size, and also the font that the numbers are written in under the code
and here is the code to do a Code39 in addition, but with the addition of control over bar height & size, and also the font that the numbers are written in under the code
/*
* Copyright 2004 Jeremias Maerki.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.krysalis.barcode4j.impl.code39.Code39Bean
if (type == 'code39'){
bean = new Code39Bean()
bean.setModuleWidth(2)
bean.doQuietZone(false)
// font height
dd = 24
//bar height
bb = 80
bean.setBarHeight(bb)
// change to suit
bean.setFontName("Calibri")
bean.setFontSize(dd)
s = bean.calcDimensions(barcode_contents)
zz = Math.ceil(s.getWidthPlusQuiet()).toInteger()
ss = Math.floor(s.getWidthPlusQuiet()).toInteger()
image = new BufferedImage(ss , bb + dd, BufferedImage.TYPE_INT_RGB)
Graphics2D g2 = image.getGraphics()
g2.setBackground(Color.WHITE)
g2.clearRect(0, 0, image.getWidth(), image.getHeight())
g2.setColor(Color.BLACK)
cc = new Java2DCanvasProvider(g2, 0)
bean.generateBarcode(cc, barcode_contents.toUpperCase())
}
#3 OFFLINE newbie
Posted 06 August 2011 - 10:58 AM
ergh... yea that should be completely included!
It wasn't completely copied, but it certainly was used as a basis.
(I didn't know that this was going to be shown to anybody... even so, it should still be in the file)
It wasn't completely copied, but it certainly was used as a basis.
(I didn't know that this was going to be shown to anybody... even so, it should still be in the file)
#4 OFFLINE newbie
Posted 25 January 2012 - 08:20 AM
This is extremely useful. It allows me to generate all necessary and complex barcodes, including PDF417 and EAN-13, just from 1 plugin. Thanks Benjamin (and, of course, Jesse). If anyone - like me - has difficulty creating EAN-13 barcodes and not getting the printed numbers below the barcode, using the sample code provided by Jesse: don't forget to add the font size to the bar height in order to calculate the canvas height, otherwise the printed numbers are clipsed.
Use for example this code to generate EAN-13 barcodes:
Use for example this code to generate EAN-13 barcodes:
import org.krysalis.barcode4j.impl.upcean.EAN13Bean
if (type == 'ean13'){
//Create the barcode bean
EAN13Bean bean = new EAN13Bean();
bean.setModuleWidth(2);
bean.doQuietZone(false) ;
bean.setFontSize(Double.parseDouble(fontSize)) ;
bb = Integer.parseInt(size) ;
bbi = bb + Integer.parseInt(fontSize) ; // add font height to bar height
bean.setBarHeight(bb)
s = bean.calcDimensions(barcode_contents) ;
zz = Math.ceil(s.getWidthPlusQuiet()).toInteger()
ss = Math.floor(s.getWidthPlusQuiet()).toInteger()
image = new BufferedImage(ss , bbi, BufferedImage.TYPE_INT_RGB)
Graphics2D g2 = image.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, image.getWidth(), image.getHeight());
g2.setColor(Color.BLACK);
Java2DCanvasProvider cc = new Java2DCanvasProvider(g2, 0);
//Generate the barcode
bean.generateBarcode(cc, barcode_contents);
}
#5 OFFLINE enthusiast, with a bit of geek
Posted 27 January 2012 - 01:36 PM
You can specify your own font too on the EAN one
e.g.
e.g.
bean.doQuietZone(false)
bean.setFontName("Miso")
#6 OFFLINE newbie
Posted 13 February 2012 - 06:28 AM
Barcodes work great this way. However, I wonder, as we often need to generate several hundreds of barcodes at once, if it is possible to prevent the screen from flickering on Windows. It seems to be application bound, as moving the window off-sreen, and inserting a 'Freeze Window' script step doesn't seem to help. Any suggestions are appreciated!
EDIT: I found the solution myself. Unchecking the 'Gui' option when registering the module does the trick.
EDIT: I found the solution myself. Unchecking the 'Gui' option when registering the module does the trick.
#7 OFFLINE novice
Posted 12 March 2012 - 12:32 PM
This script should completely included in the next version of script master.
I have tested all types, and they all work well except EAN13 (from Jeroen Aarts), I’m getting an error "groovy.lang.MissingPropertyException: No such property: fontSize for class: Script1."
I did some test, adding bean.setFontSize(dd), but didn’t work either.
Thanks for sharing.
I have tested all types, and they all work well except EAN13 (from Jeroen Aarts), I’m getting an error "groovy.lang.MissingPropertyException: No such property: fontSize for class: Script1."
I did some test, adding bean.setFontSize(dd), but didn’t work either.
Thanks for sharing.
#8 OFFLINE newbie
Posted 12 March 2012 - 06:35 PM
#9 OFFLINE member
Posted 17 March 2012 - 06:06 AM
Something the code from this thread has evolved to.
Have fun.
Have fun.
Attached Files
#10 OFFLINE newbie
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users
































