Sign in to follow this  
Followers 0

Barcode generation for datamatrix, datamatrixrectangle, code128, pdf417

22 posts in this topic

Posted

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;

Share this post


Link to post
Share on other sites

Posted

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())

}

1 person likes this

Share this post


Link to post
Share on other sites

Posted

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)

Share this post


Link to post
Share on other sites

Posted

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

}

Share this post


Link to post
Share on other sites

Posted

You can specify your own font too on the EAN one

e.g.


bean.doQuietZone(false)

bean.setFontName("Miso")

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

: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 genrator I'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!

Share this post


Link to post
Share on other sites

Posted

Something the code from this thread has evolved to.

Have fun.

barcodeFMP.zip

1 person likes this

Share this post


Link to post
Share on other sites

Posted

And both those two involve cost whereas b4j is open source. Or have I missed something??

Share this post


Link to post
Share on other sites

Posted

Yep, see this barcode generating library in C# .net

 

 

Barcode barcode = new Barcode();
barcode.Symbology = BarcodeType.Code39;
barcode.Data = "01234567890";
barcode.Save(theImage, saveFileName, RasterImageFormat.Tif, theImage.BitsPerPixel)

Share this post


Link to post
Share on other sites

Posted

Yep, see this barcode generating library in C# .net

 

 

Barcode barcode = new Barcode();

barcode.Symbology = BarcodeType.Code39;

barcode.Data = "01234567890";

barcode.Save(theImage, saveFileName, RasterImageFormat.Tif, theImage.BitsPerPixel)

Thanks for sharing.

Share this post


Link to post
Share on other sites

Posted

I think some imaging sdk  programmes can enable to quickly generate barcodes. i have use imaging sdkr which can original layouts hyperlinks, Images and tables retained in word ,text, image, Epub, html.but i forgot download the wedsite ,go to google. By the way , pay attention to your system platform, select the suitable one . I use a barcode generator driver found on the internet . Install it and it becomes a selectable option.Then you can generate barcodes  in many formats  in any program at all, including Adobe Acrobat . Just open the sdk, select barcode, and choice a form you want, then you can generate barcode in c#, vb, asp ,etc ,the task will be finished in several seconds. if you haven't found a good choice  , you can have a try. best wishes.

Share this post


Link to post
Share on other sites

Posted

Does anyone know if there is a code3of9 extended bean ( code39ext ) available to use with this script master module?

Share this post


Link to post
Share on other sites

Posted

 

the code39 bean further up the page will work with
bean.setExtendedCharSetEnabled(true)
 
 
also can me made from inside iText with a code39 barcode with
.setExtended(true)
1 person likes this

Share this post


Link to post
Share on other sites

Posted

I have code128 and pdf417 in my plugin and I am using the same jar library.

Pretty much, the only thing you need to adjust, is the reference to the code.

And if you look at Jesse's original post, you will see that these 2 codes are included.

datamatrixrectangle, is not one I know about. But go look at the jar library project website.

As I recall, they have some good documentation stuff there...

1 person likes this

Share this post


Link to post
Share on other sites

Posted

Is there any way to increase the resolution of the output image?

Share this post


Link to post
Share on other sites

Posted

Is there any way to increase the resolution of the output image?

:heart: You are luckily,maybe i can help you.I just used a ka barcode generator to increase the resolution of the output image,it can generate multi-barcode at the same time.You can create several same barcodes at the same time or generate several different barcodes also.

post-112097-0-98000300-1412936655_thumb.

Share this post


Link to post
Share on other sites

Posted

hi

and for GS1DataBar?

ITF14?

what do you use?

