Several ways to generate two-dimensional code in Java

Source: Internet
Author: User
Tags string format

1: Using Swetakeqrcode to generate two-dimensional code in a Java project
http://swetake.com/qr/
or Http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip.
This was written by the Japanese and generated by our common square QR code.
Can be in Chinese

such as: 5677777GHJJJJJ

2: Use barcode4j to generate barcodes and QR codes
BARCODE4J website: http://sourceforge. NET/projects/barcode4j/

BARCODE4J is a two-dimensional code generation algorithm that uses Datamatrix to support the QR algorithm
Datamatrix is the European and American Standard, the QR is the Japanese standard,
Barcode4j are generally generated as rectangles.

such as: 88777alec000yan
This blog is very clear about this aspect:
http://baijinshan.iteye.com/blog/1004554

3:zxing
Zxing This is Google's

Http://code.google.com/p/zxing/downloads/list

Java code:

ImportJava.io.File; Importjava.util.Hashtable; ImportCom.google.zxing.BarcodeFormat; ImportCom.google.zxing.EncodeHintType; ImportCom.google.zxing.MultiFormatWriter; ImportCom.google.zxing.client.j2se.MatrixToImageWriter; ImportCom.google.zxing.common.BitMatrix; ImportCom.google.zxing.qrcode.QRCodeWriter;  Public classqrcodeevents { Public Static voidMain (String []args)throwsexception{String Text= "Hello"; intwidth = 100; intHeight = 100; String format= "png"; Hashtable hints=NewHashtable (); Hints.put (Encodehinttype.character_set,"Utf-8"); Bitmatrix Bitmatrix=NewMultiformatwriter (). Encode (text, Barcodeformat.qr_code, width, height,hints); File OutputFile=NewFile ("New.png");                   Matrixtoimagewriter.writetofile (Bitmatrix, format, outputFile); }   } 

4:google Chart API There is a way to implement a two-dimensional code

Use this API to implement with Google AppEngine.

5:js generate two-dimensional code

Using jQuery-qrcode to generate QR codes

First of all, Jquery-qrcode, this open source tripartite library (can be obtained from Https://github.com/jeromeetienne/jquery-qrcode),

Qrcode.js is the core class to realize the computation of two-dimensional code data,

Jquery.qrcode.js is to use it in a jquery way, using it to achieve graphics rendering, is actually drawing (support canvas and table two ways)


The main features supported are:

JS Code:

    1. Text: "Https://github.com/jeromeetienne/jquery-qrcode"//Set two-dimensional code content

JS Code:

  1. Render: "Canvas",//Set rendering mode
  2. width:256, //Set width
  3. height:256, //Set height
  4. Typenumber:-1, //Calculation mode
  5. Correctlevel:qrerrorcorrectlevel.h,//error correction level
  6. Background: "#ffffff",//Background color
  7. Foreground: "#000000" //Foreground color

The way to use it is very simple

JS Code:

    1. JQuery (' #output '). QRCode ({width:200,height:200,correctlevel:0,text:content});

After a simple practice,

Using the canvas to render the performance is very good, but if you use Table mode, performance is not ideal, especially IE9 below the browser, so you need to optimize the way you render table, here is not described in detail.

In fact, the above JS has a small drawback, that is, the default does not support Chinese.

This is related to the mechanism of JS, Jquery-qrcode This library is using charCodeAt () This way to encode the conversion,

And this method by default will get its Unicode encoding, the general decoder is the use of UTF-8, iso-8859-1 and other ways,

English is no problem, if it is Chinese, the general Unicode is UTF-16 implementation, the length of 2 bits, and UTF-8 encoding is 3 bits, so that the codec of the two-dimensional code does not match.

The solution, of course, is to convert the string to UTF-8 before the QR code is encoded, as follows:

functionUtf16to8 (str) {varOut , I, Len, C; out= ""; Len=str.length;  for(i = 0; i < Len; i++) {C=str.charcodeat (i); if((c >= 0x0001) && (c <= 0x007F) ) { out+=Str.charat (i); } Else if(C > 0x07ff) { out+ = String.fromCharCode (0xE0 | ((c >> b) & 0x0F)); out+ = String.fromCharCode (0x80 | ((c >> 6) & 0x3F)); out+ = String.fromCharCode (0x80 | ((c >> 0) & 0x3F)); } Else{ out+ = String.fromCharCode (0xC0 | ((c >> 6) & 0x1F)); out+ = String.fromCharCode (0x80 | ((c >> 0) & 0x3F)); }       }       returnOut ; }  

Several ways to generate two-dimensional code in Java

Related Article

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.