java實現(xiàn)批量生成二維碼
更新時間:2019年05月29日 17:17:21 作者:coding~
這篇文章主要為大家詳細介紹了java實現(xiàn)批量生成二維碼的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)批量生成二維碼的具體代碼,供大家參考,具體內容如下
package com.youge.test;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.swetake.util.Qrcode;
import com.youge.util.JdbcDao;
import com.youge.util.WebUtil;
/**
* 批量二維碼
*/
public class Test {
private JdbcTemplate jt = JdbcDao.getJdbcTemplate();
public static void main(String[] args) throws IOException {
Test t=new Test();
for(int i=2018700001;i<2018700003;i++){
Map<String, Object> map=new HashMap<>();
String q_qrcode=i+"";//http://www.injiaxing.com:8080/youge/mApply/home.shtml?p_qrcode=
String p_code=i+"";
map.put("q_qrcode", q_qrcode);
map.put("p_code", p_code);
t.insertQrcode(map);
t.createQrcode(i+"");
}
}
public int insertQrcode(Map<String, Object> params){
String sql=" insert into y_qrcode (q_qrcode,p_code) values(?,?)";
return jt.update(sql,params.get("q_qrcode"),params.get("p_code"));
}
public void createQrcode(String str) throws IOException{
//計算二維碼圖片的高寬比
// API文檔規(guī)定計算圖片寬高的方式 ,v是本次測試的版本號
int v =6;
int width = 67 + 12 * (v - 1);
int height = 67 + 12 * (v - 1);
Qrcode x = new Qrcode();
/**
* 糾錯等級分為
* level L : 最大 7% 的錯誤能夠被糾正;
* level M : 最大 15% 的錯誤能夠被糾正;
* level Q : 最大 25% 的錯誤能夠被糾正;
* level H : 最大 30% 的錯誤能夠被糾正;
*/
x.setQrcodeErrorCorrect('L');
x.setQrcodeEncodeMode('B');//注意版本信息 N代表數(shù)字 、A代表 a-z,A-Z、B代表 其他)
x.setQrcodeVersion(v);//版本號 1-40
String qrData = "http://www.injiaxing.com:8080/youge/mApply/home.shtml?p_qrcode="+str;//內容信息
byte[] d = qrData.getBytes("utf-8");//漢字轉格式需要拋出異常
//緩沖區(qū)
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
//繪圖
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);
//偏移量
int pixoff = 2;
/**
* 容易踩坑的地方
* 1.注意for循環(huán)里面的i,j的順序,
* s[j][i]二維數(shù)組的j,i的順序要與這個方法中的 gs.fillRect(j*3+pixoff,i*3+pixoff, 3, 3);
* 順序匹配,否則會出現(xiàn)解析圖片是一串數(shù)字
* 2.注意此判斷if (d.length > 0 && d.length < 120)
* 是否會引起字符串長度大于120導致生成代碼不執(zhí)行,二維碼空白
* 根據(jù)自己的字符串大小來設置此配置
*/
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 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
//設置圖片格式,與輸出的路徑
ImageIO.write(bufferedImage, "png", new File("D:/qrcode/"+str+".png"));
System.out.println("二維碼生成完畢");
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring boot如何快速的配置多個Redis數(shù)據(jù)源
這篇文章主要介紹了Spring boot如何快速的配置多個Redis數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
Mybatis 實現(xiàn)一個搜索框對多個字段進行模糊查詢
這篇文章主要介紹了Mybatis 實現(xiàn)一個搜索框對多個字段進行模糊查詢,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

