二維碼servlet訪問使用更方便

二維碼通過servlet方式來訪問生成,無需保存,即可展現。優點多多。。。。。。



今天就來把玩一把,各位觀衆請坐好觀看:

package com.cctv.website.control.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.cctv.utils.ParamUtil;

/**
 * @description: 二維碼、條形碼工具類 zxing 2.2<br>
 * @author:jiaojun
 * @email:[email protected]
 * @project:junjiao util
 * @version:v0.2
 * @date:2013-9-11
 */
public class QRCodeServlet extends HttpServlet {
    /**
     * @description: 二維碼編碼 <br>
     * @author:jiaojun
     * @param contents
     *            欲編碼內容,必填
     * @param width
     *            生成二維碼圖片寬度,默認200
     * @param height
     *            生成二維碼圖片高度,默認200
     *  example:http://www.cctv.com/QRCode?width=160&height=120&content=http://m.cctv.com/info.html?info_id=734561
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", 1570694081590329344L);
	response.setContentType("image/png");
	response.setCharacterEncoding("GBK");
	ServletOutputStream out = response.getOutputStream();
	String contents = ParamUtil.getParameter(request, "content");
	if("".equals(contents)){
	    out.print("QRCodeServlet parameter content is empty!!");
	    out.flush();
	    out.close();
	    throw new RuntimeException("QRCodeServlet parameter content is empty!!");
	}
	int width = ParamUtil.getIntParameter(request, "width", 200);
	int height = ParamUtil.getIntParameter(request, "height", 200);

	Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
	/**
	 * 容錯率共四檔:
		L     7%
		M    15%
		Q     25%
		H     30%
	 */
	hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
	/**
	 * 指定編碼格式:特別聲明此處的編碼使用小寫如utf-8 gbk
	 * 使用“UTF-8”,會在文本編碼前添加一段ECI(擴充解釋Extended Channel Interpretation)編碼,就是這段編碼導致手機不能解析。
	 * 如果使用小寫"utf-8"會使這個ECI判斷失效而不影響內容編碼方式。
	 * 至於詳細的ECI解釋,可以看《QRCode 編碼解碼標準》
	 */
	hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	// 指定空白邊框大小
	hints.put(EncodeHintType.MARGIN, 1); 
	try {
	    BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
		    BarcodeFormat.QR_CODE, width, height, hints);
	    MatrixToImageWriter.writeToStream(bitMatrix, "png", out);
	} catch (Exception e) {
	    e.printStackTrace();
	}finally{
	    out.flush();
	    out.close();
	}
    }
}


web.xml

	<servlet>
		<servlet-name>QRCodeServlet</servlet-name>
		<servlet-class>com.cctv.website.control.servlet.QRCodeServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>QRCodeServlet</servlet-name>
		<url-pattern>/QRCode</url-pattern>
	</servlet-mapping>


那我們就可以通過http來訪問了:http://www.cctv.com/QRCode?width=160&height=120&content=http://m.cctv.com/info.html?info_id=734561


是不是很帥呀:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章