安徽做网站找谁,书画院网站模板,网站建设使用工具,wordpress性能优化前面我们知道了怎么通过 使用 zxing 生成二维码以及条形码#xff0c; 由于我们现在都是 web 端的项目了#xff0c;那么我们看下怎么使用 Spring Boot 集成然后返回给前端展示#xff1a;
工程源码
对应的工程源码我放到了这里#xff1a;github源码路径#xff0c;点击…
前面我们知道了怎么通过 使用 zxing 生成二维码以及条形码 由于我们现在都是 web 端的项目了那么我们看下怎么使用 Spring Boot 集成然后返回给前端展示
工程源码
对应的工程源码我放到了这里github源码路径点击这里查看
开始搭建
这里的整个过程就很简单了引入依赖包还是和之前一样另外搭建就两部分
controller 层utils 层
引入依赖
dependencygroupIdcom.google.zxing/groupIdartifactIdcore/artifactIdversion3.4.1/version
/dependencydependencygroupIdcom.google.zxing/groupIdartifactIdjavase/artifactIdversion3.4.1/version
/dependency生成二维码
对应的 controller 代码示例
RestController
RequestMapping(path /qrcode)
public class QrCodeController {// http://localhost:8080/qrcode/create?contentwww.baidu.comGetMapping(path /createQrCode)public void createQrCode(HttpServletResponse response, RequestParam(content) String content) {try {// 创建二维码BufferedImage bufferedImage QrCodeUtils.createImage(content, null, false);// 通过流的方式返回给前端responseImage(response, bufferedImage);} catch (Exception e) {e.printStackTrace();}}/*** 设置 可通过 postman 或者浏览器直接浏览** param response response* param bufferedImage bufferedImage* throws Exception e*/public void responseImage(HttpServletResponse response, BufferedImage bufferedImage) throws Exception {ByteArrayOutputStream byteArrayOutputStream new ByteArrayOutputStream();ImageOutputStream imageOutput ImageIO.createImageOutputStream(byteArrayOutputStream);ImageIO.write(bufferedImage, jpeg, imageOutput);InputStream inputStream new ByteArrayInputStream(byteArrayOutputStream.toByteArray());OutputStream outputStream response.getOutputStream();response.setContentType(image/jpeg);response.setCharacterEncoding(UTF-8);IOUtils.copy(inputStream, outputStream);outputStream.flush();}
}对应的 工具类 QrCodeUtils
Component
public class QrCodeUtils {private static final String CHARSET UTF-8;private static final String FORMAT_NAME JPG;/*** 二维码尺寸*/private static final int QRCODE_SIZE 300;/*** LOGO宽度*/private static final int WIDTH 60;/*** LOGO高度*/private static final int HEIGHT 60;/*** 创建二维码图片** param content 内容* param logoPath logo* param isCompress 是否压缩Logo* return 返回二维码图片* throws WriterException e* throws IOException BufferedImage*/public static BufferedImage createImage(String content, String logoPath, boolean isCompress) throws WriterException, IOException {HashtableEncodeHintType, Object hints new Hashtable();// 设置二维码的错误纠正级别 高hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置字符集hints.put(EncodeHintType.CHARACTER_SET, CHARSET);// 设置边距hints.put(EncodeHintType.MARGIN, 1);// 生成二维码BitMatrix bitMatrix new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);int width bitMatrix.getWidth();int height bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);}}if (logoPath null || .equals(logoPath)) {return image;}// 在二维码中增加 logoQrCodeUtils.insertImage(image, logoPath, isCompress);return image;}/*** 添加Logo** param source 二维码图片* param logoPath Logo* param isCompress 是否压缩Logo* throws IOException void*/private static void insertImage(BufferedImage source, String logoPath, boolean isCompress) throws IOException {File file new File(logoPath);if (!file.exists()) {return;}Image src ImageIO.read(new File(logoPath));int width src.getWidth(null);int height src.getHeight(null);// 压缩LOGOif (isCompress) {if (width WIDTH) {width WIDTH;}if (height HEIGHT) {height HEIGHT;}Image image src.getScaledInstance(width, height, Image.SCALE_SMOOTH);BufferedImage tag new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g tag.getGraphics();// 绘制缩小后的图g.drawImage(image, 0, 0, null);g.dispose();src image;}// 插入LOGOGraphics2D graph source.createGraphics();int x (QRCODE_SIZE - width) / 2;int y (QRCODE_SIZE - height) / 2;graph.drawImage(src, x, y, width, height, null);Shape shape new RoundRectangle2D.Float(x, y, width, width, 6, 6);graph.setStroke(new BasicStroke(3f));graph.draw(shape);graph.dispose();}/*** 生成带Logo的二维码** param content 二维码内容* param logoPath Logo* param destPath 二维码输出路径* param isCompress 是否压缩Logo* throws Exception void*/public static void create(String content, String logoPath, String destPath, boolean isCompress) throws Exception {BufferedImage image QrCodeUtils.createImage(content, logoPath, isCompress);mkdirs(destPath);ImageIO.write(image, FORMAT_NAME, new File(destPath));}/*** 生成不带Logo的二维码** param content 二维码内容* param destPath 二维码输出路径*/public static void create(String content, String destPath) throws Exception {QrCodeUtils.create(content, null, destPath, false);}/*** 生成带Logo的二维码并输出到指定的输出流** param content 二维码内容* param logoPath Logo* param output 输出流* param isCompress 是否压缩Logo*/public static void create(String content, String logoPath, OutputStream output, boolean isCompress) throws Exception {BufferedImage image QrCodeUtils.createImage(content, logoPath, isCompress);ImageIO.write(image, FORMAT_NAME, output);}/*** 生成不带Logo的二维码并输出到指定的输出流** param content 二维码内容* param output 输出流* throws Exception void*/public static void create(String content, OutputStream output) throws Exception {QrCodeUtils.create(content, null, output, false);}/*** 二维码解析** param file 二维码* return 返回解析得到的二维码内容* throws Exception String*/public static String parse(File file) throws Exception {BufferedImage image;image ImageIO.read(file);if (image null) {return null;}BufferedImageLuminanceSource source new BufferedImageLuminanceSource(image);BinaryBitmap bitmap new BinaryBitmap(new HybridBinarizer(source));Result result;HashtableDecodeHintType, Object hints new HashtableDecodeHintType, Object();hints.put(DecodeHintType.CHARACTER_SET, CHARSET);result new MultiFormatReader().decode(bitmap, hints);return result.getText();}/*** 二维码解析** param path 二维码存储位置* return 返回解析得到的二维码内容* throws Exception String*/public static String parse(String path) throws Exception {return QrCodeUtils.parse(new File(path));}/*** 判断路径是否存在如果不存在则创建** param dir 目录*/public static void mkdirs(String dir) {if (dir ! null !.equals(dir)) {File file new File(dir);if (!file.isDirectory()) {file.mkdirs();}}}
}测试 生成条形码
对应的 controller 代码示例
RestController
RequestMapping(path /barcode)
public class BarCodeController {AutowiredBarCodeUtils barCodeUtils;// http://localhost:8080/barcode/createCode?content987654132barCodeWord123456789GetMapping(path /createCode)public void createQrCode(HttpServletResponse response, RequestParam(content) String content, RequestParam(content) String barCodeWord) {try {// 创建二维码ByteArrayOutputStream byteArrayOutputStream barCodeUtils.barcodeGenerator(content, barCodeWord);// 通过流的方式返回给前端InputStream inputStream new ByteArrayInputStream(byteArrayOutputStream.toByteArray());OutputStream outputStream response.getOutputStream();response.setContentType(image/jpeg);response.setCharacterEncoding(UTF-8);IOUtils.copy(inputStream, outputStream);outputStream.flush();} catch (Exception e) {e.printStackTrace();}}}对应的 工具类 BarCodeUtils
Component
public class BarCodeUtils {/*** 条形码宽度*/private static final int WIDTH 200;/*** 条形码高度*/private static final int HEIGHT 50;/*** 生成条形码并加文字以流的方式返回** param content 内容* param barCodeWord 二维码的文字* return ByteArrayOutputStream*/public ByteArrayOutputStream barcodeGenerator(String content, String barCodeWord) {// 设置条形码参数HashMapEncodeHintType, Object hints new HashMap();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 设置纠错级别为L低hints.put(EncodeHintType.CHARACTER_SET, UTF-8); // 设置字符编码为UTF-8try {// 生成条形码的矩阵BitMatrix matrix new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, WIDTH, HEIGHT, hints);ByteArrayOutputStream outputStream new ByteArrayOutputStream();BufferedImage bufferedImage MatrixToImageWriter.toBufferedImage(matrix);//底部加单号BufferedImage image this.insertWords(bufferedImage, barCodeWord);if (Objects.isNull(image)) {throw new RuntimeException(条形码加文字失败);}ImageIO.write(image, png, outputStream);return outputStream;} catch (WriterException | IOException e) {throw new RuntimeException(条形码生成失败, e);}}private BufferedImage insertWords(BufferedImage image, String words) {// 新的图片把带logo的二维码下面加上文字if (StringUtils.hasLength(words)) {BufferedImage outImage new BufferedImage(WIDTH, HEIGHT 20, BufferedImage.TYPE_INT_RGB);Graphics2D g2d outImage.createGraphics();// 抗锯齿this.setGraphics2D(g2d);// 设置白色this.setColorWhite(g2d);// 画条形码到新的面板g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);// 画文字到新的面板Color color new Color(0, 0, 0);g2d.setColor(color);// 字体、字型、字号g2d.setFont(new Font(微软雅黑, Font.PLAIN, 16));//文字长度int strWidth g2d.getFontMetrics().stringWidth(words);//总长度减去文字长度的一半 居中显示int wordStartX (WIDTH - strWidth) / 2;//height (outImage.getHeight() - height) / 2 12int wordStartY HEIGHT 20;// time 文字长度// 画文字g2d.drawString(words, wordStartX, wordStartY);g2d.dispose();outImage.flush();return outImage;}return null;}/*** 设置 Graphics2D 属性 抗锯齿** param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制*/private void setGraphics2D(Graphics2D g2d) {// 消除画图锯齿g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// 消除文字锯齿g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);Stroke s new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);g2d.setStroke(s);}private void setColorWhite(Graphics2D g2d) {g2d.setColor(Color.WHITE);//填充整个屏幕g2d.fillRect(0, 0, WIDTH, HEIGHT 20);//设置笔刷g2d.setColor(Color.BLACK);}
}测试
文章转载自: http://www.morning.lrwsk.cn.gov.cn.lrwsk.cn http://www.morning.wdskl.cn.gov.cn.wdskl.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.hrkth.cn.gov.cn.hrkth.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.kdnrc.cn.gov.cn.kdnrc.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.kpgft.cn.gov.cn.kpgft.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.kyflr.cn.gov.cn.kyflr.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.ykrss.cn.gov.cn.ykrss.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.bmpjp.cn.gov.cn.bmpjp.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.nlkm.cn.gov.cn.nlkm.cn http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.bmhc.cn.gov.cn.bmhc.cn http://www.morning.kzcz.cn.gov.cn.kzcz.cn http://www.morning.qfcnp.cn.gov.cn.qfcnp.cn http://www.morning.cwgn.cn.gov.cn.cwgn.cn http://www.morning.ygxf.cn.gov.cn.ygxf.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.ssjtr.cn.gov.cn.ssjtr.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.dpbdq.cn.gov.cn.dpbdq.cn http://www.morning.cqrenli.com.gov.cn.cqrenli.com http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn http://www.morning.lxwjx.cn.gov.cn.lxwjx.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.fpqsd.cn.gov.cn.fpqsd.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.wztnh.cn.gov.cn.wztnh.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.trffl.cn.gov.cn.trffl.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.gtqx.cn.gov.cn.gtqx.cn http://www.morning.hpkgm.cn.gov.cn.hpkgm.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.rnmyw.cn.gov.cn.rnmyw.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.yfwygl.cn.gov.cn.yfwygl.cn http://www.morning.rzdpd.cn.gov.cn.rzdpd.cn http://www.morning.hqgxz.cn.gov.cn.hqgxz.cn http://www.morning.mlwpr.cn.gov.cn.mlwpr.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.rykmz.cn.gov.cn.rykmz.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.lsfrc.cn.gov.cn.lsfrc.cn