This is a note about the course "Java Generation Two-Dimension Code" (http://www.imooc.com/learn/531) on Muchow.com.
Classification of 2-D codes
Linear stacked QR codes, matrix QR codes, postal codes.
2. Advantages and disadvantages of two-dimensional codes
Advantages: 1. High density encoding, large information capacity; 2. Wide encoding range; 3. Strong fault tolerance; 4. High decoding reliability; 5. Encryption measures can be introduced; 6. Low cost, easy to produce, long-lasting.
Disadvantages: 1. Two-dimensional code technology has become a new channel for mobile phone viruses and phishing websites to spread; 2. Information is easy to leak.
3. Three International Standards
1.PDF417: Chinese is not supported;
2.DM: The patent is not publicly available and the patent fees need to be paid;
3.QR Code: Patents are publicly available and supported in Chinese.
Among them, QR Code has the advantages of fast reading speed, high data density and small footprint.
4. Error Correcting Ability
Level L: about 7% error correcting data codewords
Level M: about 15% error correcting data codewords
Level Q: about 25% of the data codewords can be corrected
Level H: about 30% error correcting data codewords
5. ZXing Generate/Read QR Codes
First, download the ZXing source file.Download address: https://github.com/zxing/zxing/releases;
Again, create a Java project.Copy the core/src/main/java/com and javase/src/main/java/com files from the ZXing source file into the project and compile them into jar files;
Finally, the jar file can be used in future development.
The code ZXing generates for the two-dimensional code is as follows:
1 import java.io.File; 2 import java.nio.file.Path; 3 import java.util.HashMap; 4 5 import com.google.zxing.BarcodeFormat; 6 import com.google.zxing.EncodeHintType; 7 import com.google.zxing.MultiFormatWriter; 8 import com.google.zxing.WriterException; 9 import com.google.zxing.client.j2se.MatrixToImageWriter; 10 import com.google.zxing.common.BitMatrix; 11 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 12 13 //Generate two-dimensional code 14 public class CreateQRCode { 15 16 public static void main(String[] args) { 17 int width = 300; //2-D code width 18 int height = 300; //Two-dimensional code height 19 String format = "png"; //2D Code Picture Format 20 String content = "www.baidu.com"; //QR Code Content 21 22 //Define two-dimensional code parameters 23 HashMap hints = new HashMap(); 24 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //Define encoding for content character set 25 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); //Define Error Correction Level 26 hints.put(EncodeHintType.MARGIN, 2); //Border blank 27 28 try { 29 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height); 30 31 Path file = new File("E:/img.png").toPath(); 32 33 MatrixToImageWriter.writeToPath(bitMatrix, format, file); 34 //MatrixToImageWriter.writeToStream(bitMatrix, format, stream); 35 36 } catch (Exception e) { 37 e.printStackTrace(); 38 } 39 } 40 }
The code for ZXing to read the QR code information is as follows:
1 import java.awt.image.BufferedImage; 2 import java.io.File; 3 import java.io.IOException; 4 import java.util.HashMap; 5 6 import javax.imageio.ImageIO; 7 8 import com.google.zxing.Binarizer; 9 import com.google.zxing.BinaryBitmap; 10 import com.google.zxing.EncodeHintType; 11 import com.google.zxing.LuminanceSource; 12 import com.google.zxing.MultiFormatReader; 13 import com.google.zxing.NotFoundException; 14 import com.google.zxing.Result; 15 import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 16 import com.google.zxing.common.BitArray; 17 import com.google.zxing.common.BitMatrix; 18 import com.google.zxing.common.HybridBinarizer; 19 20 public class ReadQRCode { 21 22 public static void main(String[] args) { 23 24 try { 25 MultiFormatReader formatReader = new MultiFormatReader(); 26 27 File file = new File("E:/img.png"); 28 29 BufferedImage image = ImageIO.read(file); //Read this file to identify it as a picture 30 31 BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); 32 33 //Define two-dimensional code parameters 34 HashMap hints = new HashMap(); 35 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //Define encoding for content character set 36 37 38 Result result = formatReader.decode(binaryBitmap,hints); 39 40 System.out.println("Resolution results:" + result.toString()); 41 System.out.println("QR code format type:" + result.getBarcodeFormat()); 42 System.out.println("QR Code Text Content:" + result.getText()); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } 46 } 47 }
6. QRCode generation/reading of QR codes
The QRCode jar s for generating and reading QR codes are separated, and the download web address is as follows:
QRCode Generate QR Code Web Address: http://swetake.com/qrcode/index-e.html
QRCode reads QR code web address: https://osdn.jp/projects/qrcode (this site cannot download to jar file)
(Later, I searched the Internet for a jar file that contains functions to generate and read QR codes. Download path: https://files.cnblogs.com/files/bigroc/QRCode.zip)
QRCode generates two-dimensional code as follows:
1 import java.awt.Color; 2 import java.awt.Graphics2D; 3 import java.awt.image.BufferedImage; 4 import java.io.File; 5 import java.io.IOException; 6 import java.io.UnsupportedEncodingException; 7 8 import javax.imageio.ImageIO; 9 10 import com.swetake.util.Qrcode; 11 12 //Generate two-dimensional code 13 public class CreateQRCode { 14 15 public static void main(String[] args) throws IOException { 16 17 //Computing aspect ratio of 2D code pictures 18 //API The document specifies how to calculate the width and height of the picture. v Is Version Number(1~40) 19 int v = 7; 20 int width = 67 + 12 * (v - 1); //Calculation Formula 21 int height = 67 + 12 * (v - 1); 22 23 Qrcode x = new Qrcode(); 24 25 /** 26 * Error Correction Level is divided into 27 * level L : A maximum of 7% of errors can be corrected; 28 * level M : A maximum of 15% of errors can be corrected; 29 * level Q : A maximum of 25% of errors can be corrected; 30 * level H : Up to 30% of errors can be corrected; 31 */ 32 x.setQrcodeErrorCorrect('L'); //Set Error Correction Level 33 x.setQrcodeEncodeMode('B'); //N Represents a number A representative a-Z B Represents other characters 34 x.setQrcodeVersion(v); //version number(1~40) 35 String qrData = "http://www.baidu.com"; //Content Information 36 37 //Buffer 38 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 39 40 //Mapping 41 Graphics2D gs = bufferedImage.createGraphics(); 42 43 gs.setBackground(Color.WHITE); //background color 44 gs.setColor(Color.black); 45 gs.clearRect(0, 0, width, height); //Clear the contents of the drawing board 46 47 //Offset 2 or 7 48 int pixoff = 2; 49 50 /** 51 * 1.Notice the order of i, j inside the for loop, 52 * s[j][i]The order of J and I of the two-dimensional array is gs.fillRect (j*3+pixoff, i*3+pixoff, 3,3) in this method. 53 * Sequence matches, otherwise the parsed picture is a string of numbers 54 * 2.Note this judgement if (d.length > 0 && d.length < 120) 55 * Does this cause string length greater than 120 to cause code generation not to execute, QR code blank 56 * Set this configuration based on your own string size 57 */ 58 //Convert the contents to be filled into bytes 59 byte[] d = qrData.getBytes("utf-8"); //Format of Chinese Character Conversion 60 if (d.length > 0 && d.length < 120) { 61 boolean[][] s = x.calQrcode(d); 62 63 for (int i = 0; i < s.length; i++) { 64 for (int j = 0; j < s.length; j++) { 65 if (s[j][i]) { 66 //hold d Content Filling in 67 gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); 68 } 69 } 70 } 71 } 72 73 gs.dispose(); //End Writing 74 bufferedImage.flush(); //End buffered 75 ImageIO.write(bufferedImage, "png", new File("E:/img.png")); //Write pictures to a specified path 76 77 } 78 }
QRCode parses two-dimensional code as follows:
1 import java.awt.image.BufferedImage; 2 import java.io.File; 3 import java.io.IOException; 4 5 import javax.imageio.ImageIO; 6 7 import jp.sourceforge.qrcode.QRCodeDecoder; 8 9 public class ReadQRCode { 10 11 public static void main(String[] args) throws IOException { 12 13 //Picture Path 14 File file = new File("E:/img.png"); 15 //Read Picture to Buffer 16 BufferedImage bufferedImage = ImageIO.read(file); 17 //QRCode Decoder 18 QRCodeDecoder codeDecoder = new QRCodeDecoder(); 19 20 /** 21 *codeDecoder.decode(new MyQRCodeImage()) 22 *Here you need to implement the QRCodeImage interface, and MyQRCodeImage.java implements the interface class 23 */ 24 //Get information by parsing a two-dimensional code 25 String result = new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)), "utf-8"); 26 System.out.println(result); 27 } 28 }
MyQRCodeImage.java class implements QRCodeImage interface
1 import java.awt.image.BufferedImage; 2 3 import jp.sourceforge.qrcode.data.QRCodeImage; 4 5 public class MyQRCodeImage implements QRCodeImage{ 6 7 BufferedImage bufferedImage; 8 9 public MyQRCodeImage(BufferedImage bufferedImage) { 10 this.bufferedImage = bufferedImage; 11 } 12 13 //wide 14 @Override 15 public int getWidth() { 16 return bufferedImage.getWidth(); 17 } 18 19 //high 20 @Override 21 public int getHeight() { 22 return bufferedImage.getHeight(); 23 } 24 25 //colour 26 @Override 27 public int getPixel(int i, int j) { 28 return bufferedImage.getRGB(i, j); 29 } 30 }
7. Notes
The jar package can be placed under the lib package. Set the builde path for the project, select the project, right-click, select Builde Path, click Configure Builde Path, and click Add Jars under libraries to add the jar file.