Java如何生成二維碼


現在二維碼已經到處普及,一般項目在個人中心這一塊都會被要求放一個二維碼。現在分享我在項目中一個小案例

首先我們需要在maven中引入谷歌的zxing包的依賴

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

 

控制層

 @RequestMapping("/getReaderQRCode") public void getReaderQRCode(HttpServletResponse response) { ReaderEntity reader = ReaderSessionHelper.getInstance().getReader(); if (reader != null) { String cardno = reader.getCardno(); //獲取session中的reader信息來作為二維碼保存信息 Map<String, String> jsonMap = new HashMap<>(); jsonMap.put("cardno", cardno); jsonMap.put("token", String.valueOf(System.currentTimeMillis())); String cardData = JSONObject.toJSONString(jsonMap); /* String qrval = "QRC:" + new DESPlus().encrypt(cardData);*/ String qrval = new DESPlus().encrypt(cardData); //這里使用了desplus來進行加密 try (OutputStream outputStream = response.getOutputStream()) { ZxingUtil.encode2DCode(outputStream, qrval); //調用zxing的方法,來生成二維碼。后面會貼出該方法代碼 } catch (IOException | WriterException e) { logger.warn("生成讀者二維碼失敗", e); response.setStatus(HttpServletResponse.SC_NOT_FOUND); } } else { response.setStatus(HttpServletResponse.SC_FORBIDDEN); } }
//二維碼的工具類
public
class ZxingUtil { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private static final int DEFAULT_HEIGHT = 300;// 二維碼圖片寬度 private static final int DEFAULT_WIDTH = 300;// 二維碼圖片高度 private static final String DEFAULT_FORMAT = "gif";// 二維碼的圖片格式 private static final int BARCODE_HEIGHT = 30; private static final int BARCODE_WIDTH = 150; private static final String BARCODE_FORMAT = "png"; /** * 生成二維碼 到指定 路徑 、這個方法二維碼會返回到你請求的位置,通過response響應,上面的控制層方法調用的是該方法,如果不用返回到請求的位置,那么就使用下面的方法,可以輸出到你想要到的地方 * * @param outputStream * 到指定 路徑 、FILE * @param text * 可以是網址 和 其他,網址就是跳轉 * @throws WriterException * @throws IOException */ public static void encode2DCode(OutputStream outputStream, String text) throws WriterException, IOException { Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 內容所使用字符集編碼 hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, DEFAULT_WIDTH, DEFAULT_HEIGHT, hints); BufferedImage image = toBufferedImage(bitMatrix); /* * Graphics2D gs = image.createGraphics(); //載入logo Image img = * ImageIO.read(new File(srcImagePath)); gs.drawImage(img, 125, 125, * null); gs.dispose(); img.flush(); */ if (!ImageIO.write(image, DEFAULT_FORMAT, outputStream)) { throw new IOException("Could not write an image of format " + DEFAULT_FORMAT + " to stream"); } } }

/**
* 生成二維碼 到指定 路徑 、二維碼會輸出到某一個位置
*
* @param outputFile
* 到指定 路徑 、FILE
* @param text
* 可以是網址 和 其他,網址就是跳轉
* @throws WriterException
* @throws IOException
*/
public static void encode2DCode(File outputFile, String text)
throws WriterException, IOException {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 內容所使用字符集編碼
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, DEFAULT_WIDTH, DEFAULT_HEIGHT, hints);
writeToFile(bitMatrix, DEFAULT_FORMAT, outputFile);
}
 

desplus加密方法網上很多,我就不貼出來。

好了,我已經最近遇到的事記錄下來。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM