?????????????
????QR_Code_Java.zip (339 KB)
?????????????JPEG????????????iPhone??Android??????????????????????????“Hello World” :)
???????????QRGen??API???????????????????????????????API??????QR??
// 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();
??????Java??????????????URL????QR??
????QR ??????????????????????????????????????????????????QR?????????URL??????????????????????????????裬????????????д???URL????????????QR???С?????????Hello World????У?????“Hello World”?????????滻?????????URL?????????????Σ?
????ByteArrayOutputStream out = QRCode.from("http://viralpatel.net").to(ImageType.PNG).stream();
????Servlet?е?QR??
?????????????????????????????????ЩQR?????????????Java??????QR?????????????????????????????????QR?????Java Servlet?С?
?????????????QRGen??ZXing????????QR??????????HTTP Servlet???QR???????????????????
??????index.jsp??????????????HTML?????????????????????????????????????????????????????????????
????index.jsp
????<form action="qrservlet" method="get">
????<p>Enter Text to create QR Code</p>
????<input name="qrtext" type="text">
????<input value="Generate QR Code" type="submit">
????</form>
??????????QRCodeServlet.java???????????????QRGen??ZXing??request.getParameter????????????QR?????????QR??????????д??response?в??????????content-type??
????QRCodeServlet.java
package net.viralpatel.qrcodes;
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??/qrservlet???????QRCodeServlet.java??
????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>
?????????????
????QR_Code_Servlet.zip (340 KB)
???????

???????
??????Java??????QR???????????????????????????????????κ????е?Java??????м????С??????????????У??????????????Java??Servlet??????QR??