QRCode二维码生成与识别

// 内网通扫码签到的二维码识别
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
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.*;
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;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

public class Test {
    private static final String fromDir = "/mnt/shared/qrcodes";
    private static final String toDir = "/mnt/shared/processedQRCodes";
    private static final String PNG = ".png";

    public static void main(String[] args) {
        File dir = new File(fromDir);
        if(dir.exists() && dir.isDirectory()){
            File[] files = dir.listFiles();
            for(File f:files){
                if(f.isFile()&&f.getName().toLowerCase().endsWith(PNG)){
                    String res = decode(f);
                    System.out.println(res);
                    if(StringUtils.isNotBlank(res)){
                        // move to processed dir
                        boolean moved = move(f,new File(toDir+File.separator+f.getName().substring(0,f.getName().indexOf("."))+"_"+res+PNG));
                        System.out.println(moved==true?"Moved to processed dir.":"Can't move file: "+f.getAbsolutePath());
                    }
                }
            }
        }

        encode("http://baike.baidu.com/link?url=BP7l08ZZZa7HBLL0g4RCuVsN3BlNbuokYRKCuUk8ZexPDjxSVNMXQq_08WI1xz5EUAjVrce4qgSisos9K-mq-q");
    }

    private static boolean  move(File f,File toDir){
        return f.renameTo(toDir);
    }

    /**
     * 解析图像
     */
    public static String decode(File file) {
        BufferedImage image;
        String res = null;
        try {
            image = ImageIO.read(file);
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
//            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
            String resultStr = result.getText();
//            System.out.println(resultStr);
            String t = resultStr.substring(resultStr.indexOf("?")+3,resultStr.length());
            String base64 = new String(Base64.decodeBase64(t));
//            System.out.println(t);
            System.out.print(base64+"   --->   ");
            DecodeResult decodeResult = new Gson().fromJson(base64,DecodeResult.class);
            res = decodeResult.getD();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static void encode(String content){
        int width = 400; // 图像宽度
        int height = 400; // 图像高度
        String format = "png";// 图像类型
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try{
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
                    BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
            Path path = FileSystems.getDefault().getPath(toDir, "code.png");
            MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

class DecodeResult{
    private String a;
    private String b;
    private String c;
    private String d;
    private String v;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

    public String getD() {
        return d;
    }

    public void setD(String d) {
        this.d = d;
    }

    public String getV() {
        return v;
    }

    public void setV(String v) {
        this.v = v;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值