论坛首页 Java企业应用论坛

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

浏览 16245 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-07-05  

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("解析二维码成功!");
    }
}

 

(五)、附件源码

 

 

 

 

   发表时间:2013-07-09  
Map<EncodeHintType,String> hints = Maps.newHashMap();
            //内容所使用编码 
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE,
                    width, height,hints);
            File file = new File(imgPath, fileName);
            MatrixToImageWriter.writeToFile(bitMatrix, filePostfix, file);

三行代码解决你的问题
0 请登录后投票
   发表时间:2013-07-09  
kfyfly 写道
Map<EncodeHintType,String> hints = Maps.newHashMap();
            //内容所使用编码 
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE,
                    width, height,hints);
            File file = new File(imgPath, fileName);
            MatrixToImageWriter.writeToFile(bitMatrix, filePostfix, file);

三行代码解决你的问题

这就是翻看API文档的好处。。。
0 请登录后投票
   发表时间:2013-07-12  
kfyfly 写道
Map<EncodeHintType,String> hints = Maps.newHashMap();
            //内容所使用编码 
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE,
                    width, height,hints);
            File file = new File(imgPath, fileName);
            MatrixToImageWriter.writeToFile(bitMatrix, filePostfix, file);

三行代码解决你的问题

生成的图片有没有最小width、height之说?
就是说我不想设定这两个属性,但要给我生成一个可识别的最小图片
0 请登录后投票
   发表时间:2014-02-08  

三行代码解决你的问题
生成的图片有没有最小width、height之说?
就是说我不想设定这两个属性,但要给我生成一个可识别的最小图片

这个应该可以把,在生成条码的同时可以DIY barcode 的尺寸大小
0 请登录后投票
   发表时间:2014-04-08  
liuchenwei2000 写道
kfyfly 写道
Map<EncodeHintType,String> hints = Maps.newHashMap();
            //内容所使用编码 
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE,
                    width, height,hints);
            File file = new File(imgPath, fileName);
            MatrixToImageWriter.writeToFile(bitMatrix, filePostfix, file);


三行代码解决你的问题

生成的图片有没有最小width、height之说?
就是说我不想设定这两个属性,但要给我生成一个可识别的最小图片


目前来说的话,好像QR code 最小的面积在1.25和1.75之间,这个主要是针对手机扫描的哈,所以你可以上网百度或者谷歌一下,有很多文章有讲这个。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics