引言
在spring boot项目中整合zxing库来生成二维码是一个常见的需求。
zxing,全称"zebra crossing",是一个功能强大的开源java库,专门用于二维码的生成与解析。它不仅能够生成qr码,还能解析包括qr码在内的多种二维码格式。zxing提供了多语言api,使得开发者能够轻松地将二维码功能集成到各种应用中。它支持android、ios、java等多个平台,并且除了qr码,还能解析其他一维码和二维码,如ean、upc、datamatrix等。
1. 添加zxing库的依赖
<dependency>
<groupid>com.google.zxing</groupid>
<artifactid>core</artifactid>
<version>3.5.2</version>
</dependency>
<dependency>
<groupid>com.google.zxing</groupid>
<artifactid>javase</artifactid>
<version>3.5.2</version>
</dependency>
2. 生成二维码
创建一个springboot服务类qrcodeservice,用于生成二维码图片:
import com.google.zxing.barcodeformat;
import com.google.zxing.encodehinttype;
import com.google.zxing.multiformatwriter;
import com.google.zxing.client.j2se.matrixtoimagewriter;
import com.google.zxing.common.bitmatrix;
import com.google.zxing.qrcode.decoder.errorcorrectionlevel;
import org.springframework.stereotype.service;
import java.io.ioexception;
import java.nio.file.filesystems;
import java.nio.file.path;
import java.util.hashmap;
import java.util.map;
@service
public class qrcodeservice {
public void generateqrcodeimage(string text, int width, int height, string filepath)
throws ioexception {
map<encodehinttype, object> hints = new hashmap<>();
hints.put(encodehinttype.character_set, "utf-8");
hints.put(encodehinttype.error_correction, errorcorrectionlevel.m);
hints.put(encodehinttype.margin, 2);
bitmatrix bitmatrix = new multiformatwriter().encode(text, barcodeformat.qr_code, width, height, hints);
path path = filesystems.getdefault().getpath(filepath);
matrixtoimagewriter.writetopath(bitmatrix, "png", path);
}
}
3. 调用二维码服务
3.1 将二维码图拍你保存
最后在springboot的controller中调用这个服务:
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.restcontroller;
import java.io.ioexception;
@restcontroller
public class qrcodecontroller {
@autowired
private qrcodeservice qrcodeservice;
@getmapping("/generateqrcode")
public string generateqrcode(@requestparam string text, @requestparam int width, @requestparam int height) {
try {
qrcodeservice.generateqrcodeimage(text, width, height, "myqrcode.png");
return "qr code generated successfully!";
} catch (ioexception e) {
return "qr code generation failed: " + e.getmessage();
}
}
}
当访问/generateqrcode端点并传递text、width和height参数时,它将生成一个名为myqrcode.png的二维码图片并保存到项目根目录下。
http://localhost:8080/generateqrcode?text=hello,world!&width=350&height=350
3.2 直接返回二维码图片
修改qrcodecontroller来返回二维码图片:
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.core.io.bytearrayresource;
import org.springframework.core.io.resource;
import org.springframework.http.httpheaders;
import org.springframework.http.mediatype;
import org.springframework.http.responseentity;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.restcontroller;
import java.awt.image.bufferedimage;
import java.io.bytearrayoutputstream;
import java.io.ioexception;
import javax.imageio.imageio;
import com.google.zxing.barcodeformat;
import com.google.zxing.encodehinttype;
import com.google.zxing.multiformatwriter;
import com.google.zxing.common.bitmatrix;
import com.google.zxing.qrcode.decoder.errorcorrectionlevel;
@restcontroller
public class qrcodecontroller {
@getmapping("/generateqrcode")
public responseentity<resource> generateqrcode(@requestparam string text, @requestparam integer width, @requestparam integer height) throws ioexception {
bitmatrix bitmatrix = new multiformatwriter().encode(text, barcodeformat.qr_code, width, height, gethints());
bufferedimage qrcodeimage = matrixtoimagewriter.tobufferedimage(bitmatrix);
bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream();
imageio.write(qrcodeimage, "png", bytearrayoutputstream);
byte[] qrcodebytes = bytearrayoutputstream.tobytearray();
httpheaders headers = new httpheaders();
headers.add(httpheaders.content_type, mediatype.image_png_value);
return responseentity.ok()
.headers(headers)
.contentlength(qrcodebytes.length)
.body(new bytearrayresource(qrcodebytes));
}
private map<encodehinttype, object> gethints() {
map<encodehinttype, object> hints = new hashmap<>();
hints.put(encodehinttype.character_set, "utf-8");
hints.put(encodehinttype.error_correction, errorcorrectionlevel.m);
hints.put(encodehinttype.margin, 2);
return hints;
}
}
generateqrcode先生成二维码的bitmatrix,然后转换为bufferedimage,以便获取二维码图片的字节流。
3.2 注册bufferedimage消息转换器返回图片
3.2中返回图片也可以通过注册一个springboot的消息转换器来实现:
@bean
public httpmessageconverter<bufferedimage> createimagehttpmessageconverter() {
return new bufferedimagehttpmessageconverter();
}
返回图片
package com.example.demo.controller;
import java.awt.image.bufferedimage;
import org.springframework.http.httpstatus;
import org.springframework.http.mediatype;
import org.springframework.http.responseentity;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import com.google.zxing.barcodeformat;
import com.google.zxing.client.j2se.matrixtoimagewriter;
import com.google.zxing.common.bitmatrix;
import com.google.zxing.qrcode.qrcodewriter;
@restcontroller
@requestmapping("/qr")
public class qrcodecontroller {
@getmapping(value = "/{barcode}", produces = mediatype.image_png_value)
public responseentity<bufferedimage> barbecueean13barcode(@pathvariable("barcode") string barcode)
throws exception {
qrcodewriter barcodewriter = new qrcodewriter();
bitmatrix bitmatrix =
barcodewriter.encode(barcode, barcodeformat.qr_code, 200, 200);
return new responseentity<>(matrixtoimagewriter.tobufferedimage(bitmatrix),httpstatus.ok);
}
}
现在,当访问/qr端点时,将直接收到一个二维码图片作为响应。

到此这篇关于springboot中生成二维码的案例分享的文章就介绍到这了,更多相关springboot生成二维码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论