java-QR code generation page Output

Source: Internet
Author: User

QR codes are very popular now.

The front end also has two-dimensional code generation way: Jquery.qrcode

QRCode is actually achieved by using jquery to achieve graphical rendering, supporting HTML5 technology, as long as it is implemented by canvas. Transmission Door

Java generated two-dimensional code is very mature, and compatibility is very good.

Directly download the jar package transmission door

There are Core-3.0.0.jar and Base64encoder.jar two jar packages in the download file.

The first one is the QR code, the second one is used by Base64.

Do not convert base64 can not use a second

Import into the project to use it.

Basic Use Method:

 Publicstring generalqrcode (string url) {Hashtable hints=NewHashtable (); Hints.put (Encodehinttype.character_set,"Utf-8"); String binary=NULL; Try {
400,400 is a two-dimensional code picture width high Bitmatrix bitmatrix
=NewMultiformatwriter (). Encode (URL, Barcodeformat.qr_code,400, 400, hints); //implementation of a: output image to the specified directory//file OutputFile = new file ("D://1.jpg ");//Matrixtoimagewriter.writetofile (Bitmatrix, "PNG", outputFile); //implementation of two: Generate two-dimensional code image and convert the picture to a binary system to pass to the foreground//1. read file conversion to byte arrayBytearrayoutputstream out =NewBytearrayoutputstream (); BufferedImage Image=tobufferedimage (Bitmatrix); ////Imageio.write (Image, "PNG", out); byte[] bytes =Out.tobytearray (); //2. Convert byte array to binaryBase64encoder encoder =NewBase64encoder (); Binary=encoder.encodebuffer (bytes). Trim (); } Catch(Exception e) {e.printstacktrace (); } return"Data:image/png;base64," +binary; } Public Staticbufferedimage tobufferedimage (Bitmatrix matrix) {intwidth =matrix.getwidth (); intHeight =matrix.getheight (); BufferedImage Image=Newbufferedimage (width, height, bufferedimage.type_int_rgb); for(intx = 0; x < width; X + +) { for(inty = 0; Y < height; y++) {Image.setrgb (x, Y, Matrix.get (x, y)? 0xff000000:0xffffffff); } } returnimage; }

After the guide package is copied directly into the project can be used.

If it is the SPRINGMVC framework, the Generalqrcode method can be directly released to the controller to be called at the front end.

As an example of annotations:

@RequestMapping ("/generalqrcode.do")

@ResponseBody

java-QR code generation page Output

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.