zxing生成二维码

依赖:

com.google.zxing

方法参数:

EncodeHintType枚举类  传递给writers的一系列参数(并没有列举全面)
	ERROR_CORRECTION 指定二维码容错等级
	ErrorCorrectionLevel(M,L,H,Q四个等级由低到高)
	CHARACTER_SET 编码格式
	MARGIN 指定生成条形码时的空白(像素为单位)
	DATA_MATRIX_SHAPE 矩形形状
	PDF417_COMPACTION 指定为PDF417压缩格式
	PDF417_DIMENSIONS 指定PDF147最大和最小行数和列数
	QR_VERSION 指定qr版本

生成方法:

MultiFormatWriter().encode(内容,BarcodeFormat.QR_CODE,width,height,map)

内容:
就是你想把什么变成二维码
BarcodeFormat二维码的格式(一个枚举)
width:宽
height:高
map:存的就是EncodeHintType的参数
Image类
抽象类 其实现类为BufferedImage,一个带缓存区的图像类。
主要作用:将一幅图片加载到内存中
//创建一个不带透明色的对象
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

代码:

public class QrCodeGenerate {
    public static void main(String[] args) {

        int width = 300;
        int height = 300;
        //制定图片格式
        String format="png";
        //内容
        String content = "我是内容";

        //定义二维码的参数
        HashMap map = new HashMap();
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        map.put(EncodeHintType.MARGIN, 2);

        //生成二维码
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,map);
            Path file = new File("D:\\image\\1.jpg").toPath();//二维码存放路径
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值