java 二维码编码解码

做一个小项目的时候写了个二维码编码和解码的小工具,感觉可能用得到,有兴趣的朋友可以看下

再次之前,徐需要用到google的zxing相关的jar包,还有javax相关包

以上为可能用到的jar

package org.ink.image.qrimgrz;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.gson.Gson;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;

/**
 * QRImage recognize Utils imcluding code and decode
 * @author ink.Flower
 *
 */
public class QRImageUtils {
    /**
     * create QRImage,default CHARACTER_SET is UTF-8 生成二维码图片
     * @param content context of image  内容
     * @param filepath target FilePath of new QRImage  目标文件路径
     * @param picFormat picture format,like png,jpg .etc 图片类型
     * @param height height of picture 图片高度
     * @param width width of picture 图片宽度
     * @throws WriterException
     * @throws IOException
     */
    public static void QRCode(String content,String filepath,String picFormat,int height,int width) throws WriterException, IOException{
        Map<EncodeHintType,Object> hints=new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //生成矩阵
        BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hints);
        Path path = FileSystems.getDefault().getPath(filepath);
        MatrixToImageWriter.writeToPath(bitMatrix, picFormat,path);
    }
    
    /**
     * create QRImage,default CHARACTER_SET is UTF-8 将实体类对象通过json的方式生成二维码
     * @param obj The Object which is to create QRCode Image 目标实体类对象
     * @param filepath target FilePath of new QRImage
     * @param picFormat picture format,like png,jpg .etc
     * @param height height of picture
     * @param width width of picture
     * @throws WriterException
     * @throws IOException
     */
    public static void QRCode(Object obj,String filepath,String picFormat,int height,int width) throws WriterException, IOException{
        String json = new Gson().toJson(obj);
        QRCode(json, filepath, picFormat, height, width);
    }
    
    /**
     * QRDecode to Content  二维码解码成字符串
     * @param filepath 图片文件路径
     * @return content 字符串内容
     * @throws FileNotFoundException
     * @throws IOException
     * @throws NotFoundException
     */
    public static String QRDecodeToString(String filepath) throws FileNotFoundException, IOException, NotFoundException{
        return QRDecodeToString(new FileInputStream(filepath));
    }
    
    /**
     * QRDecode to Content 二维码解码
     * @param is InputStream of Image File 图片输入流
     * @return content 内容
     * @throws FileNotFoundException
     * @throws IOException
     * @throws NotFoundException
     */
    public static String QRDecodeToString(InputStream is) throws FileNotFoundException, IOException, NotFoundException{
        BufferedImage image=ImageIO.read(is);
        LuminanceSource source=new BufferedImageLuminanceSource(image);
        Binarizer binarizer=new HybridBinarizer(source);
        BinaryBitmap bitmap=new BinaryBitmap(binarizer);
        Map<DecodeHintType,Object> map=new HashMap<DecodeHintType, Object>();
        map.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result result=null;
        try{
            result = new MultiFormatReader().decode(bitmap,map);
        }catch (Exception e) {
            e.printStackTrace();
        }
        if(result!=null)
            return result.getText();
        else
            return null;
    }
    
    /**
     * decode QRCode json格式内容二维码解码成实体类
     * @param <T>
     * @param filePath 图片文件路径
     * @param clazz Class of target Pojo 实体类的Class对象
     * @return Pojo 实体类对象
     * @throws FileNotFoundException
     * @throws NotFoundException
     * @throws IOException
     */
    public static <T> Object QRDecodeToObject(String filePath,Class<T> clazz) throws FileNotFoundException, NotFoundException, IOException{
        return QRDecodeToObject(new FileInputStream(filePath), clazz);
    }
    
    /**
     * decode QRCode
     * @param <T>
     * @param is 图片输入流
     * @param clazz Class of target Pojo
     * @return Pojo
     * @throws FileNotFoundException
     * @throws NotFoundException
     * @throws IOException
     */
    public static <T> Object QRDecodeToObject(InputStream is,Class<T> clazz) throws FileNotFoundException, NotFoundException, IOException{
        InputStreamReader reader=new InputStreamReader(is);
        return new Gson().fromJson(reader, clazz);
    }
}

 以上

@ink

Ink.Flower@china
原文地址:https://www.cnblogs.com/inkflower/p/6642055.html