Jump to content

BARCODE4J/scriptmaster example


This topic is 3597 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Attached is a simple SM example which generates a Code39 and POSTNET barcode using Barcode4J Link . Result is returned as a Container.

This has been tested on Windows and should run on Macs; barcode is saved as a JPEG to temporary folder, then loaded back into a container. It may be possible to instead load it directly into a container, but I wasn't sure how to achieve that. Code39 is trivial since it can be achieved with free fonts, but this may be of interest for POSTNET and other symbologies supported by BARCODE4J.

Be sure to check the JAR on the Code39 example, I forgot to do that before uploading...

SM_Barcodes_3.zip

Edited by Guest
Link to comment
Share on other sites

Here's some scrappy code to return the Code39 example direct to a container, gets over the low resolution of the saved jpeg file...

Few things to play with to see what effect the number have but it does work...

There is also to barbecue library to look at, not updated since 2007 though..

Slightly simpler to do the same thing

/*

 * 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



import org.krysalis.barcode4j.output.java2d.Java2DCanvasProvider



import java.awt.*

import java.awt.image.*



/**

 * This example demonstrates creating a bitmap barcode using the bean API.

 * 

 * @author Jeremias Maerki

 * @version $Id: SampleBitmapBarcodeWithBean.java,v 1.2 2006/11/07 16:45:28 jmaerki Exp $

 */

 // amended J Renfrew 2010/10/07





Code39Bean bean = new Code39Bean()

// sets narrow width in mm

bean.setModuleWidth(1)

// sets wide to narrow ratio

bean.setWideFactor(3)

bean.doQuietZone(false)

// font height

dd = 24

//bar height

bb = 60

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

//z = s.getBoundingRect()

image = new BufferedImage(ss , bb + dd, BufferedImage.TYPE_INT_RGB)

Graphics2D g2 = image.getGraphics()

g2.setColor(Color.white)

g2.fillRect(0,0, zz, 1)

Java2DCanvasProvider cc = new Java2DCanvasProvider(g2, 0)

bean.generateBarcode(cc, barcode_contents)

//

return image




barbecue

http://barbecue.sourceforge.net/





import net.sourceforge.barbecue.*

import java.awt.*

import java.awt.image.*





// change this to suit

font = new Font( "Arial" , Font.BOLD , 18)

barcode = BarcodeFactory.createCode39(barcode, true)

t = barcode.getWidth()

// change this to suit if you want higher

//barcode.setBarHeight(70)

// this sets DPI

barcode.setResolution(200)

barcode.setFont(font)

s = barcode.getHeight()

// change this if you wnat a coloured barcode or coloured background

//image = new BufferedImage(t, s, BufferedImage.TYPE_INT_RGB)

image = new BufferedImage(t, s, BufferedImage.TYPE_BYTE_GRAY)

Graphics2D g2 = image.getGraphics()

// default is black so draw a white box first

g2.setColor(Color.white)

g2.fillRect(0,0,t,s)

barcode.draw(g2, 0, 0)

return image

Link to comment
Share on other sites

  • 1 year later...

hello,

I'm very novice with barcodes and SM. I need to produce barcodes code39 with start/stop looking like *6589* from filemaker field.

I downloaded the sample but I have always an error (see the screen copy attached).

Can anyone help me ? Thanks

Noël

post-71369-0-58733500-1337672393_thumb.p

Link to comment
Share on other sites

Noel

You seem to be importing the POSTNET classes which will not produce a code39 barcode and what you are giving it as a parameter will not produce a POSTNET barcode either

The example file at the top appears not to work, but the code above does.

Link to comment
Share on other sites

Thanks John for your answer !

1) I'm so novice... I just tried the following thing : copied the script above for barcode4j and pasted in the sample file of SM as a new module. Entered a value in bacode_cotents and run the script : no error but nothing in the container too !

2) From another contact I got a script to produce these barcodes ; they are produced but once printed unreadable by my scanner ! I saw in troubleshooting docs that jpeg image was not the best... but didn't find a detailed expalnation for using gif or png or another... Have you links to help me ?

