1. Generate QR code in Java project using SwetakeQRCode
http://swetake.com/qr/ Download address
Or http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip This is written by the Japanese and generates the square QR code we commonly use.
Can be used in Chinese
For example: 5677777ghjjjjjj
A friend asked me about the code generated by this image. I searched online and sorted out a class. First, I should place the SwetakeQRCode jar package qrcode.jar in the project compilation path. The download address of this package is the official SwetakeQRCode given above http://www.swetake.com/qrcode/java/qr_java.html. The following 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// 7% of the L level can be corrected// 15% of the M level can be corrected// 25% of the QR code can be corrected// 30% of the H level can be corrected// QR code has fault tolerance. If the QR code graphics are damaged, they can still be read by the machine, and can be up to 7% to 30% of the area damaged. //Relatively speaking, the higher the error tolerance rate, the larger the QR code graphics area. Therefore, the general compromise uses 15% fault tolerance. qrcode.setQrcodeErrorCorrect('M');/* L','M','Q','H' */ qrcode.setQrcodeEncodeMode('B');/* "N","A" or other */ qrcode.setQrcodeVersion(3);/* 0-20 */ String testString = "567777ghjjjj"; 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.length;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!"); }}Let me explain the code, I won’t talk about the rest. I will mainly talk about how to make QRcode fill the entire picture. I will first define UNIT_WIDTH=10 or other values, and then calculate how big the picture should be. Then the picture will fill the entire picture. However, if it looks good, we can control the size of the picture, so we can also define DEFAULT_WIDTH, which is the length and width of the picture, and then calculate UNIT_WIDTH, so the size of the picture is fixed. However, the unit calculated in this way may not be well divided, so the picture will not look very pleasant. These are not big problems, just adjust it. The size of bRect.length is determined by the value in qrcode.setQrcodeVersion(3), not by the length of the string.
The following zxing can also generate such qrcode.
2. Use BarCode4j to generate barcode and QR code
BarCode4j URL: http://sourceforge.net/projects/barcode4j/
barcode4j is a QR code generation algorithm using datamatrix, which supports qr algorithm.
datamatrix is a standard in Europe and the United States, and qr is a standard in Japan.
barcode4j is generally generated as rectangular.
For example: 88777alec000yan
This article is written very clearly, please refer to it
//www.VeVB.COM/article/90048.htm
3.zxing
zxing This is from Google, download 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 = 100; int height = 100; 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 a compilation of the information of the tool for generating QR codes in Java. We will continue to add relevant information in the future. Thank you for your support for this site!