文字识别服务代码示例(java版本)
引导式阅读
Java
文字识别服务代码示例(java版本)
作者
C***
上架时间
2023-11-13 06:18:23

文字识别服务示例(java版本)

0.版本说明

本示例基于华为云SDK V3.0版本开发

1.简介

华为云提供了文字识别服务端SDK,您可以直接集成服务端SDK来调用文字识别服务的相关API,从而实现对文字识别服务的快速操作。

该示例展示了如何通过java版SDK实现文字识别。

2.开发前准备

  • 注册 华为云,并完成 实名认证
  • 已订阅文字识别服务。
  • 已具备开发环境,支持Java JDK 1.8及其以上版本。
  • 已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥

3.安装SDK

您可以通过Maven配置所依赖的文字识别服务SDK

<dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-core</artifactId> <version>3.0.73</version> </dependency> <dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-ocr</artifactId> <version>3.0.73</version> </dependency>

4. 开始使用

4.1 导入依赖模块

import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.ocr.v1.region.OcrRegion; import com.huaweicloud.sdk.ocr.v1.*; import com.huaweicloud.sdk.ocr.v1.model.*;

4.2 初始化认证信息

public static ICredential getCredential(String ak, String sk) { return new BasicCredentials().withAk(ak).withSk(sk); }

相关参数说明如下所示:

  • ak:华为云账号Access Key。
  • sk:华为云账号Secret Access Key 。

4.3 初始化文字识别服务的客户端

public static OcrClient getClient(Region region, ICredential auth) { return OcrClient.newBuilder().withCredential(auth).withRegion(region).build(); }

相关参数说明如下所示:

service region: 服务所在区域,例如:

  • CN_NORTH_1 北京一
  • CN_NORTH_4 北京四
  • CN_EAST_3 上海一
  • CN_SOUTH_1 华南广州

5. SDK demo代码解析

5.1 通用文字识别

