生活中随处可见的二维码大家都不陌生,java中常用的是矩阵式二维码QRCode,生成QRCode有很多现成的工具,这里简单记一下。
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 二维码内容
* @param imgPath 二维码图片的保存路径
*/
public static void getQrcodeImg(String content,String imgPath){
int width=140;
int height=140;
//实例化Qrcode
Qrcode qrcode=new Qrcode();
//设置二维码的排错率L(7%) M(15%) Q(25%) H(35%)
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
//设置二维码尺寸(1~49)
qrcode.setQrcodeVersion(7);
//设置图片尺寸
BufferedImage bufImg=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
//绘制二维码图片
Graphics2D gs=bufImg.createGraphics();
//设置二维码背景颜色
gs.setBackground(Color.WHITE);
//创建一个矩形区域
gs.clearRect(0, 0, width, height);
//设置二维码的图片颜色值 黑色
gs.setColor(Color.BLACK);
//获取内容的字节数组,设置编码集
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的开源项目,除了写二维码条形码一维码什么的也能写
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 = "榆次丶吴彦祖"; // 二维码内容
int width = 300; // 二维码图片宽度
int height = 300; // 二维码图片高度
String format = "gif";// 二维码的图片格式
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
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插件,这个也很简单,网页端生成图片,资源消耗小
页面引入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-
网友评论