1、ZXingUtil.java 2、MatrixToImageWriter.java 3、MainTest.java 三个类
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;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
/**
* 使用Google ZXing工具生成二维码
*/
public class ZXingUtil {
static int width = 140;
static int height = 140;
/**
* 生成二维码图片到文件
*
* @param content 内容
* @param format 格式(PNG,JPG)
* @param imageName 图片名称
* @param src 图片路径
* @throws java.io.IOException
* @throws com.google.zxing.WriterException
*/
public static void encode(String content, String format, String imageName, String src) throws IOException, WriterException {
encode(content, width, height, format, imageName, src);
}
/**
* 生成二维码图片到文件
*
* @param content 内容
* @param width 宽度
* @param height 高度
* @param format 格式(PNG,JPG)
* @param imageName 图片名称
* @param src 图片路径
* @throws java.io.IOException
* @throws com.google.zxing.WriterException
*/
public static void encode(String content, int width, int height, String format, String imageName, String src) throws IOException, WriterException {
BitMatrix bitMatrix = getBitMatrix(content, width, height);
File file = new File(src + "/" + imageName + "." + format);
MatrixToImageWriter.write2File(bitMatrix, format, file);
}
/**
* 生成二维码图片到Stream
*
* @param content 内容
* @param format 格式(PNG,JPG)
* @throws java.io.IOException
* @throws com.google.zxing.WriterException
*/
public static void encode(String content, String format, OutputStream stream) throws IOException, WriterException {
BitMatrix bitMatrix = getBitMatrix(content, width, height);
MatrixToImageWriter.write2Stream(bitMatrix, format, stream);
}
/**
* 生成二维码图片到Stream
*
* @param content 内容
* @param width 宽度
* @param height 高度
* @param format 格式(PNG,JPG)
* @throws java.io.IOException
* @throws com.google.zxing.WriterException
*/
public static void encode(String content, int width, int height, String format, OutputStream stream) throws IOException, WriterException {
BitMatrix bitMatrix = getBitMatrix(content, width, height);
MatrixToImageWriter.write2Stream(bitMatrix, format, stream);
}
/**
* 返回二维码BitMatrix
*
* @param content 内容
* @param width 宽度
* @param height 高度
* @return
* @throws com.google.zxing.WriterException
*/
public static BitMatrix getBitMatrix(String content, int width, int height) throws WriterException {
// 用于设置QR二维码参数
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object> ();
// 设置QR二维码的纠错级别——这里选择最高H级别
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 设置编码方式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
return new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
}
}
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
public class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {
}
/**
* BitMatrix to BufferedImage
*
* @param matrix
* @return
*/
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;
}
/**
* qrcode to file
*
* @param matrix
* @param format
* @param file
* @throws java.io.IOException
*/
public static void write2File(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);
}
}
/**
* qrcode to stream
*
* @param matrix
* @param format
* @param stream
* @throws java.io.IOException
*/
public static void write2Stream(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);
}
}
}
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.carsource.utils.zxing.ZXingUtil;
@Controller
@RequestMapping("/zxing")
public class ZXingController {
/**
* 生成二维码
*
* @param cont 内容
* @param response
*/
@RequestMapping("/code")
public void code(String cont, HttpServletResponse response) throws Exception {
if(StringUtils.isBlank(cont)){
return;
}
ZXingUtil.encode(cont, "JPEG", response.getOutputStream());
}
}
前端展示?
自定义图案二维码?待定