感謝同事幫忙,這個主要是生成二維碼名片,然後點擊下載鏈接進行下載。
這是中文版
public void genChineseQrCode(){
StringBuffer content = new StringBuffer();
if(sysUser.getId() != null){
SysUser user = sysUserBusiness.findById(sysUser.getId());
content.append("BEGIN:VCARD\n" +
"VERSION:3.0\n");
if(user.getName() != null){
String name = user.getName();
content.append("FN:"+ name.substring(1,name.length())+" "+ name.charAt(0) +"\n" +
"N:"+ name.charAt(0)+";"+ name.substring(1,name.length()) +"\n");
}
if(user.getChCardPositionName() != null){
content.append("TITLE: " + user.getChCardPositionName()+"\n");
}
if(user.getMobile() != null){
String mobile = user.getMobile();
content.append("TEL;CELL:+86 " +mobile.substring(0, 3)+ " " + mobile.substring(3, 7) +" "+ mobile.substring(7, 11) +"\n");
}
content.append("TEL;WORK;VOICE:+86 21 3353 2222\n");
content.append("TEL;WORK;FAX:+86 21 3353 2222\n");
if(user.getEmail() != null){
content.append("EMAIL;WORK;INTERNET:"+ user.getEmail() +"\n");
}
content.append("URL:http://www.baidu.com\n" +
"ORG:百度有限公司\n" +
"END:VCARD");
this.getQrCode(content.toString(),user.getName(),"ch");
}
}
這是英文版
public void genEnglishQrCode(){
StringBuffer content = new StringBuffer();
if(sysUser.getId() != null){
SysUser user = sysUserBusiness.findById(sysUser.getId());
content.append("BEGIN:VCARD\n" +
"VERSION:3.0\n") ;
if(user.getEnName() != null){
String enName = user.getEnName();
content.append("FN:"+ enName.substring(0, enName.lastIndexOf("."))+ " " + enName.substring(enName.lastIndexOf(".") + 1, enName.length()) +"\n" +
"N:"+ enName.substring(enName.lastIndexOf(".") + 1, enName.length()) + ";" +enName.substring(0, enName.lastIndexOf(".")) +"\n") ;
}
if(user.getEnCardPositionName() != null){
content.append("TITLE:" + user.getEnCardPositionName()+"\n");
}
if(user.getMobile() != null){
String mobile = user.getMobile();
content.append("TEL;CELL:+86 " +mobile.substring(0, 3)+ " " + mobile.substring(3, 7) +" "+ mobile.substring(7, 11) +"\n");
}
content.append("TEL;WORK;VOICE:+86 21 3353 2222\n");
content.append("TEL;WORK;FAX:+86 21 3353 2222\n");
if(user.getEmail() != null){
content.append("EMAIL;WORK;INTERNET:"+user.getEmail()+"\n");
}
content.append("URL:http://www.baidu.com\n" +
"ORG:BAIDU ELECTRONIC TECHNOLOGY CO.,LTD.\n" +
"END:VCARD");
this.getQrCode(content.toString(),user.getName(),"en");
}
}
這是生成二維碼圖片代碼
public void getQrCode(String content,String name,String tag){
BufferedImage bufImg = null;
try {
Qrcode qrcodeHandler = new Qrcode();
// 設置二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
// 設置設置二維碼尺寸,取值範圍1-40,值越大尺寸越大,可存儲的信息越大
qrcodeHandler.setQrcodeVersion(13);
// 獲得內容的字節數組,設置編碼格式
byte[] contentBytes = content.getBytes("utf-8");
// 圖片尺寸
int imgSize = 67 + 12 * (13 - 1);
bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
// 設置背景顏色
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize, imgSize);
// 設定圖像顏色> BLACK
gs.setColor(Color.BLACK);
// 設置偏移量,不設置可能導致解析出錯
int pixoff = 2;
// 輸出內容> 二維碼
if (contentBytes.length > 0 && contentBytes.length < 800) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");
}
gs.dispose();
bufImg.flush();
OutputStream os = getResponse().getOutputStream();
ImageIO.write(bufImg, "png", os);
File file = null;
if(tag == "en"){
file = new File(name + "英文版.png");
}else{
file = new File(name + "中文版.png");
}
ImageIO.write(bufImg, "png", file);
getRequest().getSession().setAttribute("file", file);
} catch (Exception e) {
e.printStackTrace();
}
}
這是下載代碼,具有彈出框功能
/**
* 二維碼下載
*/
@SuppressWarnings("static-access")
public void download() throws UnsupportedEncodingException{
File file = (File)getRequest().getSession().getAttribute("file");
try {
new Download().download(getRequest(), getResponse(), file);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void download(HttpServletRequest req, HttpServletResponse resp, File file) throws IOException {
resp.setContentType("application/x-msdownload");
resp.setHeader("Content-Disposition", "attachment; filename="+ new String(file.getName().getBytes("GBK"),"ISO-8859-1"));
int fileLength = (int) file.length();
resp.setContentLength(fileLength);
if (fileLength != 0) {
InputStream ins;
try {
ins = new FileInputStream(file);
byte[] buf = new byte[4096];
ServletOutputStream sos;
try {
sos = resp.getOutputStream();
int readLength;
while (((readLength = ins.read(buf)) != -1)) {
sos.write(buf, 0, readLength);
}
ins.close();
sos.flush();
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}