javaweb 动态成二维码

来源:互联网 发布:linux设置网络连接 编辑:程序博客网 时间:2024/05/08 06:52


import java.io.IOException;
import java.util.Hashtable;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;


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 QrcodeAction extends action{
private String userip;
private String usermac;
private String mkcode;
public String get(){
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        String text = "userip="+userip+"&usermac="+usermac+"&mkcode="+mkcode; 
        int width = 200; 
        int height = 200; 
        String format = "gif"; 
        Hashtable hints = new Hashtable(); 
        //内容所使用编码 
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
        hints.put(EncodeHintType.MARGIN,1);
        try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, 
       BarcodeFormat.QR_CODE, width, height, hints);
ServletOutputStream out = response.getOutputStream();

MatrixToImageWriter.writeToStream(bitMatrix, format,out);
out.flush();
out.close();
return null;
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

return null;
}
public String getUserip() {
return userip;
}
public void setUserip(String userip) {
this.userip = userip;
}
public String getUsermac() {
return usermac;
}
public void setUsermac(String usermac) {
this.usermac = usermac;
}
public String getMkcode() {
return mkcode;
}
public void setMkcode(String mkcode) {
this.mkcode = mkcode;
}

}



zxing 的jar http://download.csdn.net/detail/huidanyige/8603109 到这里面去找吧吐舌头

0 0
原创粉丝点击