1、目標html
輸入網址,生成網址的二維碼java
2、概況jquery
1.效果:UI醜,但功能實現了android
2.項目目錄web
3、用到的第三方資源瀏覽器
1.google的掃碼包zxingtomcat
2.JQueryapp
4、步驟(用myEclipse)jsp
1.新建工程,選擇web projectui
2.加入第三方資源(zxing包和jQuery)
2.1
2.1.1複製,粘貼,加入zxing的jar包(全部文件在附件中下載)
2.2.2右鍵jar包加入buildpath
2.2加入jQuery文件
2.2.1在WebRoot下新建文件夾js
複製粘貼jQuery文件到此文件夾
3.按需求修改webroot下的index.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> <base href="<%=basePath%>"> <title>二維碼</title> <script src="js/jquery.min.js"></script> <script> $(function(){ $("input[type=submit]").click(function(e){ e.preventDefault(); $("#qrcode_div").empty().append("<img id='qrImg' />"); $("#qrImg").attr("src", "qrcode?website="+$("input[type=text][name=website]").val());//此處會訪問servlet }); }); </script> </head> <body> <form> 網址<input type="text" name="website" /> <input type="submit" value="生成二維碼" /> <div id="qrcode_div"></div> </form> </body> </html>
4.新建servlet文件
4.1右鍵工程文件夾--》new ---->servlet(myEclipse會自動配置servlet到web.xml文件)
QRCode.java servlet文件的代碼以下:
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import util.QRCodeUtil; import com.google.zxing.WriterException; public class QRCode extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String website = request.getParameter("website"); try { QRCodeUtil.genGR(website, response.getOutputStream()); } catch (WriterException e) { e.printStackTrace(); } //String website = request.getAttribute("website").toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
5.新建輔助類文件,做用是生成二維碼(QRCodeUtil.java)
5.1右鍵工程文件夾--》new--->class,以下圖
QRCodeUtil.java的代碼以下:
package util2; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.Hashtable; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; public class QRCodeUtil { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; public static void genGR(String website, OutputStream output) throws WriterException, IOException { int width = 300; int height = 300; String format = "jpg"; Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); //Hashtable hints = new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bm = new MultiFormatWriter().encode(website, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = toImage(bm); ImageIO.write(image, format, output); //把二維碼寫到response的輸出流 } private static BufferedImage toImage(BitMatrix bm) { int width = bm.getWidth(); int height = bm.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, bm.get(x, y) ? BLACK : WHITE); } } return image; } }
6.至此,全部文件已完成,接下來佈置項目,啓動tomcat,訪問文件,以下圖
佈置完成後,點擊debug啓動項目
啓動成功後,在瀏覽器中訪問文件
PS:
能夠修改QRCodeUtil.java文件中的顏色配置,以生成彩色的二維碼,例如:
private static final int BLACK = 0xff0000ff;//0xFF000000;
private static final int WHITE = 0xffffff00;//0xFFFFFFFF;
常見顏色代碼以下:
這些顏色常數是定義在android.graphics.Color裏的:
類型 |
常數 |
值 |
色碼 |
int |
BLACK |
-16777216 |
0xff000000 |
int |
BLUE |
-16776961 |
0xff0000ff |
int |
CYAN |
-16711681 |
0xff00ffff |
int |
DKGRAY |
-12303292 |
0xff444444 |
int |
GRAY |
-7829368 |
0xff888888 |
int |
GREEN |
-16711936 |
0xff00ff00 |
int |
LTGRAY |
-3355444 |
0xffcccccc |
int |
MAGENTA |
-65281 |
0xffff00ff |
int |
RED |
-65536 |
0xffff0000 |
int |
TRANSPARENT |
0 |
0x00000000 |
int |
WHITE |
-1 |
0xffffffff |
int |
YELLOW |
-256 |
0xffffff00 |
項目附件:
http://files.cnblogs.com/files/shamgod/QRCode.zip