Jump to content




Barcode generation for datamatrix, datamatrixrectangle, code128, pdf417



  • Please log in to reply
9 replies to this topic

#1 OFFLINE   Jesse Barnum  master

Jesse Barnum
  • 360Works
  • 313 posts
  • FM Client:11 Advance
  • Platform:Mac OS 9
  • Skill Level:Entry Level
  • Time Online: 4d 18h 40m 8s

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.

===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;

--Jesse Barnum, President, 360Works
http://www.360works.com
(770) 234-9293

#2 OFFLINE   john renfrew  enthusiast, with a bit of geek

john renfrew
  • Members
  • 222 posts
  • LocationUK
  • FM Client:11 Advance
  • Platform:Cross Platform
  • Membership:TechNet, FileMaker Business Alliance
  • Time Online: 7d 13h 19m 10s

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

/*
 * 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   BBrittain  newbie

BBrittain
  • Members
  • Pip
  • 1 posts
  • Skill Level:Intermediate
  • Time Online: 6s

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)

#4 OFFLINE   Jeroen Aarts  newbie

Jeroen Aarts
  • Members
  • PipPip
  • 2 posts
  • Time Online: 9m 56s

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:


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   john renfrew  enthusiast, with a bit of geek

john renfrew
  • Members
  • 222 posts
  • LocationUK
  • FM Client:11 Advance
  • Platform:Cross Platform
  • Membership:TechNet, FileMaker Business Alliance
  • Time Online: 7d 13h 19m 10s

Posted 27 January 2012 - 01:36 PM

You can specify your own font too on the EAN one
e.g.
bean.doQuietZone(false)
bean.setFontName("Miso")


#6 OFFLINE   Jeroen Aarts  newbie

Jeroen Aarts
  • Members
  • PipPip
  • 2 posts
  • Time Online: 9m 56s

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.

#7 OFFLINE   capsprojectos  novice

capsprojectos
  • Members
  • 61 posts
  • FM Client:11 Advance
  • Platform:Windows 7
  • Skill Level:Entry Level
  • Time Online: 21h 10m 13s

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.

#8 OFFLINE   comeonfuture  newbie

comeonfuture
  • Members
  • Pip
  • 1 posts
  • Time Online: 48m 21s

Posted 12 March 2012 - 06:35 PM

:laugh2: Thanks for all the barcode4j generation code for all kinds of barcode types generation solution which help me out for the future barcode generation problem. And I recently find another barcode solution site which is Onbarcode and Onbarcode provides all kinds of barcode genration and reading solution as well as barcode4j with almost all common used barcode type. For example, .NET ASP data matrix barcode genratorI've recently used also provides details on the data matrix generation guide. Both barcode4j and Onbarcode are excellent which have their own features to meet our barcode need. God luck for all barcode developers and users!

#9 OFFLINE   Karsten Wolf  member

Karsten Wolf
  • Members
  • 24 posts
  • LocationGermany
  • FM Client:10 Advance
  • Platform:Max OS X Tiger
  • Skill Level:Intermediate
  • Time Online: 1d 5h 58m 13s

Posted 17 March 2012 - 06:06 AM

Something the code from this thread has evolved to.

Have fun.

Attached Files



#10 OFFLINE   james61  newbie

james61
  • Members
  • Pip
  • 1 posts
  • Time Online: 16m 47s

Posted 23 March 2012 - 01:15 AM

here is one qr code barcode generation library. have a look!


Back to ScriptMaster by 360 Works


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

FMForum Advertisers