java QRCode生成示例

 

原文链接:http://www.javaarch.net/jiagoushi/708.htm

 

java QRCode生成示例

我们可以使用QRGen这个比较不错的包来生成QR code。

maven依赖:

	<dependencies>
		<dependency>
			<groupId>net.glxn</groupId>
			<artifactId>qrgen</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>
	
示例代码:


	import java.io.ByteArrayOutputStream;
	import java.io.File;
	import java.io.FileNotFoundException;
	import java.io.FileOutputStream;
	import java.io.IOException;

	import net.glxn.qrgen.QRCode;
	import net.glxn.qrgen.image.ImageType;

	public class QRCodeUtil {

		public static void main(String[] args) {
			ByteArrayOutputStream out = QRCode.from("Hello World")
					.to(ImageType.PNG).stream();

			try {
				FileOutputStream fout = new FileOutputStream(new File(
						"E:\\logs\\QR_Code.JPG"));

				fout.write(out.toByteArray());

				fout.flush();
				fout.close();

			} catch (FileNotFoundException e) {
				// Do Logging
			} catch (IOException e) {
				// Do Logging
			}
			
			// get QR file from text using defaults
			 File file = QRCode.from("Hello World").file();
			 // get QR stream from text using defaults
			 ByteArrayOutputStream stream = QRCode.from("Hello World").stream();
			   
			 // override the image type to be JPG
			 QRCode.from("Hello World").to(ImageType.JPG).file();
			 QRCode.from("Hello World").to(ImageType.JPG).stream();
			   
			 // override image size to be 250x250
			 QRCode.from("Hello World").withSize(250, 250).file();
			 QRCode.from("Hello World").withSize(250, 250).stream();
			   
			 // override size and image type
			 QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).file();
			 QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).stream();
			 
	//		 Website Link (URLs) QR Code in Java
			 ByteArrayOutputStream out1 = QRCode.from("http://viralpatel.net")
						.to(ImageType.PNG).stream();
		}

	}
	
j2ee中的使用:

 index.jsp
 
	<html>
	<head>
	<title>QR Code in Java Servlet - viralpatel.net</title>
	</head>
	<body>
		 
		<form action="qrservlet" method="get">
			<p>Enter Text to create QR Code</p>
			<input type="text" name="qrtext" />
			<input type="submit" value="Generate QR Code" />
		</form>
	</body>
	</html>
	
QRCodeServlet.java

	import java.io.ByteArrayOutputStream;
	import java.io.File;
	import java.io.FileNotFoundException;
	import java.io.FileOutputStream;
	import java.io.IOException;
	import java.io.OutputStream;
	 
	import javax.servlet.ServletException;
	import javax.servlet.http.HttpServlet;
	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;
	 
	import net.glxn.qrgen.QRCode;
	import net.glxn.qrgen.image.ImageType;
	 
	public class QRCodeServlet extends HttpServlet {
		@Override
		protected void doGet(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
	 
			String qrtext = request.getParameter("qrtext");
	 
			ByteArrayOutputStream out = QRCode.from(qrtext).to(
					ImageType.PNG).stream();
			 
			response.setContentType("image/png");
			response.setContentLength(out.size());
			 
			OutputStream outStream = response.getOutputStream();
	 
			outStream.write(out.toByteArray());
	 
			outStream.flush();
			outStream.close();
		}
	}
	
web.xml

	<?xml version="1.0" encoding="UTF-8"?>
	<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xmlns="http://java.sun.com/xml/ns/javaee"
			xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
			xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
			id="WebApp_ID" version="2.5">
			 
	  <display-name>QR_Code_Servlet</display-name>
	  <welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	  </welcome-file-list>
	 
		<servlet>
			<servlet-name>QRCodeServlet</servlet-name>
			<servlet-class>net.viralpatel.qrcodes.QRCodeServlet</servlet-class>
		</servlet>
		<servlet-mapping>
			<servlet-name>QRCodeServlet</servlet-name>
			<url-pattern>/qrservlet</url-pattern>
		</servlet-mapping>
		 
	</web-app>
	
可以查看:http://www.javaarch.net/jiagoushi/qrcode.htm

 

posted @ 2013-06-03 10:15  zhwj184  阅读(666)  评论(0编辑  收藏  举报