Java implementation of two-dimensional code generation of several methods

Source: Internet
Author: User

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

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 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 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:

    1. function Utf16to8 (str) {
    2. var out, I, Len, C;
    3. out = "";
    4. len = str.length;
    5. For (i = 0; i < len; i++) {
    6. c = str.charcodeat (i);
    7. if ((C >= 0x0001) && (c <= 0x007F)) {
    8. Out + = Str.charat (i);
    9. } Else if (C > 0x07ff) {
    10. Out + = String.fromCharCode (0xE0 | ((c >> b) & 0x0F));
    11. Out + = String.fromCharCode (0x80 | ((c >> 6) & 0x3F));
    12. Out + = String.fromCharCode (0x80 | ((c >> 0) & 0x3F));
    13. } Else {
    14. Out + = String.fromCharCode (0xC0 | ((c >> 6) & 0x1F));
    15. Out + = String.fromCharCode (0x80 | ((c >> 0) & 0x3F));
    16. }
    17. }
    18. return out;
    19. }

Zxing This is Google's

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

Java code:

    1. Import Java.io.File;
    2. Import java.util.Hashtable;
    3. Import Com.google.zxing.BarcodeFormat;
    4. Import Com.google.zxing.EncodeHintType;
    5. Import Com.google.zxing.MultiFormatWriter;
    6. Import Com.google.zxing.client.j2se.MatrixToImageWriter;
    7. Import Com.google.zxing.common.BitMatrix;
    8. Import Com.google.zxing.qrcode.QRCodeWriter;
    9. Public class Qrcodeevents {
    10. public static void Main (String []args)throws exception{
    11. String text = "Hello";
    12. int width = 100;
    13. int height = 100;
    14. String format = "PNG";
    15. Hashtable hints= New Hashtable ();
    16. Hints.put (Encodehinttype.character_set, "Utf-8");
    17. Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (text, Barcodeformat.qr_code, width, height,hints);
    18. File OutputFile = new File ("New.png");
    19. Matrixtoimagewriter.writetofile (Bitmatrix, format, outputFile);
    20. }
    21. }

Two-dimensional code, also known as two-dimensional barcode, two-dimensional barcode was first invented in Japan, it is a certain geometry according to a certain pattern in the plane (two-dimensional direction) distribution of black and white graphics data symbol information, in the code to skillfully use the structure of the computer's internal logic based on the "0", "1" bitstream concept, A number of geometric shapes corresponding to the binary are used to represent the literal numerical information, which is automatically read by the image input device or photoelectric scanning device to realize automatic information processing. It has some common features of barcode technology: Each code system has its specific character set, each character occupies a certain width, and has certain checking function. At the same time, it also has the features of automatic recognition of different lines, and the processing of the change of graphics rotation.

1.java this way. Generate two-dimensional code there are many developed jar packages such as zxing,qrcode.q the former is Google development of the latter is small Japan developed, here I use the zxing development package to get


2. Download the Zxing Development Kit first, here is only the core of the jar package


3. A class is also required to develop with zxing, the code is as follows

    Package org.lxh;              Import Com.google.zxing.common.BitMatrix;       Import Javax.imageio.ImageIO;       Import Java.io.File;       Import Java.io.OutputStream;       Import java.io.IOException;                     Import Java.awt.image.BufferedImage;         Public final class Matrixtoimagewriter {private static final int BLACK = 0xff000000;                private static final int white = 0xFFFFFFFF;           Private Matrixtoimagewriter () {} public static bufferedimage Tobufferedimage (Bitmatrix matrix) {           int width = matrix.getwidth ();           int height = matrix.getheight ();           BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb); for (int x = 0; x < width; + +) {for (int y = 0; y < height; y++) {Image.setrgb (x, Y, MA Trix.get (x, y)?             Black:white);         }} return image; } public static void WriteToFile (Bitmatrix matrix, String format, file file) throws IOException {bufferedimage ima           GE = tobufferedimage (matrix); if (!  Imageio.write (image, format, file)) {throw new IOException ("Could not write an image of format" + Format + "           To "+ file);              }} public static void WriteToStream (Bitmatrix matrix, String format, OutputStream stream)           Throws IOException {bufferedimage image = Tobufferedimage (matrix); if (!            Imageio.write (image, format, stream)) {throw new IOException ("Could not write an image of format" + format);   }         }              }

4. Generate a QR code using the class above

    Package org.lxh;      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.WriterException;            Import Com.google.zxing.common.BitMatrix; public class Test {/** * @param args * @throws Exception */Public St              atic void Main (string[] args) throws Exception {String text = "http://www.baidu.com";              int width = 300;              int height = 300;              The picture format of the QR code, String formatted = "GIF";              Hashtable hints = new Hashtable ();              The content is encoded using Hints.put (Encodehinttype.character_set, "utf-8"); Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (text, barcodeformat.qr_code, width, height, hint              s); Generate two-dimensional code File OutputFile = New File ("D:" +file.separator+ "new.gif");                Matrixtoimagewriter.writetofile (Bitmatrix, format, outputFile);   }            }

Text is the content of the two-dimensional code here can make ordinary text can also be links, very simple finally the resulting QR code image to everyone

Java implementation of two-dimensional code generation of several methods

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.