Noël

Link to comment
Share on other sites

  • 6 months later...
  • Newbies

John R,  Thanks for the code sample, it save me a lot of effort.  Unfortunately I'm noticing something odd.  When I used your code, I'm not getting the entire barcode.  When I print directly to the Graphics2D object I do get the entire code.  Any thoughts?

Link to comment
Share on other sites

  • Newbies

Thanks John.

 

The problem is that Sample 1 (which is based on your example) doesn't print out the entire "start" character of the barcode.  The "start" character should look as follows: Narrow bar, wide space, Narrow bar, narrow space, Wide bar, narrow space, Wide bar, narrow space, Narrow bar.  In practice, the "start" character is missing the first narrow bar and, presumably, the first wide space.

 

Sample 2 outputs the entire barcode but I have to call translate.  I'm loathe to do this since I have multiple barcodes to print and, in my experience, I'm only allowed to invoke translate once.  This causes me to have to base  the location of the subsequent barcodes off of the new origin.

 

Sample 1 -- Doesn't work

          BufferedImage barcodeImage = new BufferedImage((int)widthMin, barHeight, BufferedImage.TYPE_INT_RGB);
          
          ...

          Graphics2D g2 = (Graphics2D)barcodeImage.getGraphics();

          Java2DCanvasProvider cc = new Java2DCanvasProvider(g2, 0);
          barcodeBean.generateBarcode(cc, data);
          
          g2.dispose();
          
          g2d.setBackground(Color.WHITE);
          g2d.setColor(Color.WHITE);
          
          g2d.drawImage(barcodeImage, 5, y, (int)widthMin, barHeight, null);     

 

Sample 2 -- Works

 

          BufferedImage barcodeImage = new BufferedImage((int)widthMin, barHeight, BufferedImage.TYPE_INT_RGB);
          
          Graphics2D g2 = (Graphics2D)barcodeImage.getGraphics();
          
          ...
          
          g2d.translate(5, 75);
          
          Java2DCanvasProvider cc = new Java2DCanvasProvider(g2d, 0);
          barcodeBean.generateBarcode(cc, data);

 

 

Link to comment
Share on other sites

  • 1 year later...
  • 4 weeks later...

If it is any help, I got this script working with Barcode4j

And just by changing the import calls to other types, it works when you want to create other supported barcode types....

/**
* This script is created by Jeremis Maerki
* Modified by Claus Lavendt - [email protected]
* Variables: barcode_contents - dpi - pixels
* Try vary the dpi and pixels
*/




import java.awt.image.BufferedImage
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream

import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider
import org.krysalis.barcode4j.tools.UnitConv

import org.krysalis.barcode4j.output.java2d.Java2DCanvasProvider

import java.awt.*
import java.awt.image.*
import org.krysalis.barcode4j.impl.AbstractBarcodeBean
import org.krysalis.barcode4j.output.bitmap.BitmapBuilder

String msg = barcode_contents
Code128Bean bean = new Code128Bean()
int resolution = Integer.parseInt(dpi) //dpi
int orientation = 0
int pixelsPerModule = Integer.parseInt(pixels) //make one module = one pixel
bean.setModuleWidth(pixelsPerModule * 25.4 / resolution)
 dim = bean.calcDimensions(msg)
BufferedImage image = BitmapBuilder.prepareImage(dim, orientation,
        resolution, BufferedImage.TYPE_BYTE_BINARY)
Graphics2D g2d = BitmapBuilder.prepareGraphics2D(image, dim, orientation, false)
Java2DCanvasProvider canvas = new Java2DCanvasProvider(g2d, orientation)
bean.generateBarcode(canvas, msg)
g2d.dispose()

return image

//Encode the BufferedImage as a PNG file
//ImageIO.write(image, "png", new File("D:/out.png"))
Link to comment
Share on other sites

This topic is 3597 days old. Please don't post here. Open a new topic instead.

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
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.