1. 程式人生 > >Java_util_02_二維碼的生成與解析

Java_util_02_二維碼的生成與解析

isp tar org img 工具 bsp nan tab htm

1.引入jar包

zxing-core-1.7.jar : http://viralpatel.net/blogs/download/jar/zxing-core-1.7.jar

zxing-javase-1.7.jar : http://viralpatel.net/blogs/download/jar/zxing-j2se-1.7.jar

2.工具類—QRCodeUtil.java

技術分享
  1 package com.ray.util;
  2 
  3 import java.awt.image.BufferedImage;
  4 import java.io.File;
5 import java.util.Hashtable; 6 import java.util.Random; 7 import javax.imageio.ImageIO; 8 9 import com.google.zxing.BarcodeFormat; 10 import com.google.zxing.Binarizer; 11 import com.google.zxing.BinaryBitmap; 12 import com.google.zxing.EncodeHintType; 13 import com.google.zxing.LuminanceSource;
14 import com.google.zxing.MultiFormatReader; 15 import com.google.zxing.MultiFormatWriter; 16 import com.google.zxing.Result; 17 import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 18 import com.google.zxing.client.j2se.MatrixToImageWriter; 19 import com.google.zxing.common.BitMatrix;
20 import com.google.zxing.common.HybridBinarizer; 21 22 public class QRCodeUtil { 23 24 /** 25 * 根據內容,生成指定寬高、指定格式的二維碼圖片 26 * 27 * @param text 內容 28 * @param width 寬 29 * @param height 高 30 * @param format 圖片格式 31 * @return 生成的二維碼圖片路徑 32 * @throws Exception 33 */ 34 public static String generateQRCode(String text, int width, int height, String format) throws Exception { 35 Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); 36 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 37 BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); 38 39 String pathName = "D:/new.png"; //指定輸出文件的路徑 40 File outputFile = new File(pathName); 41 MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); 42 43 return pathName; 44 } 45 46 /** 47 * 隨機生成指定長度的驗證碼 48 * 49 * @param length 驗證碼長度 50 * @return 生成的驗證碼 51 */ 52 public static String generateNumCode(int length) { 53 String val = ""; 54 String charStr = "char"; 55 String numStr = "num"; 56 Random random = new Random(); 57 58 //參數length,表示生成幾位隨機數 59 for (int i = 0; i < length; i++) { 60 61 String charOrNum = random.nextInt(2) % 2 == 0 ? charStr : numStr; 62 //輸出字母還是數字 63 if (charStr.equalsIgnoreCase(charOrNum)) { 64 //輸出是大寫字母還是小寫字母 65 int temp = random.nextInt(2) % 2 == 0 ? 65 : 97; 66 val += (char) (random.nextInt(26) + temp); 67 } else if (numStr.equalsIgnoreCase(charOrNum)) { 68 val += String.valueOf(random.nextInt(10)); 69 } 70 } 71 return val; 72 } 73 74 /** 75 * 解析指定路徑下的二維碼圖片 76 * 77 * @param filePath 二維碼圖片路徑 78 * @return 79 */ 80 public static String parseQRCode(String filePath) { 81 String content = ""; 82 try { 83 File file = new File(filePath); 84 BufferedImage image = ImageIO.read(file); 85 LuminanceSource source = new BufferedImageLuminanceSource(image); 86 Binarizer binarizer = new HybridBinarizer(source); 87 BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); 88 Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); 89 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 90 MultiFormatReader formatReader = new MultiFormatReader(); 91 Result result = formatReader.decode(binaryBitmap, hints); 92 93 System.out.println("result 為:" + result.toString()); 94 System.out.println("resultFormat 為:" + result.getBarcodeFormat()); 95 System.out.println("resultText 為:" + result.getText()); 96 //設置返回值 97 content = result.getText(); 98 } catch (Exception e) { 99 e.printStackTrace(); 100 } 101 return content; 102 } 103 104 105 106 107 108 }
View Code

3.測試類—QRCodeTest.java

技術分享
 1 package com.ray.test;
 2 
 3 import org.junit.Test;
 4 import com.ray.util.QRCodeUtil;
 5 
 6 
 7 public class QRCodeTest {
 8 
 9     @Test
10     public void testQRCode(){
11         //String text = QRCodeUtil.generateNumCode(12);  //隨機生成的12位驗證碼
12         //System.out.println("隨機生成的12位驗證碼為: " + text);
13 
14         String text="你好啊,@¥%[email protected]#$%,//";
15         System.out.println("text");
16         int width = 100;    //二維碼圖片的寬
17         int height = 100;   //二維碼圖片的高
18         String format = "png";  //二維碼圖片的格式
19 
20         try {
21             //生成二維碼圖片,並返回圖片路徑
22             String pathName = QRCodeUtil.generateQRCode(text, width, height, format);
23             System.out.println("生成二維碼的圖片路徑: " + pathName);
24 
25             String content = QRCodeUtil.parseQRCode(pathName);
26             System.out.println("解析出二維碼的圖片的內容為: " + content);
27         } catch (Exception e) {
28             e.printStackTrace();
29         }
30 
31 
32     }
33 
34     
35 
36 }
View Code

參考文章: Java生成、解析二維碼 ( http://www.cnblogs.com/xz-luckydog/p/6402568.html)

Java_util_02_二維碼的生成與解析