可直接生成二维码文件也可以通过outputStream 在点三方对象存储服务器上生成
//生成二维码
public class CreateQRCode {
private static final int BLACK = 0XFF000000;//前景色
private static final int WHITE = 0XFFFFFFFF;//背景色
private static int WIDTH=300;
private static int HEIGHT=300;
/**
*
*
* @param content 二维码内容
* @param fileName 图片名称 使用uuid生成
*/
public static Boolean createZxingqrCode(String content,String fileName){
Map hints=new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//设置编码
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//设置容错等级
hints.put(EncodeHintType.MARGIN, 2);//设置边距默认是5
Boolean bool = null;
//生成二维码
try{
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
BitMatrix bitMatrix = null;
bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT,hints);
BufferedImage image = toBufferedImage(bitMatrix);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//生成二维码图片及存放地址
ImageIO.write(image, "png",new File("D:/ceshi.jpg"));
//如果需要通过输出流在七牛平台上生成图片可使用
//ImageIO.write(image, "png", outputStream);
//qiniu(outputStream.toByteArray(), fileName);
}catch(Exception e){
return bool;
}
return bool;
}
/**
* 生成二维码内容<br>
*
* @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_ARGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
}
}
return image;
}