Java 解析二维码demo

记录下来 ,方便以后使用

package com.xhb.orc.controller;

import com.google.zxing.*;
import com.google.zxing.common.HybridBinarizer;
import com.xhb.orc.BufferedImageLuminanceSource;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;


@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class Ocr {

    public static String decodeQr(String filePath) {
        String retStr = "";
        if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {
            return "图片路径为空!";
        }
        try {
            BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));
            LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap bitmap = new BinaryBitmap(binarizer);

            HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>();
//            hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");
//
//
          hintTypeObjectHashMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
//
//
//        ///    hintTypeObjectHashMap.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
//            hintTypeObjectHashMap.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);




            Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap);
            retStr = result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retStr;
    }

    public Map doQr(String imgpath){
        long stime = System.currentTimeMillis();
        Map<String,Object > map = new HashMap ();

        String str = null;
        try {
            str = decodeQr(imgpath);
            map.put("result",str);
            map.put("status","success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status","fail");

        }

        System.out.println(str);


        long etime = System.currentTimeMillis();
        long time = (etime - stime);
        System.out.printf("执行时长:%d 毫秒.",time);

        map.put("time",time);
        return map;
    }

    @PostMapping("/upload")
    public Map upload(@RequestParam("imgFile") MultipartFile file, @RequestParam("imgName") String name) throws Exception {
        // 设置上传至项目文件夹下的uploadFile文件夹中,没有文件夹则创建
        File dir = new File("uploadFile");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String filename =dir.getAbsolutePath() + File.separator + System.currentTimeMillis()+".png";
        file.transferTo(new File(filename));

        return doQr(filename);
    }



    @RequestMapping("qr")
        public Map  qr(HttpServletRequest request, HttpServletResponse response)   {

        String imgpath = request.getParameter("imgpath");
//        String type = request.getParameter("type");
//        Map map = new HashMap();
        return doQr(imgpath);
//
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值