JavaUtil_02_二维码的生成与解析
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 }
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="你好啊,@¥%2@#$%,//"; 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 }
参考文章: Java生成、解析二维码 ( http://www.cnblogs.com/xz-luckydog/p/6402568.html)