调用阿里云的通用文字识别-高精版识别接口,识别图片中的文字详解

Posted 秋9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用阿里云的通用文字识别-高精版识别接口,识别图片中的文字详解相关的知识,希望对你有一定的参考价值。

阿里云的通用文字识别-高精版识别接口地址:

通用文字识别 - 文字识别(OCR) - 阿里云

直接上代码

核心代码

package com.aliyun.sample;

import com.aliyun.tea.*;
import com.aliyun.ocr_api20210707.*;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.darabonba.stream.*;

public class Sample 

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret) throws Exception 
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    

    public static void main(String[] args_) throws Exception 
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.ocr_api20210707.Client client = Sample.createClient("client id", "client secrect");
        java.io.InputStream bodySyream = com.aliyun.darabonba.stream.Client.readFromFilePath("D:\\\\work\\\\doc\\\\1.jpg");
        RecognizeAdvancedRequest recognizeAdvancedRequest = new RecognizeAdvancedRequest()
                .setBody(bodySyream);
        // 复制代码运行请自行打印 API 的返回值
        RecognizeAdvancedResponse response= client.recognizeAdvanced(recognizeAdvancedRequest);
        System.out.println(response.getBody().getData());
    

pom.xml配置文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.aliyun</groupId>
  <artifactId>sample</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>
  <name>sample</name>
  <description>Alibaba Cloud SDK Code Sample for Java
    </description>
  <url>https://github.com/aliyun/alibabacloud-code-sample</url>
  <licenses>
    <license>
      <name></name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <id>aliyundeveloper</id>
      <name>Aliyun SDK</name>
      <email>aliyunsdk@aliyun.com</email>
    </developer>
  </developers>
  <distributionManagement>
    <snapshotRepository>
      <id>sonatype-nexus-snapshots</id>
      <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
    </snapshotRepository>
    <repository>
      <id>sonatype-nexus-staging</id>
      <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
    </repository>
  </distributionManagement>
  <scm>
    <connection></connection>
    <developerConnection></developerConnection>
    <url></url>
  </scm>
  <dependencies>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>ocr_api20210707</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>darabonba-stream</artifactId>
      <version>0.0.1</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea-openapi</artifactId>
      <version>0.2.0</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea-console</artifactId>
      <version>0.0.1</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea-util</artifactId>
      <version>0.2.13</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea</artifactId>
      <version>1.1.14</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>7</source>
          <target>7</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-gpg-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <id>sign-artifacts</id>
            <phase>verify</phase>
            <goals>
              <goal>sign</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.sonatype.plugins</groupId>
        <artifactId>nexus-staging-maven-plugin</artifactId>
        <version>1.6.3</version>
        <extensions>true</extensions>
        <configuration>
          <serverId>sonatype-nexus-staging</serverId>
          <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
          <autoReleaseAfterClose>true</autoReleaseAfterClose>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>3.1.1</version>
        <configuration>
          <encoding>UTF-8</encoding>
          <doclint>none</doclint>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4.1</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.aliyun.sample.Sample</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

运行后输出内容为:

 

以上是关于调用阿里云的通用文字识别-高精版识别接口,识别图片中的文字详解的主要内容,如果未能解决你的问题,请参考以下文章

RPA中房产证的 OCR 识别

阿里云Ocr识别封装

图像识别,输出文字

python调用百度ocr接口,实现图片内文字识别

如何用阿里云的API进行身份证识别

图片识别OCR: