使用腾讯表格文字识别(TableOCR)

之前按照公司要求,根据表格图片读取订单内容,将订单内容进行保存数据库

一、网址搜索腾讯云平台

1.找到产品中的文字识别
2.操作方法按照腾讯提供的文字说明来
https://cloud.tencent.com/document/product/866/34936
3.点击控制台申请秘钥
在这里插入图片描述
二、代码实现部分,这里我抽成了一个工具类

1.调用腾讯接口util

package com.fansha.tencentcloudtest.domain.util;

import com.fansha.tencentcloudtest.domain.tencentcloudapi.v20181119.OcrClient;
import com.fansha.tencentcloudtest.domain.tencentcloudapi.v20181119.models.TableOCRRequest;
import com.fansha.tencentcloudtest.domain.tencentcloudapi.v20181119.models.TableOCRResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.util.List;

/**
 * @Author: FanSha
 * @Date: 2019/7/1 17:02
 * @Version 1.0
 * @Description:
 * @Copyright: Copyright (c) 2019
 * @Company: www.123start.cn
 */
public class TencentRequestUtil {

    public static byte[] invokeTransaction(String imgBase64) {
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        Credential cred = new Credential(Constant.TX_SECRET_ID, Constant.TX_SECRET_KEY);
        // 设置访问域名
        // SDK会自动指定域名。通常是不需要特地指定域名的,但是如果您访问的是金融区的服务,
        // 则必须手动指定域名,例如云服务器的上海金融区域名: tbaas.ap-shanghai-fsi.tencentcloudapi.com
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(Constant.TX_ENDPOINT);
        // 实例化tableOCR的client对象
        // 第二个参数是地域信息,根据资源所属地域填写相应的地域信息,比如广州地域的资源可以直接填写字符串ap-guangzhou,或者引用预设的常量
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        OcrClient client = new OcrClient(cred, Constant.TX_REGION, clientProfile);
        TableOCRRequest tableOCRRequest = new TableOCRRequest();
        tableOCRRequest.setImageBase64(imgBase64);
        TableOCRResponse tableOCRResponse = null;
        try {
            tableOCRResponse = client.TableOCR(tableOCRRequest);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        String imageBase = tableOCRResponse.getData().replaceAll("\r\n", "");
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes = new byte[0];
        try {
            bytes =  decoder.decodeBuffer(imageBase);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    public static void main(String[] args) {
        String imageBase64 = BASE64Util.encodeImgageToBase64(new File("c:\\Users\\fansha\\Desktop\\图片\\订单识别\\1563238347(1).png"));
        byte[] bytes = new byte[0];
        try {
            bytes = invokeTransaction(imageBase64);
            InputStream in = new ByteArrayInputStream(bytes);
            List<List<String>> list = ImportExcelUtil.readExcel(in, false);
            for (List<String> list1:list) {
                for (String s:list1) {
                    System.out.println(s);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2.读取返回excel内容util

package com.fansha.tencentcloudtest.domain.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: FanSha
 * @Date: 2019/7/17 14:27
 * @Version 1.0
 * @Description:
 * @Copyright: Copyright (c) 2019
 * @Company: www.123start.cn
 */
public class ImportExcelUtil {

    /**
     * 总行数
     */

    private static int totalRows = 0;

    /**
     * 总列数
     */

    private static int totalCells = 0;

    /**
     * 错误信息
     */

    private static String errorInfo;

    /**
     * 构造方法
     */

    public ImportExcelUtil() {
    }

    /**
     * @描述:得到总行数
     * @参数:@return
     * @返回值:int
     */
    public int getTotalRows() {
        return totalRows;
    }

    /**
     * @描述:得到总列数
     * @参数:@return
     * @返回值:int
     */
    public int getTotalCells() {
        return totalCells;
    }

    /**
     * @描述:得到错误信息
     * @参数:@return
     * @返回值:String
     */
    public String getErrorInfo() {
        return errorInfo;
    }

    /**
     * @描述:验证excel文件
     * @参数:@param filePath 文件完整路径
     * @参数:@return
     * @返回值:boolean
     */
    public static boolean validateExcel(String filePath) {
        /** 检查文件名是否为空或者是否是Excel格式的文件 */
        if (filePath == null || !(WDWUtil.isExcel2003(filePath) || WDWUtil.isExcel2007(filePath))) {
            errorInfo = "文件名不是excel格式";
            return false;
        }

        /** 检查文件是否存在 */
        File file = new File(filePath);
        if (file == null || !file.exists()) {
            errorInfo = "文件不存在";
            return false;
        }
        return true;
    }

    /**
     * @描述:判断excel文件是2007还是2003
     * @参数:@param filePath 文件完整路径
     * @参数:@return
     * @返回值:List
     */
    public static List<List<String>> booleanExcelType(String filePath) {
        List<List<String>> dataLst = new ArrayList<List<String>>();
        InputStream is = null;
        try {
            /** 验证文件是否合法 */
            if (!validateExcel(filePath)) {
                System.out.println(errorInfo);
                return null;
            }
            /** 判断文件的类型,是2003还是2007 */
            boolean isExcel2003 = true;
            if (WDWUtil.isExcel2007(filePath)) {
                isExcel2003 = false;
            }
            /** 调用本类提供的根据流读取的方法 */
            File file = new File(filePath);
            is = new FileInputStream(file);
            dataLst = readExcel(is, isExcel2003);
            is.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    is = null;
                    e.printStackTrace();
                }
            }
        }
        /** 返回最后读取的结果 */
        return dataLst;
    }

    /**
     * @描述:根据流读取Excel文件
     * @参数:@param inputStream
     * @参数:@param isExcel2003
     * @参数:@return
     * @返回值:List
     */
    public static List<List<String>> readExcel(InputStream inputStream, boolean isExcel2003) {
        List<List<String>> dataLst = null;
        try {
            /** 根据版本选择创建Workbook的方式 */
            Workbook wb = null;
            if (isExcel2003) {
                wb = new HSSFWorkbook(inputStream);
            } else {
                wb = new XSSFWorkbook(inputStream);
            }
            dataLst = readData(wb);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataLst;
    }

    /**
     * @描述:读取数据
     * @参数:@param Workbook
     * @参数:@return
     * @返回值:List<List<String>>
     */
    private static List<List<String>> readData(Workbook wb) {
        List<List<String>> dataLst = new ArrayList<List<String>>();
        /** 得到第一个shell */
        Sheet sheet = wb.getSheetAt(0);
        /** 得到Excel的行数 */
        totalRows = sheet.getPhysicalNumberOfRows();
        /** 得到Excel的列数 */
        if (totalRows >= 1 && sheet.getRow(0) != null) {
            totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
        }

        /** 循环Excel的行 */
        for (int r = 0; r < totalRows; r++) {
            Row row = sheet.getRow(r);
            if (row == null) {
                continue;
            }
            List<String> rowLst = new ArrayList<String>();
            /** 循环Excel的列 */
            for (int c = 0; c < new ImportExcelUtil().getTotalCells(); c++) {
                Cell cell = row.getCell(c);
                String cellValue = "";
                if (null != cell) {
                    // 以下是判断数据的类型
                    switch (cell.getCellType()) {
                        case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                            cellValue = cell.getNumericCellValue() + "";
                            break;
                        case HSSFCell.CELL_TYPE_STRING: // 字符串
                            cellValue = cell.getStringCellValue();
                            break;
                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                            cellValue = cell.getBooleanCellValue() + "";
                            break;
                        case HSSFCell.CELL_TYPE_FORMULA: // 公式
                            cellValue = cell.getCellFormula() + "";
                            break;
                        case HSSFCell.CELL_TYPE_BLANK: // 空值
                            cellValue = "";
                            break;
                        case HSSFCell.CELL_TYPE_ERROR: // 故障
                            cellValue = "非法字符";
                            break;
                        default:
                            cellValue = "未知类型";
                            break;
                    }
                }
                rowLst.add(cellValue);
            }
            /** 保存第r行的第c列 */
            dataLst.add(rowLst);
        }
        return dataLst;
    }
}

/**
 * @描述:工具类
 */
class WDWUtil {
    /**
     * @描述:是否是2003的excel,返回true是2003
     * @参数:@param filePath 文件完整路径
     * @参数:@return
     * @返回值:boolean
     */
    public static boolean isExcel2003(String filePath) {
        return filePath.matches("^.+\\.(?i)(xls)$");
    }

    /**
     * @描述:是否是2007的excel,返回true是2007
     * @参数:@param filePath 文件完整路径
     * @参数:@return
     * @返回值:boolean
     */
    public static boolean isExcel2007(String filePath) {
        return filePath.matches("^.+\\.(?i)(xlsx)$");
    }
}

三、总结

1.关于腾讯表格文字识别还是比较简单的,但是识别效果比较差,不建议识别插库!!!
参考:https://www.cnblogs.com/f-young/p/11024505.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,使用腾讯OCR进行文字识别需要以下步骤: 1. 注册并登录腾讯云账号,在控制台中开通腾讯OCR服务,获取API密钥。 2. 在代码中引入腾讯OCR SDK,并使用API密钥进行身份验证。 3. 准备要识别的图片,并将其转换为Base64格式。 4. 调用腾讯OCR的API,传入图片数据并设置识别类型(如通用文字识别、身份证识别等)。 5. 解析API返回的识别结果,获取识别出的文字内容。 以下是一个Python示例代码,用于使用腾讯OCR进行通用文字识别: ``` import base64 import json import requests # 设置密钥和API地址 app_id = "your_app_id" app_key = "your_app_key" api_url = "https://recognition.image.myqcloud.com/ocr/general" # 准备图片数据 with open('image.jpg', 'rb') as f: image_data = f.read() image_base64 = str(base64.b64encode(image_data), 'utf-8') # 构造请求参数 params = { "appid": app_id, "image": image_base64, "nonce_str": "random_string", "time_stamp": str(int(time.time())), } # 生成签名 sign_str = "&".join([f"{key}={params[key]}" for key in sorted(params.keys())]) sign_str += f"&appkey={app_key}" sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest().upper() # 发送POST请求 headers = {'Content-Type': 'application/json'} data = { "appid": app_id, "image": image_base64, "nonce_str": "random_string", "time_stamp": str(int(time.time())), "sign": sign, } response = requests.post(api_url, headers=headers, data=json.dumps(data)) # 解析结果 result = json.loads(response.text) if result.get("code") == 0: words_list = result.get("data").get("item_list") for words in words_list: print(words.get("itemstring")) else: print(result.get("message")) ``` 需要注意的是,使用腾讯OCR服务需要收取一定的费用,具体费用标准可以在腾讯云控制台中查看。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值