生成二维码

1 File file=new File("E://zxd.jpg");//这个路径必须存在,不然回报路径不存在的nul异常

2 (Qrcode)使用了 qrcode-1.0.jar 第三方 包
(ImageIO)第二个jar就是jdK自带的 jre7\lib\rt.jar包
javax.imageio.ImageIO


package com.zct.yfq.mgm.tools;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

public class CordeUtil2 {

static int width = 90;
static int height = 90;


public static void main(String[] args) throws Exception {

File file=new File("E://zxd.jpg");
//encoderQRCoder("https://www.baidu.com/",file);
encoderQRCoder("小黄人,这个周出于什么原因不去练车",file);
}

public static void encoderQRCoder(String sms_info,File file) throws Exception {
try {
Qrcode testQrcode = new Qrcode();
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
testQrcode.setQrcodeVersion(7);
String testString = sms_info;
byte[] d = testString.getBytes("utf-8");
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = bi.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, width, height);
g.setColor(Color.BLACK);

// 限制最大字节数为119
if (d.length > 0 && d.length < 120) {
boolean[][] s = testQrcode.calQrcode(d);
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s.length; j++) {
if (s[j][i]) {
g.fillRect(j * 2, i * 2, 2, 2);
}
}
}
}
g.dispose();
bi.flush();
ImageIO.write(bi, "jpg", file);
} catch (Exception e) {
e.printStackTrace();
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值