首页>代码>java利用zxing生成仿新浪微博二维码>/qrcode/src/cn/qrcode/ZxingEAN13EncoderHandler.java
package cn.qrcode;

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;

/**
 */
public class ZxingEAN13EncoderHandler {

    /**
     * 编码
     * @param contents
     * @param width
     * @param height
     * @param imgPath
     */
    public void encode(String contents, int width, int height, String imgPath) {
        int codeWidth = 3 + // start guard
                (7 * 6) + // left bars
                5 + // middle guard
                (7 * 6) + // right bars
                3; // end guard
        codeWidth = Math.max(codeWidth, width);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.EAN_13, codeWidth, height, null);
            MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
    	String imgPath = "src/zxing_EAN13.png";
        encode();
        System.out.println(decode(imgPath));
    }

	private static void encode() {
		String imgPath = "src/zxing_EAN13.png";
        // 益达无糖口香糖的条形码
        String contents = "6923450657713";

        int width = 105, height = 50;
        ZxingEAN13EncoderHandler handler = new ZxingEAN13EncoderHandler();
        handler.encode(contents, width, height, imgPath);
	}
    
    /** 
     * 解码
     * @param imgPath 
     * @return String 
     */  
    public static String decode(String imgPath) {  
        BufferedImage image = null;  
        Result result = null;  
        try {  
            image = ImageIO.read(new File(imgPath));  
            if (image == null) {  
                System.out.println("the decode image may be not exit.");  
            }  
            LuminanceSource source = new BufferedImageLuminanceSource(image);  
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
  
            result = new MultiFormatReader().decode(bitmap, null);  
            return result.getText();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
}

最近下载更多
xuexizhuanyong23  LV16 2022年3月23日
yangctz  LV24 2021年9月29日
yiangeee  LV9 2021年1月14日
abcd11112  LV6 2020年8月12日
hanweinan6  LV12 2020年6月29日
huasir2018  LV14 2020年5月9日
霸道小盆友  LV3 2020年4月30日
sonofy123  LV9 2020年2月14日
kong.yee  LV40 2020年2月1日
15838634741  LV18 2019年12月4日
最近浏览更多
crosa_Don  LV18 2022年9月22日
liys1234  LV9 2022年4月22日
xuexizhuanyong23  LV16 2022年3月23日
newhaijun  LV15 2022年3月2日
Q375892799  LV9 2022年2月25日
yangctz  LV24 2021年9月29日
whfuai  LV14 2021年6月16日
yiangeee  LV9 2021年1月14日
MrReady  LV14 2020年12月27日
1126055836  LV13 2020年11月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友