joedfuse
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.
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">
You can specify size in lowagie library. Resizing adds blur and does not add to machine readability.
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;
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
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
ASKER
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
I am sure there is an output command that I can add but everyone i am trying is failing
<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>
I am sure there is an output command that I can add but everyone i am trying is failing
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. How do I save it to a directory on the server?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Guys!
Looks like the following code is working nice. Let me know if you see anywhere it can be optimized.
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>
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" ...
ASKER
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.
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>
You can get barcode font and print it in different sizes from word... Then try until it is read well.
ASKER
I tried that font method but didnt have great results. The BBQ lib seems to be the best I found thus far