Notes-Google zxing two-dimensional code, output to the page with Data flow display

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.