生活中隨處可見的二維碼大家都不陌生侍瑟,java中常用的是矩陣式二維碼QRCode蝎毡,生成QRCode有很多現(xiàn)成的工具,這里簡單記一下抛寝。
1.QRCode熊杨,這個是日本人寫的,下載QRCode.jar就可以用了
package com.util;
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 javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class QrCodeUtil {
/**
* 生成二維碼圖片
* @param content 二維碼內(nèi)容
* @param imgPath 二維碼圖片的保存路徑
*/
public static void getQrcodeImg(String content,String imgPath){
int width=140;
int height=140;
//實(shí)例化Qrcode
Qrcode qrcode=new Qrcode();
//設(shè)置二維碼的排錯率L(7%) M(15%) Q(25%) H(35%)
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
//設(shè)置二維碼尺寸(1~49)
qrcode.setQrcodeVersion(7);
//設(shè)置圖片尺寸
BufferedImage bufImg=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
//繪制二維碼圖片
Graphics2D gs=bufImg.createGraphics();
//設(shè)置二維碼背景顏色
gs.setBackground(Color.WHITE);
//創(chuàng)建一個矩形區(qū)域
gs.clearRect(0, 0, width, height);
//設(shè)置二維碼的圖片顏色值 黑色
gs.setColor(Color.BLACK);
//獲取內(nèi)容的字節(jié)數(shù)組,設(shè)置編碼集
try {
byte[] contentBytes=content.getBytes("utf-8");
int pixoff=2;
//輸出二維碼
if(contentBytes.length>0&&contentBytes.length<120){
boolean[][] codeOut=qrcode.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);
}
}
}
}
gs.dispose();
bufImg.flush();
//生成二維碼圖片
File imgFile=new File(imgPath);
ImageIO.write(bufImg, "png", imgFile);
System.out.println("二維碼生成成功盗舰!");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
getQrcodeImg("傻狗", "E:\\qrcode.png");
}
}
2.ZXing掉奄,google的開源項(xiàng)目晤锹,除了寫二維碼條形碼一維碼什么的也能寫
package com.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
public class ZxingUtil {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private ZxingUtil() {
}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
public static void main(String[] args) throws WriterException, IOException {
String text = "榆次丶吳彥祖"; // 二維碼內(nèi)容
int width = 300; // 二維碼圖片寬度
int height = 300; // 二維碼圖片高度
String format = "gif";// 二維碼的圖片格式
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 內(nèi)容所使用字符集編碼
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
// 生成二維碼
File outputFile = new File("E:" + File.separator + "zxing.jpg");
writeToFile(bitMatrix, format, outputFile);
System.out.println("生成成功");
}
}
3.jquery.qrcode插件宋渔,這個也很簡單毁靶,網(wǎng)頁端生成圖片,資源消耗小
頁面引入js文件
<script type="text/javascript" src="<%=basePath %>/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=basePath %>/js/jquery.qrcode.min.js"></script>```
寫js
('#qrcode').qrcode("http://www.baidu.com");
}); ```
-end-