2015-05-29 83 views
1

嗨我正面临一个问题的一代条形码在Java中。我使用code128生成条形码。生成条形码长度的数据是20位数字(123456789),我可以生成条形码,条形码的宽度为宽度165,高度为58。但是我需要108个宽度和29个高度作为相同的20位数输入。Java条码生成

我已经使用下面的代码生成条形码,请帮助,如果任何人有解决问题的想法。

package com.tcs.tpos.business.dayactivities; 

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.RenderingHints; 
import java.awt.image.BufferedImage; 
import java.io.File; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

import com.onbarcode.barcode.Code128; 



public class barTest { 


    public static void main(String[] args) throws Exception 
    { 



     String barip="1234567890"; 

     File poFile = new File("c:/tmp/work/barcode"+barip+ ".png"); 

     Code128 barcode=new Code128(); 

     barcode.setData(barip); 


     System.out.println("The Barocde height is been refered here " +barcode.getX()); 

     System.out.println("The Barcode width is been refered here " +barcode.getY()); 

     System.out.println("The Barcode width is been refered here " +barcode.getBarcodeHeight()); 


     barcode.setX(0.5f); 

     barcode.setY(15f); 

     barcode.setLeftMargin(0.1f); 

     barcode.setRightMargin(0.1f); 




     System.out.println("The Barcode width is been refered here " +barcode.getBarcodeHeight()); 


     System.out.println("The Barcode width is been refered here " +barcode.getBarcodeWidth()); 

     System.out.println("The Barcode width is been refered here " +barcode.getTopMargin()); 

     System.out.println("The Barcode width is been refered here " +barcode.getBottomMargin()); 

     System.out.println("The Barcode width is been refered here " +barcode.getLeftMargin()); 

     System.out.println("The Barcode width is been refered here " +barcode.getRightMargin()); 





     barcode.drawBarcode("c:/tmp/work/barcode"+barip+ ".png"); 

     ImageIcon oimage=new ImageIcon("c:/tmp/work/barcode"+barip+ ".png"); 


     BufferedImage inputImage=ImageIO.read(poFile); 

     BufferedImage genom=generateomImage(inputImage,113,25);    


     ImageIO.write(genom, "png", new File("c:/tmp/work/barcode"+barip+ ".png")); 



    } 

    public static BufferedImage generateomImage(BufferedImage ret,int width,int height) 
    { 

     int w = ret.getWidth(null); 
     int h = ret.getHeight(null); 
     Image scaledImage = ret.getScaledInstance((int)width, (int)height, Image.SCALE_SMOOTH); 
     BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
     Graphics2D g = result.createGraphics(); 
     g.drawImage(scaledImage, 0, 0, null); 
     g.dispose(); 

     return result; 

    } 

} 
+0

高度所以,如果你这样做发生了什么? –

回答

1

图像生成后,您不应该缩放Graphics2D中的条形码。这是因为重新缩放会使图像变形,从而导致图像无法扫描。

你应该从他们的网站查看他们的API,他们有设置barcodeWidth和Height的方法。

http://www.onbarcode.com/products/java_barcode/code_128_size_setting.html

集X,barcodeWidth,LEFTMARGIN和RightMargin设为与Java

barcode.setX(2f); 

    barcode.setbarcodeWidth(200); 

    barcode.setLeftMargin(1f); 

    barcode.setRightMargin(1f); 

设置代码128的宽度在与上述属性设置的128码宽,你需要注意的是设置固定条宽(X)后,将在Java应用程序中设置自动条形码宽度 - 最小条形码宽度。但是,如果set barcodeWidth值小于最小条码宽度,则Java中创建的Code 128图像宽度将为最小值。如果set barcodeWith大于最小值,则额外宽度将被添加到Java的右边和左边。 组Y,barcodeHeight,TOPMARGIN和bottomMargin设置代码128的使用Java

barcode.setY(75f); 

    barcode.setbarcodeHeight(100f); 

    barcode.setTopMargin(2f); 

    barcode.setBottomMargin(2f); 
+0

我试着将默认值设置为宽度和高度,对于这些默认值作为输入,生成的条形码(宽度为165,高度为20,输入数据为49)。 –

+0

有没有什么办法可以在java中为尺寸109生成条形码,宽度为29,高度为20位数为输入。我试着用代码128生成条形码,宽度为165。除了代码128之外,还有其他方式吗? –

+0

问题是关于“重新缩放”。考虑一个3像素长的图像,例如黑白色的白色,如果我将其从宽度3重新缩放到宽度2,结果会是什么?它变成50%黑色和50%白色?条形码是否仍具有与之前相同的上下文?类似于从165重新调整到108.但是,如果你说216到108,这可能值得一试。 –