Java生成2维码源码
2015-09-17 15:58
323 查看
参考http://blog.csdn.net/lmb55/article/details/46039445此博客
@WebServlet(“/PrintTwoBarCode”)//对servlet加注解,可以不用在xml配置servlet
public class PrintTwoBarCode extends HttpServlet {
private static final long serialVersionUID = 1L;
}
jsp文件如下
@WebServlet(“/PrintTwoBarCode”)//对servlet加注解,可以不用在xml配置servlet
public class PrintTwoBarCode extends HttpServlet {
private static final long serialVersionUID = 1L;
[code]/** * @see HttpServlet#HttpServlet() */ public PrintTwoBarCode() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub 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); //通过calQrcode函数将byte数组转换成boolean数组 ,然后依据编码后的boolean数组绘图 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());//把二维码图片写入输出流中 }
}
jsp文件如下
[code]<body> <div class="box"> <h2>扫一扫开启爱的密码</h2> <img src="PrintTwoBarCode?code=http://blog.csdn.net/lmb55"> </div> </body>
相关文章推荐
- [Android 硬件] Eclipse错误:Conversion to Dalvik format failed with error 1
- Spring AOP Example – Advice
- struts2文件上传
- struts2文件上传
- Maven+Spring framework生成的可执行jar文件执行异常
- Spring-IOC容器的配置
- java常见的几种内存溢出和解决方案
- Java中char和String 的深入理解 - 字符编码
- test3.14
- Java处理时区的注意事项
- spring-jdbc直接获得POJO对象
- 使用jdb和gdb同时调试java native code
- java安全编程
- java.math.BigDecimal类的用法 解决double计算精度问题
- spring方法注入
- 浅谈java枚举(enum)
- java学习2-循环中的嵌套语句2
- Java native HelloWorld
- java int short long float double精度最大值整理
- struts开发中文乱码问题总结