雲計算

阿里雲視覺智能開放平臺–文字識別使用教程

Step By Step

1、服務開通,參考鏈接:阿里雲視覺智能開放平臺使用簡明教程

2、目前文字識別提供5大類:個人類卡證識別、資產類證件識別、通用文字類識別、車輛交通類識別和車輛交通類識別,共21個API的接口能力

3、Code Sample

增值稅發票識別接口為例分別演示使用本地圖片及OSS圖片的使用;

其它接口的使用方式類似,注意目前:二維碼識別RecognizeQrCode API不支持上傳本地文件,但是支持公網圖片URL,其它API接口支持OSS地址和本地圖片上傳。


  • 3.1 pom.xml
    <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>ocr20191230</artifactId>
            <version>0.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.52</version>
        </dependency>
    </dependencies>

3.2 Java Code

import com.alibaba.fastjson.JSON;
import com.aliyun.ocr20191230.Client;
import com.aliyun.ocr20191230.models.RecognizeVATInvoiceAdvanceRequest;
import com.aliyun.ocr20191230.models.RecognizeVATInvoiceRequest;
import com.aliyun.ocr20191230.models.RecognizeVATInvoiceResponse;
import com.aliyun.tearpc.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class RecognizeVATInvoiceDemo {

    public static void main(String[] args) throws Exception {

        // accessKeyId、accessKeySecret 獲取參考:https://yq.aliyun.com/articles/693979
        Config config = new Config();
        config.accessKeyId= "LTAIOZZg********";
        config.accessKeySecret= "v7CjUJCMk7j9aK****************";
        config.regionId="cn-shanghai";
        config.protocol = "https";
        config.endpoint="ocr.cn-shanghai.aliyuncs.com";

        Client client = new Client(config);
        String filePath = "C:\\Users\\Administrator\\Desktop\\2019041500152001020003743286_0.jpg";
        String fileURL = "https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/ant_ai/vat_invoice/2019041500152001020003743286_0.jpg";

        recognizeVATInvoiceAdvance(client, filePath);
        recognizeVATInvoice(client, fileURL);
    }

    /**
     * 增值稅發票識別--使用本地圖片
     * @param client
     * @param filePath 本地圖片的路徑
     */
    public static void recognizeVATInvoiceAdvance(Client client, String filePath)
    {
        RecognizeVATInvoiceAdvanceRequest recognizeVATInvoiceAdvanceRequest = new RecognizeVATInvoiceAdvanceRequest();

        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // 傳遞請求參數
        recognizeVATInvoiceAdvanceRequest.fileURLObject = inputStream;
        recognizeVATInvoiceAdvanceRequest.fileType = "jpg";

        try {
            RecognizeVATInvoiceResponse recognizeVATInvoiceResponse = client.recognizeVATInvoiceAdvance(recognizeVATInvoiceAdvanceRequest, new RuntimeOptions());
            System.out.println(JSON.toJSONString(recognizeVATInvoiceResponse));// 輸出請求結果
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 增值稅發票識別--使用OSS圖片
     * @param client
     * @param fileURL OSS 圖片URL
     */
    public static void recognizeVATInvoice(Client client, String fileURL)
    {
        RecognizeVATInvoiceRequest recognizeVATInvoiceRequest = new RecognizeVATInvoiceRequest();

        recognizeVATInvoiceRequest.fileType = "jpg";
        recognizeVATInvoiceRequest.fileURL = fileURL;

        try {
            RecognizeVATInvoiceResponse recognizeVATInvoiceResponse = client.recognizeVATInvoice(recognizeVATInvoiceRequest,new RuntimeOptions());
            System.out.println(JSON.toJSONString(recognizeVATInvoiceResponse));// 輸出請求結果
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 3.3 Result
{"data":{"box":{"checkers":[589.0,1003.0,662.0,1033.0],"clerks":[986.0,1003.0,1060.0,1033.0],"invoiceAmounts":[1364.0,776.0,1438.0,804.0],"invoiceCodes":[1378.0,41.0,1520.0,68.0],"invoiceDates":[1376.0,115.0,1596.0,145.0],"invoiceFakeCodes":[1376.0,153.0,1640.0,181.0],"invoiceNoes":[1377.0,78.0,1478.0,105.0],"payeeAddresses":[355.0,909.0,734.0,939.0],"payeeBankNames":[354.0,947.0,938.0,977.0],"payeeNames":[356.0,833.0,633.0,865.0],"payeeRegisterNoes":[356.0,873.0,571.0,902.0],"payees":[189.0,1003.0,264.0,1033.0],"payerAddresses":[0.0,0.0,0.0,0.0],"payerBankNames":[0.0,0.0,0.0,0.0],"payerNames":[354.0,222.0,700.0,255.0],"payerRegisterNoes":[358.0,262.0,567.0,290.0],"sumAmounts":[532.0,774.0,629.0,805.0],"taxAmounts":[1606.0,721.0,1658.0,748.0],"withoutTaxAmounts":[1265.0,721.0,1339.0,749.0]},"content":{"antiFakeCode":"02702870934284730434","checker":"趙彎彎","clerk":"趙彎彎","invoiceAmount":"200.00","invoiceCode":"031001600311","invoiceDate":"20190415","invoiceNo":"80625433","payee":"趙彎彎","payeeAddress":"上海虹橋機場迎賓二路161號22342185","payeeBankName":"上海浦東發展銀行空港支行076389-98910158000000030-22","payeeName":"上海機場(集團)有限公司","payeeRegisterNo":"91310000132284295X","payerAddress":"","payerBankName":"","payerName":"百特醫療用品貿易(上海)有限公司","payerRegisterNo":"91310000607402073L","sumAmount":"200.00","taxAmount":"9.52","withoutTaxAmount":"190.48"}},"requestId":"75E88483-753C-4D5E-9EF5-5E132FF67DED"}
{"data":{"box":{"checkers":[589.0,1003.0,662.0,1033.0],"clerks":[986.0,1003.0,1060.0,1033.0],"invoiceAmounts":[1364.0,776.0,1438.0,804.0],"invoiceCodes":[1378.0,41.0,1520.0,68.0],"invoiceDates":[1376.0,115.0,1596.0,145.0],"invoiceFakeCodes":[1376.0,153.0,1640.0,181.0],"invoiceNoes":[1377.0,78.0,1478.0,105.0],"payeeAddresses":[355.0,909.0,734.0,939.0],"payeeBankNames":[354.0,947.0,938.0,977.0],"payeeNames":[356.0,833.0,633.0,865.0],"payeeRegisterNoes":[356.0,873.0,571.0,902.0],"payees":[189.0,1003.0,264.0,1033.0],"payerAddresses":[0.0,0.0,0.0,0.0],"payerBankNames":[0.0,0.0,0.0,0.0],"payerNames":[354.0,222.0,700.0,255.0],"payerRegisterNoes":[358.0,262.0,567.0,290.0],"sumAmounts":[532.0,774.0,629.0,805.0],"taxAmounts":[1606.0,721.0,1658.0,748.0],"withoutTaxAmounts":[1265.0,721.0,1339.0,749.0]},"content":{"antiFakeCode":"02702870934284730434","checker":"趙彎彎","clerk":"趙彎彎","invoiceAmount":"200.00","invoiceCode":"031001600311","invoiceDate":"20190415","invoiceNo":"80625433","payee":"趙彎彎","payeeAddress":"上海虹橋機場迎賓二路161號22342185","payeeBankName":"上海浦東發展銀行空港支行076389-98910158000000030-22","payeeName":"上海機場(集團)有限公司","payeeRegisterNo":"91310000132284295X","payerAddress":"","payerBankName":"","payerName":"百特醫療用品貿易(上海)有限公司","payerRegisterNo":"91310000607402073L","sumAmount":"200.00","taxAmount":"9.52","withoutTaxAmount":"190.48"}},"requestId":"9B97F14B-0970-45C7-BE9E-CD3204BB3E1B"}

參考鏈接

文字識別介紹
阿里雲視覺智能開放平臺使用簡明教程

Leave a Reply

Your email address will not be published. Required fields are marked *