Java的方式生成条形码

用Java的方式生成条形码可以有两种方式:

1.用servlet的方式来生成

2.纯Java的方式来生成


第一种:

代码如下:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String code = request.getParameter("code");
		
		try {  
            ByteArrayOutputStream outputStream = null;  
            BufferedImage bi = null;  
            int len = code.length();  
            JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),  
                    WidthCodedPainter.getInstance(),  
                    EAN13TextPainter.getInstance());  
   
            // 尺寸,面积,大小 密集程度  
            productBarcode.setXDimension(Double.valueOf("0.5").doubleValue());  
            // 高度 10.0 = 1cm 默认1.5cm  
            productBarcode.setBarHeight(Double.valueOf("30").doubleValue());  
            // 宽度  
            productBarcode.setWideRatio(Double.valueOf(30).doubleValue());  
//              是否显示字体  
            productBarcode.setShowText(true);  
//             显示字体样式  
            //System.out.println(BaseLineTextPainter.getInstance().toString());
            productBarcode.setTextPainter(BaseLineTextPainter.getInstance());   
   
            // 生成二维码  
            bi = productBarcode.createBarcode(code);  
            
            ImageIO.write(bi, "jpg", response.getOutputStream());  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
	}
这种方式生成的条形码会出现在jsp页面里,你可以在浏览器中访问你的servlet就可已看见它


第二种将条形码生成到本地:

既然是生成到本地,那肯定要用到IO流

代码如下:

package cn.wge.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;
public class Util { 
    /**
     * 生成商品条形码
     *
     * @param filePath
     *            商品条形码图片存放路径:../xxx/yyy/
     * @param jbarCode
     *            商品条形码:8位、13位
     * @param format
     *            商品条形码图片格式:.gif/.png/.jpg/.jpeg
     * @return 图片存放路径+图片名称+图片文件类型
     */
    public String createBarCode(String filePath, String jbarCode, String format) {
    	
        String barCodeName = jbarCode + format;

        try {
            BufferedImage bi = null;          
            JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),
                    WidthCodedPainter.getInstance(),
                    EAN13TextPainter.getInstance());
 
            bi = productBarcode.createBarcode(jbarCode);
            saveToJPG(bi, filePath, barCodeName);
            // 尺寸,面积,大小 密集程度  
            productBarcode.setXDimension(Double.valueOf("0.5").doubleValue());  
            // 高度 10.0 = 1cm 默认1.5cm  
            productBarcode.setBarHeight(Double.valueOf("30").doubleValue());  
            // 宽度  
            productBarcode.setWideRatio(Double.valueOf(30).doubleValue());  
            //是否显示字体  
            productBarcode.setShowText(true);
            return filePath + barCodeName;
        } catch (Exception localException) {
            localException.printStackTrace();
            return null;
        }
    }
    /**
     * 生成JPEG图片
     *
     * @param paramBufferedImage
     * @param paramString
     */
    @SuppressWarnings("unused")
    private void saveToJPG(BufferedImage paramBufferedImage, String filePath,
            String fileName) {
        saveToFile(paramBufferedImage, filePath, fileName, "jpeg");
    }
 
    /**
     * 生成PNG图片
     *
     * @param paramBufferedImage
     * @param paramString
     */
    @SuppressWarnings("unused")
    private void saveToPNG(BufferedImage paramBufferedImage, String filePath,
            String fileName) {
        saveToFile(paramBufferedImage, filePath, fileName, "png");
    }

    /**
     * 保存图片文件
     *
     * @param paramBufferedImage
     *            图片流
     * @param filePath
     *            文件路径
     * @param imgName
     *            图片参数
     * @param imgFormat
     *            图片格式
     */
    private  void saveToFile(BufferedImage paramBufferedImage, String filePath,
            String imgName, String imgFormat) {
    	
        try {
        	System.out.println("saveToFile");
            FileOutputStream fileOutputStream = null;
            try {
                
                String dirPath = filePath;
                
                File dirFile = new File(dirPath);
                if (!dirFile.exists()) {
                    dirFile.mkdirs();
                }
                String imgPath = dirPath + imgName;
                fileOutputStream = new FileOutputStream(imgPath);
            } catch (Exception e) {
                System.out.println("Create Img File Error:" + e.toString());
            }
            ImageUtil.encodeAndWrite(paramBufferedImage, imgFormat,
                    fileOutputStream, 96, 96);
            fileOutputStream.close();
        } catch (Exception localException) {
            System.out.println("Save Img File Error:" + localException);
            localException.printStackTrace();
        }
    }
}




  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值