Two-dimensional code login

Source: Internet
Author: User

v I. Preface

These days in the study QR Code of the scan code login. There are a lot of things I don't understand. On the internet to see someone wrote some through QRCode or zxing realize the generation and decoding of two-dimensional code. On a whim, I decided to try it myself. I was through the QRCode realization, the following specific to say.

v two or two-D code principle

Basic Knowledge Reference: http://news.cnblogs.com/n/191671/

A very important part of the knowledge: QR Code A total of 40 sizes. The official name is version versions. Version 1 is a matrix of x 21, version 2 is a matrix of x 25, version 3 is the size of 29, each additional Version will increase the size of 4, the formula is: (V-1) + (V is the version number) Max Ver Sion 40, (40-1) *4+21 = 177, so the highest is a square of 177 x 177.

  

v three or two-D code generation and decoding toolv1. The effect is as shown.

Generate QR code (without logo) to generate two-dimensional code (with logo)

The corresponding decoding

The tool is simple but practical. Interface can also be beautified, the function can also be enhanced, beginner's mind just to practice the generation and parsing of QR code.

v2. Core classes for two-dimensional code generation and parsingView Codev3. Specific areas of attention
QR code sizeprivate static final int code_img_size = 235;//LOGO SIZE (in order to insert the integrity of the picture, we choose to insert in the most middle, and the length width is recommended for the entire two-dimensional code of 1/7 to a quarter) private s tatic Final int insert_img_size = CODE_IMG_SIZE/5;

For the two-dimensional code picture size is still not calculated, if someone see here, convenient words can tell the younger brother. The value I have here (235) is by setting the Qrcodeversion (version 15), as well as the offset pixoff=2 and black area of the size=3 when the image is drawn, after the final image is generated, the picture is opened by PS and then the image size information is determined.

There is the middle of the logo not too big, otherwise it will cause QRCode parsing error, but mobile phone scan code will not necessarily be wrong. The sense of mobile phone scanning code analysis than the qrcode of strong analytical ability.

QRCode Qrcodehandler = new QRCode ();  Set the two-dimensional code error rate, optional L (7%), M (15%), Q (25%), H (30%), the higher the error rate can be stored less information, but the two-dimensional code clarity requirements of the smaller  qrcodehandler.setqrcodeerrorcorrect (' M ');  Qrcodehandler.setqrcodeencodemode (' B ');  Set the size of the two-dimensional code, the value range of 1-40, the larger the size of the larger, the more information can be stored  qrcodehandler.setqrcodeversion (15);  

General settings version is good, many online are 7 or 8, I try to make a larger value, 15 words QR code looks very dense.

Set offset, not set may cause parsing error  final int pixoff = 2;final int sz = 3;//output content > QR Code  if (Contentbytes.length > 0 &&am P  Contentbytes.length <) {  boolean[][] codeout = Qrcodehandler.calqrcode (contentbytes);  for (int i = 0, i < codeout.length; i++) {for          (int j = 0; J < Codeout.length; J + +) {                if (Codeout[j][i]) {
   gs.fillrect (J * sz + pixoff, I * sz + pixoff, SZ, SZ);}}}  

Set the offset when plotting black area, otherwise it may cause the QR Code recognition error. The size of black area is good according to the actual situation.

v four or two-D code login principlev1. Schematic diagram

According to your own understanding of the painting, combined, look at the code bar.

v2. Getqrcodecontroller.java
/** * @author hjzgg * get QR code picture */@Controllerpublic class Getqrcodecontroller {@RequestMapping (value= "/gettwodemensionco De ") @ResponseBody public String Gettwodemensioncode (HttpServletRequest request) {string UUID = Uuid.randomuu        ID (). toString (). substring (0, 8);        String ip = "localhost";        try {IP = inetaddress.getlocalhost (). gethostaddress ();        } catch (Unknownhostexception e) {e.printstacktrace ();        }//Two-D code contents String content = "/http" + IP + ": 8080/yycc-portal/loginpage?uuid=" + uuid;        Generate a two-dimensional code String imgname = uuid + "_" + (int) (New Date (). GetTime ()/+) + ". png";        String Imgpath = Request.getservletcontext (). Getrealpath ("/") + imgname;        String Insertimgpath = Request.getservletcontext (). Getrealpath ("/") + "img/hjz.jpg";        Twodimensioncode handler = new Twodimensioncode ();                Handler.encoderqrcode (content, Imgpath, "PNG", NULL); Generated picture access address String QrCodeimg = "/http" + IP + ": 8080/yycc-portal/" + imgname;        Jsonobject json = new Jsonobject ();        Json.put ("uuid", uuid);        Json.put ("Qrcodeimg", qrcodeimg);    return json.tostring (); }}

The user requests the scan code way to log in, generates the QR code in the background, passes the UUID and the QR code access address to the user.

v3. Longconnectioncheckcontroller.java
@Controllerpublic class Longconnectioncheckcontroller {private static final int long_time_wait = 30000;//30s @Autow        ired private redistemplate<string, object> redistemplate; @RequestMapping (value= "/longusercheck") public String Longusercheck (String uuid) {Long inTime = new Date (). Getti        Me ();        Boolean bool = true;            while (bool) {try {thread.sleep (500);            } catch (Interruptedexception e) {e.printstacktrace ();            }//Detect login Uservo Uservo = (Uservo) redistemplate.opsforvalue (). get (UUID);            System.out.println ("longconnectioncheckaction:" + Uservo);                if (Uservo! = null) {redistemplate.delete (UUID);            Return "Forward:/logintest?username=" + uservo.getusername () + "&password=" + Uservo.getpassword ();    }else{if (new Date (). GetTime ()-inTime > long_time_wait) {bool = false;                Redistemplate.delete (UUID);    }}} return "Forward:/longconnectionfail"; } @RequestMapping (value= "/longconnectionfail") @ResponseBody public String Longconnectionfail () {Jsono        Bject json = new Jsonobject ();        Json.put ("Success", false);        Json.put ("message", "Long connection disconnected!");    return json.tostring (); }}

After the user obtains the UUID and the QR code, requests the background long connection (carries the UUID), continuously detects whether the UUID has the corresponding user information, if has then goes to the login module (carries the login information).

v4. Phonelogincontroller.java
/** * @author Hjzgg *    Mobile Login Verification */@Controllerpublic class Phonelogincontroller {        @Autowired    private Redistemplate<string, object> redistemplate;        @RequestMapping (value= "/phonelogin") public    void Phonelogin (string uuid, string Username, string password) {        Uservo user = (Uservo) redistemplate.opsforvalue (). get (UUID);        if (user = = null) {            user = new Uservo (username, password);        }        SYSTEM.OUT.PRINTLN (user);        Redistemplate.opsforvalue (). Set (uuid, user);    }        @RequestMapping (value= "/loginpage") public    String LoginPage (httpservletrequest request, string uuid) {        Request.setattribute ("uuid", uuid);        return "Phone_login";    }}

After the user has scanned the code through the mobile phone, the user information is entered on the phone and then verified (carrying UUID), and the user information of the UUID is updated in the background so that the long connection can detect the user's login information.

v Five, source code download

Two-dimensional code login example and two-dimensional code generation parsing tool source code download: Https://github.com/hjzgg/QRCodeLoginDemo

Two-dimensional code login

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.