java生成二维码

1.使用Q-code生成二维码
Qrcode x = new Qrcode();
x.setQrcodeErrorCorrect(‘M’);//纠错等级
x.setQrcodeEncodeMode(‘B’);//B其他数字
x.setQrcodeVersion(7);//版本
int width=67+12*(7-1);//图片的宽度
int height=67+12*(7-1);//图片的高度
String qrData = “www.baidu.com”;
BufferedImage buffreImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);//设置图片的高度
Graphics2D gs = buffreImage.createGraphics();//图片画笔
gs.setBackground(Color.white);//白色背景
gs.setColor(Color.black);//字体的颜色
gs.clearRect(0, 0, width, height);//画矩形
byte[] d=new byte[120];//使用字节保存字符串
try {
d = qrData.getBytes(“utf-8”);//字节编码方式
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

	int pos = 5;
	if (d.length > 0 && d.length < 120) {

		boolean[][] s = x.calQrcode(d);

		for (int i = 0; i < s.length; i++) {

			for (int j = 0; j < s.length; j++) {

				if (s[j][i]) {

					gs.fillRect(j * 3 + pos, i * 3 + pos, 3, 3);//填充矩形
				}
			}

		}

	}
	gs.dispose();
	buffreImage.flush();//刷新流
	  try {
		ImageIO.write(buffreImage, "png", new File("F:/qrcode.png"));//使用ImageIo进行生成
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

使用zxing1

	 int width =200;
	 int height=200;
	 String format="png";
	 String content="hbnb";
	 
	 HashMap map=new HashMap();
	 map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
	 map.put(EncodeHintType.MARGIN, 2);
	 map.put(EncodeHintType.CHARACTER_SET, "utf-8");
	  
	 
 	 try {
		BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
		Path file =new File("D:"+File.separator+"wangzhongyuan.png").toPath();
	   try {
		MatrixToImageWriter.writeToPath(bitMatrix, format, file);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	} catch (WriterException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值