RegisterGroovy( "generateBarcode( barcode_contents ; type ; barSize ; fontSize ; moduleWidth )" ; "import org.krysalis.barcode4j.impl.codabar.CodabarBean¶
¶
import org.krysalis.barcode4j.impl.code128.Code128Bean¶
¶
import org.krysalis.barcode4j.impl.code39.Code39Bean¶
¶
import org.krysalis.barcode4j.impl.datamatrix.DataMatrixBean¶
import org.krysalis.barcode4j.impl.datamatrix.SymbolShapeHint¶
¶
import org.krysalis.barcode4j.impl.fourstate.RoyalMailCBCBean¶
import org.krysalis.barcode4j.impl.fourstate.USPSIntelligentMailBean¶
¶
import org.krysalis.barcode4j.impl.int2of5.Interleaved2Of5Bean¶
import org.krysalis.barcode4j.impl.int2of5.ITF14Bean¶
¶
import org.krysalis.barcode4j.impl.pdf417.PDF417Bean¶
¶
import org.krysalis.barcode4j.impl.postnet.POSTNETBean¶
¶
import org.krysalis.barcode4j.impl.upcean.EAN13Bean¶
import org.krysalis.barcode4j.impl.upcean.EAN8Bean¶
import org.krysalis.barcode4j.impl.upcean.UPCABean¶
import org.krysalis.barcode4j.impl.upcean.UPCEBean¶
import org.krysalis.barcode4j.impl.upcean.UPCEANBean¶
¶
import org.krysalis.barcode4j.output.java2d.Java2DCanvasProvider¶
import org.krysalis.barcode4j.output.eps.EPSCanvasProvider¶
import org.krysalis.barcode4j.output.svg.SVGCanvasProvider¶
¶
¶
import java.awt.*¶
import java.awt.image.*¶
¶
barSize_i = Integer.parseInt(barSize)¶
fontSize_d = Double.parseDouble(fontSize)¶
fontSize_i = Integer.parseInt(fontSize)¶
modulewith_i = Integer.parseInt(moduleWidth)¶
¶
¶
def bean¶
¶
if (type == 'datamatrixrectangle')¶
{¶
    bean = new DataMatrixBean()¶
    bean.setShape(SymbolShapeHint.FORCE_RECTANGLE )¶
}¶
¶
if (type == 'datamatrix'){¶
    bean = new DataMatrixBean()¶
    bean.setShape(SymbolShapeHint.FORCE_SQUARE )¶
}¶
¶
if (type == 'code128'){¶
    bean = new Code128Bean()¶
}¶
¶
if (type == 'pdf417'){¶
    bean = new PDF417Bean()¶
}¶
¶
if (type == 'code39'){¶
    bean = new Code39Bean()¶
}¶
¶
if (type == 'ean13'){¶
    bean = new EAN13Bean()¶
}¶
¶
if (type == 'codabar'){¶
    bean = new CodabarBean()¶
}¶
¶
if (type == 'ean8'){¶
    bean = new EAN8Bean()¶
}¶
¶
¶
if (type == 'upca'){¶
    bean = new UPCABean()¶
}¶
¶
if (type == 'upce'){¶
    bean = new UPCEBean()¶
}¶
¶
if (type == 'interleaved2of5'){¶
    bean = new Interleaved2Of5Bean()¶
}¶
¶

¶
// results do not look ok¶
if (type == 'itf14'){¶
    bean = new ITF14Bean()¶
}¶
¶
/*¶
if (type == 'postnet'){¶
    bean = new POSTNETBean()¶
}¶
¶
if (type == 'royalmailcbc'){¶
    bean = new RoyalMailCBCBean()¶
}¶
¶
if (type == 'usps4cb'){¶
    bean = new USPSIntelligentMailBean()¶
}¶
*/¶
¶
¶
bean.setBarHeight( barSize_i )¶
¶
bean.setFontName("Miso")¶
bean.setFontSize( fontSize_d )¶
¶
// bean.doQuietZone( quietZone_b )¶
bean.doQuietZone( false )¶
¶
bean.setModuleWidth( modulewith_i )¶
¶
barcodeDimensions = bean.calcDimensions(barcode_contents)¶
¶
totalWidth = Math.floor(barcodeDimensions.getWidthPlusQuiet()).toInteger()¶
totalHeight = Math.floor(barcodeDimensions.getHeightPlusQuiet()).toInteger()¶
¶
image = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB)¶
¶
Graphics2D context = image.getGraphics()¶
¶
context.setBackground(Color.WHITE)¶
context.clearRect(0, 0, image.getWidth(), image.getHeight())¶
context.setColor(Color.BLACK)¶
¶
Java2DCanvasProvider provider = new Java2DCanvasProvider(context, 0)¶
¶
bean.generateBarcode(provider, barcode_contents)¶
¶
return image" )

this is for ITF14 does not work

"ERROR"

 

please

Share this post


Link to post
Share on other sites

Posted

Barcode Creator can generate ITF barcodes (including ITF14). DataBar (14) and DataBar Extended are both on the roadmap, but not in place yet. It does, however, support GS1 data encoded in Code 128 barcodes right now, in case that's an acceptable alternative.

 

Disclosure: I created Barcode Creator. I get a cut when anyone buys a copy, so naturally I'm biased.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0