Java Generating two-dimensional Code tool data collation _java

Source: Internet
Author: User
Tags set background string format

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

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

Or the Http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip, which is written by the Japanese, produces our common square two-dimensional code.

can use Chinese

such as: 5677777GHJJJJJ

A friend asked me to have this picture generated code, I will search the internet and then sorted out a class, first of all to put the Swetakeqrcode jar package Qrcode.jar in the project's compile path, the package download address is given above the swetakeqrcode of the official HTTP ://www.swetake.com/qrcode/java/qr_java.html, here is a test class

Import Com.swetake.util.Qrcode;
Import Javax.imageio.ImageIO;
Import java.awt.*;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;

Import java.io.UnsupportedEncodingException;
 /** * Created with IntelliJ idea.
 * DATE:10/9/13 * time:11:31 AM * */public class Qrcodetest {private static int default_width;

 private static int unit_width = 10;

 public static void Main (String args[]) throws exception{createimg ();
 public static void Createimg () {QRCode qrcode=new qrcode (); Error correction capacity//l level 7% codewords can be modified//m level 15% codewords can be modified//q level 25% codewords can be modified//h level 30% codewords can be modified//QR code has fault tolerance, QR code graphics If there is damage, can still be machine read content, the maximum can to 7
 %~30% area damage can still be read. Comparatively speaking, the higher the fault tolerance rate, the larger the area of QR code graphics.
 So the general tradeoff uses 15% fault tolerant capabilities.
 Qrcode.setqrcodeerrorcorrect (' m '); * L ', ' m ', ' Q ', ' H ' */qrcode.setqrcodeencodemode (' B ');/* "N", "A" or "other" * *

 Qrcode.setqrcodeversion (3);/* 0-20/String teststring = "5677777GHJJJJJ";
 byte[] buff = null;
 try {buff = teststring.getbytes ("Utf-8"); catch (UnsupportedencodingexcePtion e) {e.printstacktrace ();
 } boolean[][] Brect = Qrcode.calqrcode (buff);

 Default_width = Brect.length * unit_width;
BufferedImage bi = new BufferedImage (Default_width, Default_width, Bufferedimage.type_int_rgb);

int unitwidth = Default_width/brect.length;

CreateGraphics graphics2d g = bi.creategraphics ();
 Set Background g.setbackground (Color.White);
 G.clearrect (0, 0, default_width, default_width);

 G.setcolor (Color.Black); if (buff.length>0 && buff.length <123) {for (int i=0;i<brect.length;i++) {for (int j=0;j<brect.len
 gth;j++) {if (Brect[j][i]) {g.fillrect (J*unit_width, I*unit_width, Unit_width-1, unit_width-1);
 }}} g.dispose ();

 Bi.flush ();
 String filepath= "Qrcode.png";

 File F = new file (FilePath);
 try {imageio.write (bi, "PNG", f);
 catch (IOException e) {e.printstacktrace ();
 } System.out.println ("Create qrcode finished!"); }
}

For a description of the code, other do not say, mainly say how to make qrcode full of the picture, I here is to define unit_width=10 or other values, and then calculate the screen should be how big, after the painting will be filled with the whole picture, but, so good-looking is good-looking, We can control the size of the screen, so we can also define the default_width is the length of the screen, and then calculate the unit_width, so that the size of the picture is fixed, but, so the unit default_width/ Brect.length not necessarily whole good division, so the screen looks will not be very cool, these are not big problems, adjust a tune on it. The size of the brect.length is determined by the value of the Qrcode.setqrcodeversion (3), not by the length of the string.

There are also the following zxing can also generate such a qrcode.

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 generally produced in rectangular form.

such as: 88777alec000yan

This article is written very clearly, can refer to the following

Http://www.jb51.net/article/90048.htm

3.zxing

Zxing This is Google's, download the address

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

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);
 
 }

The above is the Java to generate two-dimensional code tool data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.