java实现二维码的生成和解析

如今的二维码应用真是如火如荼,很多场景都在应用,很多网站、软件、app等都在使用,最为开发人员,无论是否真正使用的到,我们都应该了解一下二维码的基本操作,生成和解析。

 生成二维码的工具琳琅满目,有js操作的也有Java代码操作的,这里介绍一个Java代码实现的,来自Google 公司的zxing ,下面介绍一下他的 使用步骤。

第一步:引jar包,我的jar是直接从maven仓库中拷贝出来的。这里贴出来,避免大家再浪费时间去找了。(core-3.3.0.jar , javase-3.3.0.jar)两个jar包就可以了;jar包的下载地址和代码都放在这里(下载地址:点击打开链接);



第二步:引入jar,相信大家都会。


第三步:二维码的生成:

/**
     * 生成包含字符串信息的二维码图片
     * @param outputStream 文件输出流路径
     * @param content 二维码携带信息
     * @param qrCodeSize 二维码图片大小
     * @param imageFormat 二维码的格式
     * @throws WriterException 
     * @throws IOException 
     */
    public static boolean createQrCode(String  filePath, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{  
            //设置二维码纠错级别MAP
            Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();  
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  // 矫错级别  
            QRCodeWriter qrCodeWriter = new QRCodeWriter();  
            //创建比特矩阵(位矩阵)的QR码编码的字符串  
            BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);  
            // 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)
            int matrixWidth = byteMatrix.getWidth();  
            BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);  
            image.createGraphics();  
            Graphics2D graphics = (Graphics2D) image.getGraphics();  
            graphics.setColor(Color.WHITE);  
            graphics.fillRect(0, 0, matrixWidth, matrixWidth);  
            // 使用比特矩阵画并保存图像
            graphics.setColor(Color.BLACK);  
            for (int i = 0; i < matrixWidth; i++){
                for (int j = 0; j < matrixWidth; j++){
                    if (byteMatrix.get(i, j)){
                        graphics.fillRect(i-100, j-100, 1, 1);  
                    }
                }
            }
            OutputStream outputStream=new FileOutputStream(new File(filePath));
            return ImageIO.write(image, imageFormat, outputStream);  
    }  

上述方法中共有4个参数,按照顺序分别是(文件生成路径,存储内容,二维码尺寸,图片格式);


第四步:读取二维码中的数据:

 /**
     * 读二维码并输出携带的信息
     */
    public static void readQrCode(InputStream inputStream) throws IOException{  
        //从输入流中获取字符串信息
        BufferedImage image = ImageIO.read(inputStream);  
        //将图像转换为二进制位图源
        LuminanceSource source = new BufferedImageLuminanceSource(image);  
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
        QRCodeReader reader = new QRCodeReader();  
        Result result = null ;  
        try {
         result = reader.decode(bitmap);  
        } catch (ReaderException e) {
            e.printStackTrace();  
        }
        System.out.println(result.getText());  
    }

第五步:运行查看,采用主函数运行。

/**
     * 测试代码
     * @throws WriterException 
     */
    public static void main(String[] args) throws IOException, WriterException {  
        //createQrCode(new FileOutputStream(new File("d:\\qrcode.jpg")),"china is good",900,"JPEG");
    	createQrCode("C:\\Users\\28994\\Desktop\\qrcode.jpg","hebeibeifangxueyuanjiaowuchu",900,"JPEG");
        readQrCode(new FileInputStream(new File("C:\\Users\\28994\\Desktop\\qrcode.jpg")));  
    }  

查看结果:

   

以上就是运行产出的二维码。


欢迎关注公众号    IT技术自查    获取更多跟详细的IT情报



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值