如题
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.google.zxing.WriterException;
import com.google.zxing.qrcode.decoder.QRCodeDecoderMetaData;
import com.google.zxing.qrcode.encoder.QRCode;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import cn.jbolt.common.tools.CrossDomainInterceptor;
import cn.jbolt.common.tools.QRcodeTools;
@Before(CrossDomainInterceptor.class)
public class QRcodeController extends Controller{
@Inject
private static QRcodeTools qt = new QRcodeTools();
/**
* Jfinal官方二维码生成器
* 底层调用的是zxing
* 返回的是一个即时的二维码图片,只能手动保存,不方便进行下载
* @content 二维码内容
* @width 二维码宽度
* @height 二维码高度
*/
public void getQRcode() {
String content = getPara("content");
Integer width = getParaToInt("width",400);
Integer height = getParaToInt("height",400);
if (content==null) {
renderText("content 参数不能为空");
return;
}
renderQrCode(content, width, height);
}
/**
* 调用工具类,可以实现二维码下载
* @content 二维码内容
* @filename 要保存的二维码图片的名称(不带后缀)
* @format 图片格式(jpg,png...默认png)
* @size 图片尺寸(正方形,默认400px)
*/
public void getQRcodeDownload() {
String content = getPara("content");
String filename = getPara("filename");
String format = getPara("format","png");
Integer size = getParaToInt("size",400);
if (content==null) {
renderText("content 参数不能为空");
return;
}
/*
* 注意!!!
*/
//此处需要手动在项目根路径下创建一个名为“临时”的文件夹
String filepath = "临时/"+filename+"."+format;
File file = new File(filepath);
try {
//调用工具类
BufferedImage createQrCode = qt.createQrCode(content, size, format);
ImageIO.write(createQrCode, format, file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
renderFile(file);
}
}
工具类(非自己编写,借鉴了现成的)
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
public class QRcodeTools {
/**
* 生成包含字符串信息的二维码图片
* @param content 二维码携带信息
* @param qrCodeSize 二维码图片大小
* @param imageFormat 二维码的格式
* @throws IOException
*/
public static BufferedImage createQrCode(String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{
Hashtable<EncodeHintType, Object> hintMap = new Hashtable<EncodeHintType, Object>();
// 编码设置为 UTF-8
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 设置二维码四周白色区域大小
hintMap.put(EncodeHintType.MARGIN,2);
// 设置二维码的容错性
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//创建比特矩阵(位矩阵)的QR码编码的字符串
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth-65, matrixWidth-65, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// 使用比特矩阵画并保存图像
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++){
for (int j = 0; j < matrixWidth; j++){
if (byteMatrix.get(i, j)){
graphics.fillRect(i-33, j-33, 2, 2);
}
}
}
return image;
}
/**
* 读二维码并输出携带的信息
*/
public static void readQrCode(InputStream inputStream) throws IOException{
//从输入流中获取字符串信息
BufferedImage image = ImageIO.read(inputStream);
//将图像转换为二进制位图源
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result result = null ;
try {
result = reader.decode(bitmap);
} catch (ReaderException e) {
e.printStackTrace();
}
System.out.println(result.getText());
}
}