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

joedfuseAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gheistCommented:
You can specify size in lowagie library. Resizing adds blur and does not add to machine readability.
joedfuseAuthor Commented:
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

gheistCommented:
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
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

joedfuseAuthor Commented:
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
gheistCommented:
Better writePNG for automatic readability...
joedfuseAuthor Commented:
Thanks.  How do I save it to a directory on the server?
_agx_Commented:
EDIT: Fix copy + paste error

joedfuse - While that code works from CF, it's really geared towards java servlets. Aside from generating the bar code, the rest is just a longer way of writing:

           <cfcontent variable="#someBinaryObject#" type="image/jpeg">

The writePNG  method gheist suggested definitely works. There are also 2 other options:  

1) The more natural from a CF perspective is to extract the buffered image. Then use it to create a CF Image object. The rest is standard CF code.

<!--- 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(buffImage) />  
<!--- do whatever you want with the image object -->
<cfimage source="#CFImageObject#" action="write" destination="c:/path/to/FileName.jpg"> 

Open in new window


2)  However, unless you need to manipulate the image, it is simpler to take the java.io.File route: Save the image directly to a file. This is very similar to the writePNG/JPG/GIF approach that gheist suggested:


<!--- create a file object representing the path where the image will be saved -->
<cfset OutFile = createObject("java", "java.io.File").init("c:/path/to/FileName.jpg")>  
<cfset Handler = createObject("java", "net.sourceforge.barbecue.BarcodeImageHandler")>  
<!--- save it to the file  -->
<cfset Handler.saveJPG( Barcode, OutFile ) />  

Open in new window

_agx_Commented:
I forgot FileOutputStream also accepts a string path, so here's the same example from above, only using the  writePNG/JPG/GIF method gheist suggested (not tested):

<cfset Handler = createObject("java", "net.sourceforge.barbecue.BarcodeImageHandler")>  
<!--- create a file object representing the path where the image will be saved -->
<cfset OutFile = createObject("java", "java.io.FileOutputStream").init("c:/path/to/FileName.jpg")>  
<!--- save it to the file  -->
<cfset Handler.writeJPG( Barcode, OutFile ) />  

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joedfuseAuthor Commented:
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

_agx_Commented:
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" ...
joedfuseAuthor Commented:
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.
_agx_Commented:
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

gheistCommented:
You can get barcode font and print it in different sizes from word... Then try until it is read well.
joedfuseAuthor Commented:
I tried that font method but didnt have great results. The BBQ lib seems to be the best I found thus far
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.