Link to home
Start Free TrialLog in
Avatar of joedfuse
joedfuseFlag for United States of America

asked on

Resizing code128 barcode using cold fusion

Hello all,

So i am trying to enlarge the barcode that I am generation using the follow code. I can't seem to figure out where i should make this change. Using cfimage resize seems to make things blurry but perhaps I'm just not doing it correctly.  Thanks in advance for any help working the scale on this thing.

<cfscript>
code128= createobject("java","com.lowagie.text.pdf.Barcode128");
code128.setCodeType(code128.CODE128);
/* Set the code to generate */
code128.setCode("#Participant_ID__c#");
color =  createobject("java","java.awt.Color");
image = code128.createAwtImage(color.black, color.white);
bufferedImage = createObject("java", "java.awt.image.BufferedImage");
bufferedImageType = bufferedImage.TYPE_BYTE_GRAY;
bufferedImage = bufferedImage.init(image.getWidth(JavaCast("null", "")),image.getHeight(JavaCast("null", "")), bufferedImageType);
graphics2D = bufferedImage.createGraphics();
graphics2D.drawImage(image,0,0,JavaCast("null", ""));
barcodeImage = imageNew(bufferedImage);
</cfscript>
<!--- Save the code as an image --->
<cfimage source="#barcodeImage#" action="write" overwrite="Yes" destination="barcodes/#Participant_ID__c#.jpg">

Open in new window

Avatar of gheist
gheist
Flag of Belgium image

You can specify size in lowagie library. Resizing adds blur and does not add to machine readability.
Avatar of joedfuse

ASKER

I added the following parameters to the script and dont get any errors but there is NO size difference

size = 10;
baseline = size;
barHeight = size * 4;

Open in new window

Because you set global variables, where you should be approaching object properties.
Why you use coldfusion to call java image resize? Actually coldfusion can do same, and calling java is meant for cases it cannot do something
Ok so i got this working using Barbecue lib now the issue is I cant find the parameter to save the barcode to a file see the code below

<cfscript>
Barcode = createObject("java", "net.sourceforge.barbecue.linear.code128.Code128Barcode").init(#url.p#);
BarcodeImageHandler = createObject("java", "net.sourceforge.barbecue.BarcodeImageHandler");
Barcode.setDrawingText(true);
Barcode.setBarHeight(30);
Barcode.setBarWidth(2);
context = getPageContext();
context.setFlushOutput(false);
response = context.getResponse().getResponse();
response.setContentType("image/jpeg");
BarcodeImageHandler.writeJPEG(Barcode, response.getOutputStream());
response.flush();
response.close();
</cfscript>

Open in new window


I am sure there is an output command that I can add but everyone i am trying is failing
SOLUTION
Avatar of gheist
gheist
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  How do I save it to a directory on the server?
SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Guys!

Looks like the following code is working nice. Let me know if you see anywhere it can be optimized.

<cfoutput>

<cfscript>
Barcode = createObject("java", "net.sourceforge.barbecue.linear.code128.Code128Barcode").init(#url.p#);
BarcodeImageHandler = createObject("java", "net.sourceforge.barbecue.BarcodeImageHandler");
Barcode.setDrawingText(false);
Barcode.setBarHeight(30);
Barcode.setBarWidth(2);

</cfscript>

<!--- extract the buffered image of the bar code --->
<cfset Handler = createObject("java", "net.sourceforge.barbecue.BarcodeImageHandler")>  
<cfset BuffImg = Handler.getImage( Barcode ) />  
<!--- use it create a CF image object --->
<cfset CFImageObject = ImageNew(buffImg) />  
<!--- do whatever you want with the image object --->
<cfimage source="#CFImageObject#" action="write" overwrite="Yes" destination="C:\inetpub\wwwroot\barcodes/#url.p#.jpg"> 

<img src="barcodes/#url.p#.jpg">
</cfoutput>

Open in new window

How are the bar codes ultimate used? Is this for display once only .. or do you need to save them for later use? If it is for one time use only, you may not want to bother with the file. Just use cfimage action="writeToBrowser" ...
We were thinking of using this to create ID cards for people to use to enter the building.  I already wrote an app to track the users by barcode swipe. My issue was that our hardware is very picky about the barcode so I needed more control over size.
Ok, then it make more sense to save them to a file.  

If you're not doing any other manipulation of the image on this page, it's quicker to save it directly to the file.  Also, be sure to validate the URL input first.  Primarily to ensure the bar code call doesn't blow up. However, it's a good idea to make sure it wasn't tampered with.

<cfscript>
// Should always validate url values first. Ensure #URL.p# 
// contains a valid code (and was not tampered with)
Barcode = createObject("java", "net.sourceforge.barbecue.linear.code128.Code128Barcode").init( URL.p );
BarcodeImageHandler = createObject("java", "net.sourceforge.barbecue.BarcodeImageHandler");
Barcode.setDrawingText(false);
Barcode.setBarHeight(30);
Barcode.setBarWidth(2);
OutFile = createObject("java", "java.io.File").init("C:\inetpub\wwwroot\barcodes\"& URL.p &".jpg");
BarcodeImageHandler.saveJPG( Barcode, OutFile );
</cfscript>

<cfoutput>
<img src="barcodes/#URL.p#.jpg">
</cfoutput>

Open in new window

You can get barcode font and print it in different sizes from word... Then try until it is read well.
I tried that font method but didnt have great results. The BBQ lib seems to be the best I found thus far