腾讯云OCR图片文字识别

走远了吗. 提交于 2019-12-28 20:56:52

一、 OCR

OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程;

-- 来自百度

二、腾讯云OCR

基于腾讯自研的深度学习技术和海量的数据,提供卡证、票据类印刷体和手写体、自定义模板等多种场景和类型的文字识别服务。

三、接口对接

说明:基于 spring boot 的接口对接

1、添加开发的SDK

<dependency>
  <groupId>com.qcloud</groupId>
  <artifactId>qcloud-image-sdk</artifactId>
  <version>2.3.6</version>
</dependency>

2、编写工具类

注意:此接口对接版本有点低,现在的sdk是2.0了,不过这个工具类是可以正常食用的。2.0的sdk可以官方给出的文档 传送门

import com.qcloud.image.ImageClient;
import com.qcloud.image.exception.AbstractImageException;
import com.qcloud.image.request.*;
import java.io.File;

/**
 * 腾讯云Ocr文字识别
 *
 * @author lixingwu
 */
public class OcrUtil {
    /*配置参数为空,请自行填写*/
    private static final String APP_ID = "";
    private static final String SECRET_ID = "";
    private static final String SECRET_KEY = "";
    private static final String BUCKET_NAME = "";
    private static final ImageClient IMAGECLIENT = new ImageClient(APP_ID, SECRET_ID, SECRET_KEY, ImageClient.NEW_DOMAIN_recognition_image_myqcloud_com);

    /**
     * 方法描述:识别身份证URL方式.
     * 创建时间:2018-12-19 11:54:36
     *
     * @param url      图片地址
     * @param cardType 0正面,1反面
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrIdCardUrl(String url, Integer cardType) {
        String ret = null;
        String[] idcardUrlList = new String[]{url};
        IdcardDetectRequest idReq = new IdcardDetectRequest(BUCKET_NAME, idcardUrlList, cardType);
        try {
            ret = IMAGECLIENT.idcardDetect(idReq);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:识别身份证path方式.
     * 创建时间:2018-12-19 11:54:36
     *
     * @param path     图片路径
     * @param cardType 0正面,1反面
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrIdCardPath(String path, Integer cardType) {
        String ret = null;
        File[] idcardUrlList = new File[]{new File(path)};
        IdcardDetectRequest idReq = new IdcardDetectRequest(BUCKET_NAME, idcardUrlList, cardType);
        try {
            ret = IMAGECLIENT.idcardDetect(idReq);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:驾驶证识别URL方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param url 图片地址
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrDrivingLicenceUrl(String url) {
        String ret = null;
        OcrDrivingLicenceRequest request = new OcrDrivingLicenceRequest(BUCKET_NAME, OcrDrivingLicenceRequest.TYPE_DRIVER_LICENSE, url);
        try {
            ret = IMAGECLIENT.ocrDrivingLicence(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:驾驶证识别path方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param path 图片路径
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrDrivingLicencePath(String path) {
        String ret = null;
        File file = new File(path);
        OcrDrivingLicenceRequest request = new OcrDrivingLicenceRequest(BUCKET_NAME, OcrDrivingLicenceRequest.TYPE_DRIVER_LICENSE, file);
        try {
            ret = IMAGECLIENT.ocrDrivingLicence(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:行驶证识别URL方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param url 图片地址
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrVehicleLicenceUrl(String url) {
        String ret = null;
        OcrDrivingLicenceRequest request = new OcrDrivingLicenceRequest(BUCKET_NAME, OcrDrivingLicenceRequest.TYPE_VEHICLE_LICENSE, url);
        try {
            ret = IMAGECLIENT.ocrDrivingLicence(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:行驶证识别path方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param path 图片路径
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrVehicleLicencePath(String path) {
        String ret = null;
        File file = new File(path);
        OcrDrivingLicenceRequest request = new OcrDrivingLicenceRequest(BUCKET_NAME, OcrDrivingLicenceRequest.TYPE_VEHICLE_LICENSE, file);
        try {
            ret = IMAGECLIENT.ocrDrivingLicence(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:车牌识别URL方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param url 图片地址
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrPlateUrl(String url) {
        String ret = null;
        OcrPlateRequest request = new OcrPlateRequest(BUCKET_NAME, url);
        try {
            ret = IMAGECLIENT.ocrPlate(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:车牌识别path方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param path 图片路径
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrPlatePath(String path) {
        String ret = null;
        File file = new File(path);
        OcrPlateRequest request = new OcrPlateRequest(BUCKET_NAME, file);
        try {
            ret = IMAGECLIENT.ocrPlate(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:营业执照识别URL方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param url 图片地址
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrBizLicenseUrl(String url) {
        String ret = null;
        OcrBizLicenseRequest request = new OcrBizLicenseRequest(BUCKET_NAME, url);
        try {
            ret = IMAGECLIENT.ocrBizLicense(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:营业执照识别path方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param path 图片路径
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrBizLicensePath(String path) {
        String ret = null;
        File file = new File(path);
        OcrBizLicenseRequest request = new OcrBizLicenseRequest(BUCKET_NAME, file);
        try {
            ret = IMAGECLIENT.ocrBizLicense(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:银行卡识别URL方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param url 图片地址
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrBankCardUrl(String url) {
        String ret = null;
        OcrBankCardRequest request = new OcrBankCardRequest(BUCKET_NAME, url);
        try {
            ret = IMAGECLIENT.ocrBankCard(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:银行卡识别path方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param path 图片路径
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrBankCardPath(String path) {
        String ret = null;
        File file = new File(path);
        OcrBankCardRequest request = new OcrBankCardRequest(BUCKET_NAME, file);
        try {
            ret = IMAGECLIENT.ocrBankCard(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:通用印刷体识别URL方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param url 图片地址
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrGeneralUrl(String url) {
        String ret = null;
        GeneralOcrRequest request = new GeneralOcrRequest(BUCKET_NAME, url);
        try {
            ret = IMAGECLIENT.generalOcr(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 方法描述:通用印刷体识别URL方式.
     * 创建时间:2018-12-19 12:52:27
     *
     * @param path 图片路径
     * @return the string
     * @author "lixingwu"
     */
    public static String ocrGeneralPath(String path) {
        String ret = null;
        File file = new File(path);
        GeneralOcrRequest request = new GeneralOcrRequest(BUCKET_NAME, file);
        try {
            ret = IMAGECLIENT.generalOcr(request);
        } catch (AbstractImageException e) {
            e.printStackTrace();
        }
        return ret;
    }
}

3、天气太热了

上面的工具类只是简单的说了一下,如果项目需要用到,得自己去测试,等我那天心情好了再说详细了点。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!