Java生成二维码(案例源码)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java生成二维码(案例源码)

Java生成二维码(案例源码)

 2014/9/3 21:45:16  明子健  程序员俱乐部  我要评论(0)
  • 摘要:index.jsp代码<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>生成二维码</title><metahttp-equiv="pragma"content="no-cache">
  • 标签:源码 Java 二维码

index.jsp代码

class="html" name="code"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>生成二维码</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<script type="text/javascript">
	function check() {
		var content = document.getElementById("content").value;
		if (content.length == 0 || content.length - 80 > 0) {
			alert("请输入1-80长度内容,当前长度:" + content.length);
			return false;
		}
		document.getElementById("createEwmForm").submit();
		return true;
	}
</script>
<body>
	<div align="center">
		<form method="post" id="createEwmForm"
			action="qrcode_getQRcodeImg.action">
			<textarea rows="5" cols="30" name="content" id="content"></textarea>
			<p>
				<input type="button" onclick="check()" name="ewmCreate"
					id="ewmCreate" value="生成二维码" />
		</form>
		<img alt="二维码" src="img/ewm.png">
	</div>
</body>
</html>

?

struts.xml代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="qrcode" extends="struts-default">
		<action name="qrcode_*" class="com.singlee.action.QRcodeAction"
			method="{1}">
			<result name="ok">/index.jsp
			</result>
			<result name="no">/error.jsp
			</result>
		</action>
	</package>
</struts>    

?

?

CreateEwm.java代码

package com.singlee.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import com.swetake.util.Qrcode;

/**
 * 
 * @author mingzl
 * @function 生成二维码
 * 
 */
public class CreateEwm {

	/**
	 * 生成二维码(QRCode)图片的公共方法
	 * 
	 * @param content
	 *            存储内容
	 * @param size
	 *            二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
	 * @return
	 */
	public BufferedImage qrCodeCommon(String content, int size) {
		BufferedImage bufImg = null;
		try {
			Qrcode qrcodeHandler = new Qrcode();
			// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
			qrcodeHandler.setQrcodeErrorCorrect('M');
			qrcodeHandler.setQrcodeEncodeMode('B');
			// 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
			qrcodeHandler.setQrcodeVersion(size);
			// 获得内容的字节数组,设置编码格式
			byte[] contentBytes = content.getBytes("utf-8");
			// 图片尺寸
			int imgSize = 67 + 12 * (size - 1);
			bufImg = new BufferedImage(imgSize, imgSize,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D gs = bufImg.createGraphics();
			// 设置背景颜色:白色
			gs.setBackground(Color.WHITE);
			gs.clearRect(0, 0, imgSize, imgSize);
			// 设定图像颜色:黑色
			gs.setColor(Color.BLACK);
			// 设置偏移量
			int pixoff = 2;
			// 输出内容:二维码
			if (contentBytes.length > 0 && contentBytes.length < 800) {
				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 {
				throw new Exception("QRCode content bytes length = "
						+ contentBytes.length + " not in [0, 800].");
			}
			gs.dispose();
			bufImg.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bufImg;
	}

}

?

?

QRcodeAction代码

package com.singlee.action;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.opensymphony.xwork2.ActionSupport;
import com.singlee.util.CreateEwm;

public class QRcodeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String content;

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getQRcodeImg() {
		CreateEwm ce = new CreateEwm();
		int size = (content.getBytes().length - 1) / 20;
		if (size >= 40) {
			return "no";
		}
		size = size < 5 ? 5 : size;
		BufferedImage bi = ce.qrCodeCommon(content, size);
		try {
			String upPath = this.getClass().getClassLoader().getResource("")
					.getPath().replace("WEB-INF/classes", "img");
			ImageIO.write(bi, "png", new File(upPath + "/ewm.png"));
		} catch (IOException e) {
			e.printStackTrace();
			return "no";
		}
		return "ok";
	}
}

?

?

Java生成二维码案例下载

?

  • CreateQRCode.zip (4.2 MB)
  • 描述: Java生成二维码项目案例
  • 下载次数: 1
  • QrCode.jar (1 MB)
  • 下载次数: 0
上一篇: java中多态三种实现方式之一 下一篇: 没有下一篇了!
发表评论
用户名: 匿名