【实现】
首先两个辅助方法:(来自MatrixToImageWriter类,该类是由Google提供的,可以将方法直接拷贝到代码中使用)
1. toBufferedImage
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;
}
注:
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
2. writeToStream
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);
}
}
然后具体实现方法:
1.生成二维码并存到本地
public static void main(String[] args) {
String contents = "Java生成二维码,并存到本地。";
int width = 430;
int height = 430;
String format = "gif";
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
File outputFile = new File("D:" + File.separator + "myqrcode.gif");
BufferedImage image = toBufferedImage(bitMatrix);
if (!ImageIO.write(image, format, outputFile)) {
throw new IOException(format + " not to " + outputFile);
}
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
效果
2.生成二维码并存到MongoDB数据库
public void testCreateQrCode() {
String contents = "Java生成二维码,并存到MongoDB数据库。";
int width = 430;
int height = 430;
String format = "png";
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage image = toBufferedImage(bitMatrix);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, format, os);
byte b[] = os.toByteArray();
grouponDAO.test(b, "5c404615166a7c081c906b40");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
效果
最后引用的包:
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
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;
生成二维码的方法还有有很多种,这个是本人觉得相对较简单的方式,其他就不赘述了。
点此欢迎光临我的个人网站【一几文星球】
微信公众号,欢迎关注,一起学习。