本篇文章主要介绍了"java生成解析二维码功能",主要涉及到方面的内容,对于Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:
[java] viewplaincopypackage com.herman.test; import java.io.File; import jav...
[java] view
plaincopy
- package com.herman.test;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.imageio.ImageIO;
-
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.BinaryBitmap;
- import com.google.zxing.DecodeHintType;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.MultiFormatReader;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.NotFoundException;
- import com.google.zxing.WriterException;
- import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
- import com.google.zxing.client.j2se.MatrixToImageWriter;
- import com.google.zxing.common.HybridBinarizer;
- import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
-
- /**
- * 二维码工具类
- * @author Herman.Xiong
- * @date 2015-7-10 上午10:52:15
- * @version V3.0
- * @since jdk 1.6,tomcat 6.0
- */
- public class ZxingUtil {
- private static final Map encodeMap = new HashMap();
- private static final Map decodeMap=new HashMap();
- private static final String charset="UTF-8",format="png";
- private static final int size=150;
- private ZxingUtil() {}
-
- /**
- * 生成二维码图片
- * @author Herman.Xiong
- * @date 2015-7-10 上午10:26:33
- * @param file
- * @throws WriterException
- * @throws IOException
- */
- public static void createQRCode(String data, File file)
- throws WriterException, IOException {
- MatrixToImageWriter.writeToFile(new MultiFormatWriter().encode(
- new String(data.getBytes(charset), charset),
- BarcodeFormat.QR_CODE, size, size, encodeMap), format, file);
- }
-
- /**
- * 读取二维码信息
- * @author Herman.Xiong
- * @date 2015-7-10 上午10:25:58
- * @param filePath 二维码图片路径
- * @return
- * @throws FileNotFoundException
- * @throws IOException
- * @throws NotFoundException
- */
- public static String readQRCode(String filePath)
- throws FileNotFoundException, IOException, NotFoundException {
- return new MultiFormatReader().decode(new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(filePath))))),decodeMap).getText();
- }
-
- public static void main(String [] arg){
- try{
- createQRCode("http://blog.csdn.net/xmtblog", new File("D:\\111.png"));
- System.out.println(readQRCode("D:\\111.png"));
- //源码下载地址:http://download.csdn.net/detail/xmt1139057136/8886473
- //欢迎大家关注我的博客!如有疑问,请加QQ群:454796847共同学习!
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
运行效果如下:
源码下载地址:http://download.csdn.net/detail/xmt1139057136/8886473
以上就介绍了java生成解析二维码功能,包括了方面的内容,希望对Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_178760.html