private static void generalText(OcrClient ocrClient, String image) { RecognizeGeneralTextRequest recognizeGeneralTextRequest = new RecognizeGeneralTextRequest(); GeneralTextRequestBody requestBody = new GeneralTextRequestBody(); requestBody.setImage(image); recognizeGeneralTextRequest.setBody(requestBody); try { RecognizeGeneralTextResponse textResponse = ocrClient.recognizeGeneralText(recognizeGeneralTextRequest); System.out.println(textResponse.getResult()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.2 通用表格识别

private static void generalTable(OcrClient ocrClient, String image) { RecognizeGeneralTableRequest request = new RecognizeGeneralTableRequest(); GeneralTableRequestBody requestBody = new GeneralTableRequestBody(); requestBody.withImage(image); request.withBody(requestBody); try { RecognizeGeneralTableResponse response = ocrClient.recognizeGeneralTable(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.3 身份证识别

private static void idCard(OcrClient ocrClient, String image) { RecognizeIdCardRequest request = new RecognizeIdCardRequest(); IdCardRequestBody body = new IdCardRequestBody(); body.withReturnVerification(true); body.withSide("front"); body.withImage(image); request.withBody(body); try { RecognizeIdCardResponse response = ocrClient.recognizeIdCard(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.4 银行卡识别

private static void bankcard(OcrClient ocrClient, String image) { RecognizeBankcardRequest recognizeBankcardRequest = new RecognizeBankcardRequest(); BankcardRequestBody bankcardRequestBody = new BankcardRequestBody(); bankcardRequestBody.setImage(image); recognizeBankcardRequest.setBody(bankcardRequestBody); try { RecognizeBankcardResponse response = ocrClient.recognizeBankcard(recognizeBankcardRequest); System.out.println(response); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.5 智能分类识别

private static void autoClassification(OcrClient ocrClient, String image) { RecognizeAutoClassificationRequest request = new RecognizeAutoClassificationRequest(); AutoClassificationRequestBody requestBody = new AutoClassificationRequestBody(); requestBody.withImage(image); request.setBody(requestBody); try { RecognizeAutoClassificationResponse response = ocrClient.recognizeAutoClassification(request); System.out.println(response); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.6 增值税发票识别

private static void vatInvoice(OcrClient ocrClient, String image) { RecognizeVatInvoiceRequest request = new RecognizeVatInvoiceRequest(); VatInvoiceRequestBody body = new VatInvoiceRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeVatInvoiceResponse response = ocrClient.recognizeVatInvoice(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.7 定额发票识别

private static void quotaInvoice(OcrClient ocrClient, String image) { RecognizeQuotaInvoiceRequest request = new RecognizeQuotaInvoiceRequest(); QuotaInvoiceRequestBody body = new QuotaInvoiceRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeQuotaInvoiceResponse response = ocrClient.recognizeQuotaInvoice(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.8 手写文字识别

private static void handwriting(OcrClient ocrClient, String image) { RecognizeHandwritingRequest request = new RecognizeHandwritingRequest(); HandwritingRequestBody body = new HandwritingRequestBody(); body.withDetectDirection(true); body.withQuickMode(true); body.withImage(image); request.withBody(body); try { RecognizeHandwritingResponse response = ocrClient.recognizeHandwriting(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.9 行驶证识别

private static void vehicleLicense(OcrClient ocrClient, String image) { RecognizeVehicleLicenseRequest request = new RecognizeVehicleLicenseRequest(); VehicleLicenseRequestBody body = new VehicleLicenseRequestBody(); body.withReturnIssuingAuthority(true); body.withImage(image); request.withBody(body); try { RecognizeVehicleLicenseResponse response = ocrClient.recognizeVehicleLicense(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.10 道路运输证识别

private static void transportationLicense(OcrClient ocrClient, String image) { RecognizeTransportationLicenseRequest request = new RecognizeTransportationLicenseRequest(); TransportationLicenseRequestBody body = new TransportationLicenseRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeTransportationLicenseResponse response = ocrClient.recognizeTransportationLicense(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.11 出租车发票识别

private static void taxiInvoice(OcrClient ocrClient, String image) { RecognizeTaxiInvoiceRequest request = new RecognizeTaxiInvoiceRequest(); TaxiInvoiceRequestBody body = new TaxiInvoiceRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeTaxiInvoiceResponse response = ocrClient.recognizeTaxiInvoice(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.12 车辆通行费发票识别

private static void tollInvoice(OcrClient ocrClient, String image) { RecognizeTollInvoiceRequest request = new RecognizeTollInvoiceRequest(); TollInvoiceRequestBody body = new TollInvoiceRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeTollInvoiceResponse response = ocrClient.recognizeTollInvoice(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.13 机动车销售发票识别

private static void mvsInvoice(OcrClient ocrClient, String image) { RecognizeMvsInvoiceRequest request = new RecognizeMvsInvoiceRequest(); MvsInvoiceRequestBody body = new MvsInvoiceRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeMvsInvoiceResponse response = ocrClient.recognizeMvsInvoice(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.14 车牌识别

private static void licensePlate(OcrClient ocrClient, String image) { RecognizeLicensePlateRequest request = new RecognizeLicensePlateRequest(); LicensePlateRequestBody body = new LicensePlateRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeLicensePlateResponse response = ocrClient.recognizeLicensePlate(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.15 飞机行程单识别

private static void flightItinerary(OcrClient ocrClient, String image) { RecognizeFlightItineraryRequest request = new RecognizeFlightItineraryRequest(); FlightItineraryRequestBody body = new FlightItineraryRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeFlightItineraryResponse response = ocrClient.recognizeFlightItinerary(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.16 营业执照识别

private static void businessLicense(OcrClient ocrClient, String image) { RecognizeBusinessLicenseRequest request = new RecognizeBusinessLicenseRequest(); BusinessLicenseRequestBody body = new BusinessLicenseRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeBusinessLicenseResponse response = ocrClient.recognizeBusinessLicense(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.17 网络图片识别

private static void webImage(OcrClient ocrClient, String image) { RecognizeWebImageRequest request = new RecognizeWebImageRequest(); WebImageRequestBody body = new WebImageRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeWebImageResponse response = ocrClient.recognizeWebImage(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.18 驾驶证识别

private static void driverLicense(OcrClient ocrClient, String image) { RecognizeDriverLicenseRequest request = new RecognizeDriverLicenseRequest(); DriverLicenseRequestBody body = new DriverLicenseRequestBody(); body.withReturnIssuingAuthority(true); body.withSide("front"); body.withImage(image); request.withBody(body); try { RecognizeDriverLicenseResponse response = ocrClient.recognizeDriverLicense(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.19 名片识别

private static void businessCard(OcrClient ocrClient, String image) { RecognizeBusinessCardRequest request = new RecognizeBusinessCardRequest(); BusinessCardRequestBody body = new BusinessCardRequestBody(); body.withDetectDirection(true); body.withImage(image); request.withBody(body); try { RecognizeBusinessCardResponse response = ocrClient.recognizeBusinessCard(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.20 火车票识别

private static void trainTicket(OcrClient ocrClient, String image) { RecognizeTrainTicketRequest request = new RecognizeTrainTicketRequest(); TrainTicketRequestBody body = new TrainTicketRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeTrainTicketResponse response = ocrClient.recognizeTrainTicket(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.21 VIN码识别

private static void vin(OcrClient ocrClient, String image) { RecognizeVinRequest request = new RecognizeVinRequest(); VinRequestBody body = new VinRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeVinResponse response = ocrClient.recognizeVin(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.22 护照识别

private static void passport(OcrClient ocrClient, String image) { RecognizePassportRequest request = new RecognizePassportRequest(); PassportRequestBody body = new PassportRequestBody(); body.withImage(image); request.withBody(body); try { RecognizePassportResponse response = ocrClient.recognizePassport(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.23 保险单识别

private static void insurancePolicy(OcrClient ocrClient, String image) { RecognizeInsurancePolicyRequest request = new RecognizeInsurancePolicyRequest(); InsurancePolicyRequestBody body = new InsurancePolicyRequestBody(); body.withDetectDirection(true); body.withImage(image); request.withBody(body); try { RecognizeInsurancePolicyResponse response = ocrClient.recognizeInsurancePolicy(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

5.24 从业资格证识别

private static void qualificationCertificate(OcrClient ocrClient, String image) { RecognizeQualificationCertificateRequest request = new RecognizeQualificationCertificateRequest(); QualificationCertificateRequestBody body = new QualificationCertificateRequestBody(); body.withImage(image); request.withBody(body); try { RecognizeQualificationCertificateResponse response = ocrClient.recognizeQualificationCertificate(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException e) { LOGGER.error(e.toString()); } catch (ServiceResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getErrorCode()); LOGGER.error(e.getErrorMsg()); } }

6.参考

更多信息请参考文字识别服务

7.修订记录

发布日期 文档版本 修订说明
2021-12-25 1.0 文档首次发布