-
參考資料
-
項目環(huán)境
- jdk1.8(**zxing 生成二維碼圖片文件需要 jdk1.7 及以上版本 **)
- zxing-javase
<dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency>
- 工具類代碼
import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; /** * 二維碼工具類 */ public class QRCodeUtil { private static final int width = 300;// 默認二維碼寬度 private static final int height = 300;// 默認二維碼高度 private static final String format = "png";// 默認二維碼文件格式 private static final Map<EncodeHintType, Object> hints = new HashMap();// 二維碼參數(shù) static { hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 字符編碼 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 容錯等級 L拙绊、M秕岛、Q、H 其中 L 為最低, H 為最高 hints.put(EncodeHintType.MARGIN, 2);// 二維碼與圖片邊距 } /** * 返回一個 BufferedImage 對象 * @param content 二維碼內(nèi)容 * @param width 寬 * @param height 高 */ public static BufferedImage toBufferedImage(String content, int width, int height) throws WriterException, IOException { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); return MatrixToImageWriter.toBufferedImage(bitMatrix); } /** * 將二維碼圖片輸出到一個流中 * @param content 二維碼內(nèi)容 * @param stream 輸出流 * @param width 寬 * @param height 高 */ public static void writeToStream(String content, OutputStream stream, int width, int height) throws WriterException, IOException { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); MatrixToImageWriter.writeToStream(bitMatrix, format, stream); } /** * 生成二維碼圖片文件 * @param content 二維碼內(nèi)容 * @param path 文件保存路徑 * @param width 寬 * @param height 高 */ public static void createQRCode(String content, String path, int width, int height) throws WriterException, IOException { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); //toPath() 方法由 jdk1.7 及以上提供 MatrixToImageWriter.writeToPath(bitMatrix, format, new File(path).toPath()); } }
- 使用 SpringMVC 動態(tài)生成二維碼
@RequestMapping(value = "/qrcode") public void qrcode(String content, @RequestParam(defaultValue = "300", required = false) int width,@RequestParam(defaultValue = "300", required = false) int height, HttpServletResponse response) { ServletOutputStream outputStream = null; try { outputStream = response.getOutputStream(); QRCodeUtil.writeToStream(content, outputStream, width, height); } catch (Exception e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
- QRCodeUtils.java 下載(右鍵另存為)
- JavaScript 生成二維碼可參考 生成二維碼之 JavaScript(jquery-qrcode) 篇
- 如果想生成帶 LOGO 的二維碼可參考 生成帶 LOGO 的二維碼
生成二維碼之 Java (Google zxing) 篇
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門斥黑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人眉厨,你說我怎么就攤上這事锌奴。” “怎么了憾股?”我有些...
- 文/不壞的土叔 我叫張陵鹿蜀,是天一觀的道長。 經(jīng)常有香客問我服球,道長茴恰,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任斩熊,我火速辦了婚禮往枣,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己分冈,他們只是感情好圾另,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著丈秩,像睡著了一般盯捌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蘑秽,一...
- 文/蒼蘭香墨 我猛地睜開眼梢睛,長吁一口氣:“原來是場噩夢啊……” “哼肥印!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起绝葡,我...
- 正文 年R本政府宣布,位于F島的核電站藕届,受9級特大地震影響挪蹭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜休偶,卻給世界環(huán)境...
- 文/蒙蒙 一梁厉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦词顾、人聲如沸八秃。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽昔驱。三九已至,卻和暖如春上忍,著一層夾襖步出監(jiān)牢的瞬間骤肛,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 1絮蒿、二維碼的生成 http://www.cnblogs.com/jtmjx/archive/2012/06/18/...