`

ZXing 生成或读取二维码(解决中文乱码的问题)

阅读更多

ZXing 生成或读取二维码(解决中文乱码的问题) 

 

(一)zxing项目的介绍

ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。 其提供了多种平台下的客户端包括:J2ME、J2SE和Android。

Zxing可以解析的条码如下:

§ UPC-A and UPC-E

§ EAN-8 and EAN-13

§ Code 39

§ Code 128

§ QR Code

目前也也有实验支持DataMatrix和ITF编码。

你可以在

http://code.google.com/p/zxing/ 下载

 

(二)zxing项目库的组成部分

core: core decoding library, and the main component of the entire project

javame: JavaME client

javase: J2SE-specific client code

android: Android client (1.0 SDK)

androidtest: Android test app (1.0 SDK)

rim: RIM/Blackberry-specific client build

iphone: iPhone client + port to Objective C / C++ (QR code only)

zxingorg: The source behind zxing.org/w

zxing.appspot.com: The source behind our barcode generator

 

(三)ZXING 生成二维码

 

public class ZxingEncoderHandler
{
    public void encode(String contents, int width, int height, String imgPath)
    {
        try
        {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                    BarcodeFormat.QR_CODE, width, height);
            MatrixToImageWriter
                    .writeToFile(bitMatrix, "png", new File(imgPath));
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args)
    {
        String imgPath = "d:/qrcode.png";
        String contents = "您好!微信湖:http://www.weixinhu.net/";
        try
        {
            // 如果不用更改源码,将字符串转换成ISO-8859-1编码
            contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        int width = 300, height = 300;
        ZxingEncoderHandler handler = new ZxingEncoderHandler();
        handler.encode(contents, width, height, imgPath);

        System.out.println("生成二维码成功!");
    }
}

 

 

(四)ZXING 解析二维码

 

public class ZxingDecoderHandler
{
    public String decode(String imgPath)
    {
        BufferedImage image = null;
        Result result = null;
        try
        {
            image = ImageIO.read(new File(imgPath));
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

            Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
            // 注意要使用 utf-8,因为刚才生成二维码时,使用了utf-8
            hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
            result = new MultiFormatReader().decode(bitmap, hints);
            return result.getText();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args)
    {
        String imgPath = "d:/qrcode.png";
        ZxingDecoderHandler handler = new ZxingDecoderHandler();
        String decodeContent = handler.decode(imgPath);
        System.out.println("二维码内容:");
        System.out.println(decodeContent);
        System.out.println("解析二维码成功!");
    }
}

 

(五)、附件源码

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics