在哪生成二维码就把下面这个代码放到哪里
<img class="q_code" src="/xiaotong/QRCode/getQ" />
下面是java代码
package com.xiaotong.controller;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.xiaotong.bean.VersionsNumber;
import com.xiaotong.service.app.VersionsNumberAppService;
import com.xiaotong.utils.MatrixToImageWriter;
@Controller
@RequestMapping(value="/QRCode")
public class QRCode {
@Autowired
private VersionsNumberAppService versionsNumberAppService;
@RequestMapping(value = "/getQ", method = { RequestMethod.POST, RequestMethod.GET })
public void getqcode(HttpServletResponse resp,HttpServletRequest request ,String id) throws IOException {
int tomCat = request.getLocalPort();//端口号
String ip = request.getLocalAddr();//tomcat运行的ip地址
String path = request.getContextPath();//项目名
System.out.println(ip);
id="http://"+ip+":"+tomCat+path+"/app/xiaotong.jsp";
String url = id;
if (url != null && !"".equals(url)) {
ServletOutputStream stream = null;
try {
int width = 200;// 图片的宽度
int height = 200;// 高度
stream = resp.getOutputStream();
QRCodeWriter writer = new QRCodeWriter();
BitMatrix m = writer.encode(url, BarcodeFormat.QR_CODE, height, width);
MatrixToImageWriter.writeToStream(m, "jpg", stream);
} catch (WriterException e) {
e.printStackTrace();
} finally {
if (stream != null) {
stream.flush();
stream.close();
}
}
}
}
}
package com.xiaotong.utils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import com.google.zxing.common.BitMatrix;
/**
* 二维码
*
* @author admin
*
*/
public class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {
}
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);
}
}
}
jar包下载
jar包下载地址:http://download.csdn.net/download/qq_37172500/9995432