二维码生成--Springboot打包jar的方式

生成带logo的二维码

springboot打成jar之后,读取resources下的静态资源图片,然后根据读取的图片生成到logo的二维码。
jar打包之后在linux下不能用一般的方式进行读取,需采用流的形式。

获取姿态资源图片

InputStream inputStream = GenerateQRCodeUtil.class.getClassLoader.getResourceAsStream(QRCODE_LOGO );

pom依赖

<dependency>
		<groupId>com.google.zxing</groupId>
		<artifactId>javase</artifactId>
		<version>3.2.1</version>
</dependency>
<dependency>
		<groupId>com.google.zxing</groupId>
		<artifactId>core</artifactId>
		<version>3.2.1</version>
</dependency>

生成Base64二维码

@Component
public class GenerateQRCodeUtil {
	private static Logger logger = LoggerFactory.getLogger(GenerateQRCodeUtil .class);
	private static final String CHARSET = "UTF-8";
	private static final int HEIGHT = 200;
	private static final int WIDTH = 200;
	private static final int MARGIN = 0;
	private static final String FORMAT = "png";
	private static final String QRCODE_LOGO = "static/images/qrcode_logo.png";
	
	// 生成二维码
	public static BufferedImage createQRCodeWithLogo(String data) {
			logger.info("GenerateQRCodeUtil.createQRCodeWithLogo data: {}", data);
			try {
					// 读取jar里面的静态资源
					InputStream inputStream = GenerateQRCodeUtil.class.getClassLoader.getResourceAsStream(QRCODE_LOGO );
					BufferedImage logo = ImageIO.read(inputStream );
					return createQRCodeWithLogo(data, WIDTH, HEIGHT, MARGIN, logo);
			} catch () {
			}
	}

	// 生成带有logo的
	public static BufferedImage createQRCodeWithLogo(String data, int width, int height, int margin, BufferedImage logo) {
	BufferedImage bi= null;
	try {
			BufferedImage qrCode = createQRCode(data, width, height, margin);
			int deltaHeight = height - logo.getHeight();
			int deltaWidth = width - logo.getWidth();
			bi = new BufferedImage(hright, width, BufferedImage.TYPE_INT_ARGB);
			Graphics2D g = (Graphics2D) bi.getGraphics();
			g.drawImage(qrCode, 0, 0, null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
			g.drawImage(logo, (int)Math.round(deltaWidth / 2), (int)Math.round(deltaHeight / 2));
	} catch () {
	}
	return bi;
	}

	public static BufferedImage createQRCode(String data, int width, int height, int margin) {
	BitMatrix matrix = null;
	try {
			Map<EncodeHintType, Object> hints = new HashMap<>(2);
			hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
			hints.put(EncodeHintType.MARGIN, 0);
			matrix = new MultiFormatWriter().encode(data, BarcodeFormat.QR_CODE, width, height, hints);
	} catch(){
	}
	return MatrixToImageWriter.toBufferedImage(matrix);
	}
	
	// 获取base64字符串
	public static String writeToString (BufferedImage image) {
		String base64Str = "";
		try {
				ByteArrayOutputStream bos = new ByteArrayOutputStream ();
				OutputStream os = new Base64OutputStream(bos);
				writeToString(image, os);
				base64Str = bos.toString(CHARSET).replace("\r\n", "");
				base64Str = "data:image/png;base64," + base64Str;
		}catch(){
		}
		return base64Str ;
	}
	
	// 二维码写入流
	public static void writeToString(BufferedImage image, OutputStream outputStream) {
	try {
			ImageIO.write(image, FORMAT, outputStream);
	} catch(){
	}
	}

	public static void main(String[] args) {
	String data = "this is a test.";
	String base64Str = writeToString(createQRCodeWithLogo(data));
	System.out.println(base64Str );
	}

}

静态资源结构位置
src
----main
--------resources
------------static
----------------images
--------------------qrcode_logo.png

End

因为公司是内网,无法复制粘贴,异常和格式就不手动打了,最近在做扫码登录功能,所以写了一个工具类,用来记录学习。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值