博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二维码
阅读量:6929 次
发布时间:2019-06-27

本文共 3679 字,大约阅读时间需要 12 分钟。

生成和解析使用zxing,下载链接 https://pan.baidu.com/s/1zN6aXfnsAlUud11J-nxirQ

package comsxlg.match;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.WriterException;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import java.io.*;import java.nio.file.Path;import java.util.HashMap;import java.util.Map;/** * 生成QRCode二维码 */public class CreateQRCode{    public static void writeCode()    {        int width = 300;        int height = 300;        String format = "png";        String content = "www.baidu.com";        // 定义二维码参数        Map params = new HashMap();        params.put(EncodeHintType.CHARACTER_SET,"utf-8"); // 设置字符编码        params.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 设置纠错等级        params.put(EncodeHintType.MARGIN,2);// 设置边距        try        {            // 设置内容,二维码类型,长度,宽度            BitMatrix  bitMatrix=  new MultiFormatWriter()                    .encode(content, BarcodeFormat.QR_CODE,width,height,params);            /*                方式一:直接输出文文件                // 获取保存路径                File file  = new File("D:/img.png");                if (!file.exists()){                    // 注意在创建文件时,如果存放文件的文件夹不存在,直接用createNewFile,会抛异常                    file.createNewFile();                }                Path path = file.toPath();                MatrixToImageWriter.writeToPath(bitMatrix,format,path);            */            // 输出到流            File file  = new File("D:/img1.png");            if (!file.exists()){                // 注意在创建文件时,如果存放文件的文件夹不存在,直接用createNewFile,会抛异常                file.createNewFile();            }            OutputStream out = new FileOutputStream(file);            MatrixToImageWriter.writeToStream(bitMatrix,format,out);            if (null != out ){                out.close();            }        }        catch (WriterException e)        {            e.printStackTrace();        }        catch (IOException e) {            e.printStackTrace();        }    }    public static void main(String[] args)    {        writeCode();    }}

 

package comsxlg.match;import com.google.zxing.*;import com.google.zxing.client.j2se.BufferedImageLuminanceSource;import com.google.zxing.common.HybridBinarizer;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Map;/*** * 读取QR Code 二维码 */public class ReadQRCode{    public static void main(String[] args)    {        ReadCode();    }    private static void ReadCode()    {        MultiFormatReader reader = new MultiFormatReader();        try        {            //File file  = new File("D:/img.png");            File file  = new File("E:\\spring_boot\\springboot\\src\\main\\resources\\static\\qrcode.dib");            BufferedImage image = ImageIO.read(file);            BinaryBitmap binaryBitmap = new BinaryBitmap(                    new HybridBinarizer(new BufferedImageLuminanceSource(image)));            Map params = new HashMap();            params.put(EncodeHintType.CHARACTER_SET,"utf-8"); // 设置字符编码            Result result = reader.decode(binaryBitmap,params);            System.out.println("content is :"+result.getText());            System.out.println("format is :"+result.getBarcodeFormat());            System.out.println("result is :"+result.toString());        }        catch (IOException e)        {            e.printStackTrace();        }        catch (NotFoundException e)        {        }    }}

 

转载于:https://www.cnblogs.com/nevegiveup/p/8563722.html

你可能感兴趣的文章
centos安装redis及php-redis扩展
查看>>
[DOM Event Learning] Section 4 事件分发和DOM事件流
查看>>
GBK、UTF8、UNICODE编码转换
查看>>
关于web页面性能测量指标与建议
查看>>
linux tar命令简介
查看>>
GTD时间管理(1)---捕获搜集
查看>>
分享web前端七款HTML5 Loading动画特效集锦
查看>>
HttpWebRequest和HttpWebResponse
查看>>
oracle10g获得Date类型字段无分,秒的解决方案!
查看>>
POJ2029——Get Many Persimmon Trees
查看>>
精彩理发头盔
查看>>
Android基调(十六)- Service:startService()、stopService()、bindService()、unbindService()加...
查看>>
linux 安装jdk
查看>>
ubuntu 编译android 源码笔记
查看>>
ExtJS4.2学习(10)分组表格控件--GroupingGrid(转)
查看>>
【iCore3 双核心板】例程六:IWDG看门狗实验——复位ARM
查看>>
使用AppCompat_v7 21.0.0d的几个兼容问题
查看>>
MODBUS RTU协议中浮点数是如何存储,读到浮点数寄存器的数值如何转换成所需的浮点数...
查看>>
微信公众号通过链接实现关注(盗用广告主流量主)
查看>>
JS实现类似QQ好友头像hover时显示资料卡的效果
查看>>