public class ZxingTest {
public static void main(String[] args) throws WriterException, IOException {
String mecard = "MECARD:N:王**;ORG:杭州;EMAIL:wgshuaiit@163.com;ADR:杭州;NOTE:java開(kāi)發(fā);;";
// 還是亂碼 mecard = new String(mecard.getBytes("ISO8859-1"), "UTF-8");
//解決亂碼
Map<EncodeHintType,String> hints = Maps.newHashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(mecard, BarcodeFormat.QR_CODE, 200, 200,hints);
MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream("D:/qr.png"));
}
}
/**
* 將客戶信息生成二維碼
*/
@RequestMapping(value = "/qrcode/{id:\\d+}.png",method = RequestMethod.GET)
public void makeQrCode(@PathVariable Integer id,HttpServletResponse response) throws IOException, WriterException {
String mecard = customerService.makeMeCard(id);
Map<EncodeHintType,String> hints = Maps.newHashMap();
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(mecard, BarcodeFormat.QR_CODE,200,200,hints);
OutputStream outputStream = response.getOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix,"png",outputStream);
outputStream.flush();
outputStream.close();
}
/**
* 將客戶信息生成MECard格式
* @param id
* @return
*/
public String makeMeCard(Integer id) {
Customer customer = customerMapper.findById(id);
StringBuilder mecard = new StringBuilder("MECARD:");
if(StringUtils.isNotEmpty(customer.getName())) {
mecard.append("N:"+customer.getName()+";");
}
if(StringUtils.isNotEmpty(customer.getTel())) {
mecard.append("TEL:"+customer.getTel()+";");
}
if(StringUtils.isNotEmpty(customer.getEmail())) {
mecard.append("EMAIL:"+customer.getEmail()+";");
}
if(StringUtils.isNotEmpty(customer.getAddress())) {
mecard.append("ADR:"+customer.getAddress()+";");
}
if(StringUtils.isNotEmpty(customer.getCompanyname())) {
mecard.append("ORG:"+customer.getCompanyname()+";");
}
mecard.append(";");
return mecard.toString();
}
使用
div class="box-body" style="text-align: center">
![](/makeQrCode.png)
</div>
工具類
package com.kaishengit.util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
/**
* 二維碼工具類
*/
public class QrCodeUtil {
// 默認(rèn)二維碼寬度
private static final int width = 300;
// 默認(rèn)二維碼高度
private static final int height = 300;
// 默認(rèn)二維碼文件格式
private static final String format = "png";
// 二維碼參數(shù)
private static final Map<EncodeHintType, Object> hints = new HashMap();
static {
// 字符編碼
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 容錯(cuò)等級(jí) L重贺、M、Q、H 其中 L 為最低, H 為最高
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 二維碼與圖片邊距
hints.put(EncodeHintType.MARGIN, 2);
}
/**
* 將二維碼圖片輸出到一個(gè)流中
* @param content 二維碼內(nèi)容
* @param stream 輸出流
* @param width 寬
* @param height 高
*/
public static void writeToStream(String content, OutputStream stream, int width, int height) throws WriterException, IOException {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, format, stream);
}
/**
* 生成二維碼圖片文件
* @param content 二維碼內(nèi)容
* @param path 文件保存路徑
* @param width 寬
* @param height 高
*/
public static void createQRCode(String content, String path, int width, int height) throws WriterException, IOException {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//toPath() 方法由 jdk1.7 及以上提供
MatrixToImageWriter.writeToPath(bitMatrix, format, new File(path).toPath());
}
}
maven 依賴
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.0.0</version>
</dependency>
package com.kaishengit;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Hashtable;
public class Test {
public static void main(String[] args) throws IOException {
String text = "www.baidu.com";
int width = 100;
int height = 100;
String format = "png";
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
Path file = new File("D:/new.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//解析二維碼
resolve();
}
private static void resolve() {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File("D:/new.png");
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
Result result = null;
try {
result = formatReader.decode(binaryBitmap, hints);
} catch (NotFoundException e) {
e.printStackTrace();
}
System.err.println("解析結(jié)果:" + result.toString());
System.out.println(result.getBarcodeFormat());
System.out.println(result.getText());
}
}