Java生成二维码并存储到MongoDB数据库中(或本地中)

【实现】

首先两个辅助方法:(来自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;

生成二维码的方法还有有很多种,这个是本人觉得相对较简单的方式,其他就不赘述了。

点此欢迎光临我的个人网站【一几文星球】

微信公众号,欢迎关注,一起学习。
在这里插入图片描述

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值