第一個類:QRCodeRequestForm
public class QRCodeRequestForm extends BaseForm {
private int width = 100;
private int height = 100;
private String imageType = "png";
@VNotEmpty
private String content;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getImageType() {
return imageType;
}
public void setImageType(String imageType) {
this.imageType = imageType;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
第二個類:QRCodeUtil
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
/**
* 二維碼工具類
*/
@SuppressWarnings("unchecked")
public class QRCodeUtil {
private static Log log = LogFactory.getLog(QRCodeUtil.class);
public static void encode(String contents, int width, int height, String type, OutputStream output) {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
MultiFormatWriter formatWriter = new MultiFormatWriter();
BitMatrix bitMatrix = formatWriter.encode(contents,BarcodeFormat.QR_CODE, width, height, hints);
//1.1去白邊
int[] rec = bitMatrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (bitMatrix.get(i + rec[0], j + rec[1])) {
resMatrix.set(i, j);
}
}
}
QRCodeUtil.writeToStream(resMatrix, type, output);
} catch (Exception e) {
log.error("QRCode utils exception", e);
}
}
private 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) ? 0 : 16777215);
}
}
return image;
}
private 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);
}
}
}
第三個類:QRCodeController
@Controller("QRCodeController")
public class QRCodeController extends BaseController {
@RequestMapping(value = "*/makeCode/QRCode")
public String bringRandCode(HttpServletResponse response,
@Valid @ModelAttribute QRCodeRequestForm form) {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0L);
OutputStream output = null;
try {
output = response.getOutputStream();
QRCodeUtil.encode(form.getContent(), form.getWidth(), form.getHeight(), form.getImageType(), output);
} catch(Exception e) {
logger.error("QRController exception", e);
} finally {
close(output);
}
return null;
}
private void close(OutputStream output) {
if(output != null) {
try {
output.flush();
output.close();
} catch(Exception e){}
finally {
output = null;
}
}
}
}