notes, Google zxing two-dimensional code, with data flow output to the page display
Two-dimensional code has been very popular, today practiced a bit, requirements are generated for the different parameters of the address link two-dimensional code, and displayed on the page, if the use of generated pictures to save the local return to the relative path, will produce a heap of garbage picture files, but also to remove the late is really trouble, think of the log on the checkout code can directly output to the page , so on the same, less nonsense directly on the code.
Project is Springmvc,maven management
First import Google's two-dimensional code pom file zxing dependencies
<!--two-dimensional code-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactid >core</artifactId>
<version>3.2.0</version>
</dependency>
< dependency>
<groupId>com.google.zxing</groupId>
<artifactid>javase</artifactid >
<version>3.2.0</version>
</dependency>
The page JSP displays the two-dimensional code place:
Java Controller Code
@RequestMapping (value = "/getq", method = {requestmethod.post, requestmethod.get}) Publ
IC void Getqcode (HttpServletResponse resp, string id) throws IOException {string url = "Address to generate a two-dimensional code" +ID; if (URL!= null &&! "".
Equals (URL)) {Servletoutputstream stream = null; try {int width = 200;//picture width int height = 200;//height stream = resp.getoutp
Utstream ();
Qrcodewriter writer = new Qrcodewriter ();
Bitmatrix m = writer.encode (URL, barcodeformat.qr_code, height, width);
Matrixtoimagewriter.writetostream (M, "PNG", stream);
catch (Writerexception e) {e.printstacktrace ();
finally {if (stream!= null) {Stream.flush ();
Stream.Close (); }
}
}
}
Finally, when the page loads, it executes the SRC request inside the IMG tag to return to the stream and show it.