日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java开发微信二维码

發布時間:2024/4/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java开发微信二维码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

要用到一個jar包QRcode.jar
主要用到了com.swetake.util.Qrcode這個類。API鏈接如下:
http://www.swetake.com/qrcode/java/docs/index.html

以本人博客首頁為例:

代碼如下:(生成一個固定內容的二維碼以圖片形式輸出到文件中)
CreateTwoBarImage.java(一個普通的java類)

package com.lmb;import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import com.swetake.util.Qrcode;public class CreateTwoBarImage {public void creatTxm(String param) throws Exception {Qrcode qrcode = new Qrcode();qrcode.setQrcodeErrorCorrect('M');qrcode.setQrcodeEncodeMode('B');qrcode.setQrcodeVersion(7);byte[] bstr = param.getBytes("UTF-8");//返回用指定名字命名的字節數組值BufferedImage bi = new BufferedImage(139, 139,BufferedImage.TYPE_INT_RGB);//實例化指定參數的BufferedImageGraphics2D g = bi.createGraphics();//返回一個呈現指定 BufferedImage 的 Graphics2D 對象g.setBackground(Color.WHITE); // 設置該Graphics2D 對象的背景顏色g.clearRect(0, 0, 139, 139); //擦除指定的矩形,并且用一個透明的顏色填充它g.setColor(Color.BLACK); // 條碼顏色if (bstr.length > 0 && bstr.length < 123) {boolean[][] b = qrcode.calQrcode(bstr); //通過calQrcode函數將byte數組轉換成boolean數組 ,然后依據編碼后的boolean數組繪圖 for (int i = 0; i < b.length; i++) {for (int j = 0; j < b.length; j++) {if (b[j][i]) {g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3); //填充指定的矩形}}}}g.dispose(); //處理圖形上下文,并釋放系統資源bi.flush();//將生成的BufferedImage序列化到磁盤String FilePath = "F:/私人物品劉夢冰/學習資料/exercise/" + param + ".jpg";//生成的二維碼要存放的文件路徑File f = new File(FilePath);ImageIO.write(bi, "jpg", f);//將生成的二維碼以圖片的形式寫入相應的文件}public static void main(String args[]) {try {new CreateTwoBarImage().creatTxm("lmb");} catch (Exception e) {e.printStackTrace();}} }

運行結果:

掃碼之后顯示的就是一個字符串“lmb”,如果我們想通過掃碼訪問一個網址,該怎么改進呢?

代碼如下:(生成二維碼顯示到網頁上)
PrintTwoBarCodeServlet、qrcode.jsp

PrintTwoBarCodeServlet.java

package com.lmb;import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter;import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import com.swetake.util.Qrcode;public class PrintTwoBarCodeServlet extends HttpServlet {/*** 專門用來生成二維碼圖片的servlet*/private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {String code = request.getParameter("code");Qrcode testQrcode = new Qrcode();testQrcode.setQrcodeErrorCorrect('M');testQrcode.setQrcodeEncodeMode('B');testQrcode.setQrcodeVersion(7);byte[] d = code.getBytes("gbk");BufferedImage image = new BufferedImage(98, 98,BufferedImage.TYPE_BYTE_BINARY);Graphics2D g = image.createGraphics();g.setBackground(Color.WHITE);g.clearRect(0, 0, 98, 98);g.setColor(Color.BLACK);if (d.length > 0 && d.length < 120) {boolean[][] s = testQrcode.calQrcode(d);for (int i = 0; i < s.length; i++) {for (int j = 0; j < s.length; j++) {if (s[j][i]) {g.fillRect(j * 2 + 3, i * 2 + 3, 2, 2);}}}}g.dispose();image.flush();ImageIO.write(image, "jpg", response.getOutputStream());} }

qrcode.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>顯示頁面</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style type="text/css">.box{width:400px;height:300px;margin:0 auto;border:1px solid #39b2e2;text-align:center;color:red;}</style></head><body><div class="box"><h2>掃一掃開啟愛的密碼</h2><img src="printTwoBarCode.do?code=http://blog.csdn.net/lmb55"></div></body> </html>

生成的二維碼:

掃一掃的顯示:

總結

以上是生活随笔為你收集整理的java开发微信二维码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。