Servlet中用ZXing生成条码二维码并输出到web页面
zxing
ZXing ("Zebra Crossing") barcode scanning library for Java, Android
项目地址:https://gitcode.com/gh_mirrors/zx/zxing
免费下载资源
·
1、生成条码的servlet代码:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
/**
* @Description: 生成条码(CODE128格式) */
public class BarCode1DServlet extends HttpServlet {
/**
* @Fields serialVersionUID : default serialVersionUID
*/
private static final long serialVersionUID = 1L;
private static final String KEY = "keycode";
private static final String WIDTH = "mwidth";
private static final String HEIGHT = "mheight";
private static final String IMAGETYPE = "JPEG";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String keycode = req.getParameter(KEY);
if (keycode != null && !"".equals(keycode)) {
ServletOutputStream stream = null;
try {
Code128Writer writer = new Code128Writer();
int width=180;
int height=60;
String mwidth = req.getParameter(WIDTH);
if (mwidth != null && !"".equals(mwidth.trim())) {
try{
width=Integer.valueOf(mwidth);
} catch (NumberFormatException e) {
//TODO output to log
}
}
String mheight = req.getParameter(HEIGHT);
if (mheight != null && !"".equals(mheight.trim())) {
try{
height = Integer.valueOf(mheight);
} catch (NumberFormatException e) {
//TODO output to log
}
}
stream = resp.getOutputStream();
BitMatrix m = writer.encode(keycode, BarcodeFormat.CODE_128, width, height);
MatrixToImageWriter.writeToStream(m, IMAGETYPE, stream);
} catch (WriterException e) {
e.printStackTrace();
} finally {
if (stream != null) {
stream.flush();
stream.close();
}
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}
}
2、生成qrcode二维码的servlet示例
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
/**
* @Description: 生成二维码 (QR格式)
* @author lwei
*/
public class BarCode2DServlet extends HttpServlet {
/**
* @Fields serialVersionUID : serialVersionUID
*/
private static final long serialVersionUID = 1L;
private static final String KEY = "keycode";
private static final String SIZE = "msize";
private static final String IMAGETYPE = "JPEG";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String keycode = req.getParameter(KEY);
if (keycode != null && !"".equals(keycode)) {
ServletOutputStream stream = null;
try {
int size=129;
String msize = req.getParameter(SIZE);
if (msize != null && !"".equals(msize.trim())) {
try{
size=Integer.valueOf(msize);
} catch (NumberFormatException e) {
//TODO output to log
}
}
stream = resp.getOutputStream();
QRCodeWriter writer = new QRCodeWriter();
BitMatrix m = writer.encode(keycode, BarcodeFormat.QR_CODE, size, size);
MatrixToImageWriter.writeToStream(m, IMAGETYPE, stream);
} catch (WriterException e) {
e.printStackTrace();
} finally {
if (stream != null) {
stream.flush();
stream.close();
}
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}
}
3、页面引用
<img alt="条码" src="${pageContext.request.contextPath}/barCode?keycode=RT100200300400"></img>
<img alt="二维码" src="${pageContext.request.contextPath}/qrCode?keycode=RT100200300400500"></img>
转自网络,原作者已佚
GitHub 加速计划 / zx / zxing
32.53 K
9.31 K
下载
ZXing ("Zebra Crossing") barcode scanning library for Java, Android
最近提交(Master分支:2 个月前 )
8944e607
3 个月前
6ea3726b
Bumps `spring.version` from 6.1.5 to 6.1.8.
Updates `org.springframework:spring-test` from 6.1.5 to 6.1.8
- [Release notes](https://github.com/spring-projects/spring-framework/releases)
- [Commits](https://github.com/spring-projects/spring-framework/compare/v6.1.5...v6.1.8)
Updates `org.springframework:spring-web` from 6.1.5 to 6.1.8
- [Release notes](https://github.com/spring-projects/spring-framework/releases)
- [Commits](https://github.com/spring-projects/spring-framework/compare/v6.1.5...v6.1.8)
---
updated-dependencies:
- dependency-name: org.springframework:spring-test
dependency-type: direct:development
update-type: version-update:semver-patch
- dependency-name: org.springframework:spring-web
dependency-type: direct:development
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Sean Owen <srowen@gmail.com> 3 个月前
更多推荐
已为社区贡献6条内容
所有评论(0)