1、下載生成二維碼所需要的jar包qrcode.jar;2、直接上生成二維碼的java代碼 //需要導(dǎo)入的包import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import javax.imageio.ImageIO;import com.swetake.util.Qrcode; /** * 生成二維碼(QRCode)圖片 * @param content 二維碼圖片的內(nèi)容 * @param imgPath 生成二維碼圖片完整的路徑 * @param ccbpath 二維碼圖片中間的logo路徑 */ public static int createQRCode(String content, String imgPath,String ccbPath) { try { Qrcode qrcodeHandler = new Qrcode(); qrcodeHandler.setQrcodeErrorCorrect('M'); qrcodeHandler.setQrcodeEncodeMode('B'); qrcodeHandler.setQrcodeVersion(7); // System.out.println(content); byte[] contentBytes = content.getBytes("gb2312"); //構(gòu)造一個(gè)BufferedImage對(duì)象 設(shè)置寬、高 BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufImg.createGraphics(); gs.setBackground(Color.WHITE); gs.clearRect(0, 0, 140, 140); // 設(shè)定圖像顏色 BLACK gs.setColor(Color.BLACK); // 設(shè)置偏移量 不設(shè)置可能導(dǎo)致解析出錯(cuò) int pixoff = 2; // 輸出內(nèi)容 二維碼 if (contentBytes.length 0 contentBytes.length 120) { boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes); for (int i = 0; i codeOut.length; i++) { for (int j = 0; j codeOut.length; j++) { if (codeOut[j][i]) { gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); } } } } else { System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. "); return -1; } Image img = ImageIO.read(new File(ccbPath));//實(shí)例化一個(gè)Image對(duì)象。 gs.drawImage(img, 55, 55, 30, 30, null); gs.dispose(); bufImg.flush(); // 生成二維碼QRCode圖片 File imgFile = new File(imgPath); ImageIO.write(bufImg, "png", imgFile); }catch (Exception e){ e.printStackTrace(); return -100; } return 0; }
網(wǎng)站的建設(shè)成都創(chuàng)新互聯(lián)公司專注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營(yíng)網(wǎng)站定制開(kāi)發(fā).小程序定制開(kāi)發(fā),H5頁(yè)面制作!給你煥然一新的設(shè)計(jì)體驗(yàn)!已為效果圖設(shè)計(jì)等企業(yè)提供專業(yè)服務(wù)。
來(lái)自網(wǎng)友 孤獨(dú)青鳥(niǎo)的博客
這個(gè)代碼可能比較多。。看微信公眾平臺(tái)的api,里面寫(xiě)的很清楚,其實(shí)就是一個(gè)http請(qǐng)求,加上指定的報(bào)文就可以了,用java寫(xiě)還比較簡(jiǎn)單的。。
java公眾號(hào)不需要特殊的架構(gòu) ,
從最原始的servlet到流行的ssh ssm框架都可以做 ,
后端通過(guò)網(wǎng)絡(luò)請(qǐng)求微信的接口,
從而獲得請(qǐng)求的數(shù)據(jù) ,
前端可以使用各種前端框架實(shí)現(xiàn),
比如easyui或者bootstrap都可以,
微信開(kāi)發(fā)文檔中有詳細(xì)的示例 。
1.首先我們新建一個(gè)Java開(kāi)發(fā)包WeiXinSDK
2.包路徑:com.ansitech.weixin.sdk
測(cè)試的前提條件:
假如我的公眾賬號(hào)微信號(hào)為:vzhanqun
我的服務(wù)器地址為:
下面我們需要新建一個(gè)URL的請(qǐng)求地址
我們新建一個(gè)Servlet來(lái)驗(yàn)證URL的真實(shí)性,具體接口參考
接入指南
3.新建com.ansitech.weixin.sdk.WeixinUrlFilter.java
這里我們主要是獲取微信服務(wù)器法師的驗(yàn)證信息,具體驗(yàn)證代碼如下
[java] view plain copy print?
package com.ansitech.weixin.sdk;
import com.ansitech.weixin.sdk.util.SHA1;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WeixinUrlFilter implements Filter {
//這個(gè)Token是給微信開(kāi)發(fā)者接入時(shí)填的
//可以是任意英文字母或數(shù)字,長(zhǎng)度為3-32字符
private static String Token = "vzhanqun1234567890";
@Override
public void init(FilterConfig config) throws ServletException {
System.out.println("WeixinUrlFilter啟動(dòng)成功!");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//微信服務(wù)器將發(fā)送GET請(qǐng)求到填寫(xiě)的URL上,這里需要判定是否為GET請(qǐng)求
boolean isGet = request.getMethod().toLowerCase().equals("get");
System.out.println("獲得微信請(qǐng)求:" + request.getMethod() + " 方式");
if (isGet) {
//驗(yàn)證URL真實(shí)性
String signature = request.getParameter("signature");// 微信加密簽名
String timestamp = request.getParameter("timestamp");// 時(shí)間戳
String nonce = request.getParameter("nonce");// 隨機(jī)數(shù)
String echostr = request.getParameter("echostr");//隨機(jī)字符串
ListString params = new ArrayListString();
params.add(Token);
params.add(timestamp);
params.add(nonce);
//1. 將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序
Collections.sort(params, new ComparatorString() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
//2. 將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密
String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2));
if (temp.equals(signature)) {
response.getWriter().write(echostr);
}
} else {
//處理接收消息
}
}
@Override
public void destroy() {
}
}
好了,不過(guò)這里有個(gè)SHA1算法,我這里也把SHA1算法的源碼給貼出來(lái)吧!
4.新建com.ansitech.weixin.sdk.util.SHA1.java
[java] view plain copy print?
/*
* 微信公眾平臺(tái)(JAVA) SDK
*
* Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ansitech.weixin.sdk.util;
import java.security.MessageDigest;
/**
* pTitle: SHA1算法/p
*
* @author qsyangyangqisheng274@163.com
*/
public final class SHA1 {
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* Takes the raw bytes from the digest and formats them correct.
*
* @param bytes the raw bytes from the digest.
* @return the formatted bytes.
*/
private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
// 把密文轉(zhuǎn)換成十六進(jìn)制的字符串形式
for (int j = 0; j len; j++) {
buf.append(HEX_DIGITS[(bytes[j] 4) 0x0f]);
buf.append(HEX_DIGITS[bytes[j] 0x0f]);
}
return buf.toString();
}
public static String encode(String str) {
if (str == null) {
return null;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
messageDigest.update(str.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
5.把這個(gè)Servlet配置到web.xml中
[html] view plain copy print?
filter
description微信消息接入接口/description
filter-nameWeixinUrlFilter/filter-name
filter-classcom.ansitech.weixin.sdk.WeixinUrlFilter/filter-class
/filter
filter-mapping
filter-nameWeixinUrlFilter/filter-name
url-pattern/api/vzhanqun/url-pattern
/filter-mapping
好了,接入的開(kāi)發(fā)代碼已經(jīng)完成。
6.下面就把地址URL和密鑰Token填入到微信申請(qǐng)成為開(kāi)發(fā)者模式中吧。