Several methods for Java implementation of two-dimensional code generation (recommended) _java

Source: Internet
Author: User
Tags string format

Java implementation of the two-dimensional code generation of several methods, specific as follows:

1: Using Swetakeqrcode to generate a two-dimensional code in a Java project

http://swetake.com/qr/Download Address

or Http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip.

This is written by the Japanese and produces our common square two-dimensional code.

can use Chinese

such as: 5677777GHJJJJJ

2: Using BARCODE4J to generate barcodes and two-dimensional codes

barcode4j URL: http://sourceforge.NET/projects/barcode4j/

BARCODE4J is a two-dimensional code generation algorithm using Datamatrix to support QR algorithms.

Datamatrix is the standard of Europe and America, QR is the standard of Japan,

BARCODE4J is usually produced in rectangular form.

such as: 88777alec000yan

3:zxing

Zxing This is Google's

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

Java code:

Import Java.io.File;  
Import java.util.Hashtable;  
 
Import Com.google.zxing.BarcodeFormat;  
Import Com.google.zxing.EncodeHintType;  
Import Com.google.zxing.MultiFormatWriter;  
Import Com.google.zxing.client.j2se.MatrixToImageWriter;  
Import Com.google.zxing.common.BitMatrix;  
Import Com.google.zxing.qrcode.QRCodeWriter;  
 
 
 
public class Qrcodeevents {public  
    
  static void main (string []args) throws exception{  
    string text = "Hello";  
    int width = m;  
    int height = m;  
    String format = "png";  
    Hashtable hints= new Hashtable ();  
    Hints.put (Encodehinttype.character_set, "Utf-8");  
     Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (text, Barcodeformat.qr_code, width, height,hints);  
     File outputfile = new file ("New.png");  
     Matrixtoimagewriter.writetofile (Bitmatrix, format, outputfile);  
      
  }  
 

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

Using this API, use Google AppEngine for implementation.

5:js generate two-dimensional code

Using Jquery-qrcode to generate two-dimensional code

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 for the realization of two-dimensional code data calculation,

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

The main features supported are:

JS Code:

Text   

JS Code:

Render  : "Canvas",//Set render mode width    : 256,//   set width height   : 256,   //Set height  
typenumber: -1,   //Calculation mode  
correctlevel  : qrerrorcorrectlevel.h,//error correction level  
background   : "#ffffff",//Background color  
foreground   

Very simple to use

JS Code:

 
 

After a simple practice,

Rendering performance with canvas is still very good, but if you use the table, the performance is not ideal, especially the browser below IE9, so you need to optimize the way to render the table, here is not detailed.

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

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

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

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

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

function Utf16to8 (str) {  
  var out, 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  
    = = S Tring.fromcharcode (0xe0 | ((c >>) & 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));  
  }  
  return out;  
} 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.