AliyunOcr.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Library;
  6. using Tea;
  7. using Tea.Utils;
  8. namespace MySystem
  9. {
  10. public class AliyunOcr
  11. {
  12. private static string AccessKeyID = "LTAI5tBNY8CzWquEUx8HGqUX";
  13. private static string AccessKeySecret = "VOGf7ibdydKeKqkb2BfEwp7Nh4gPRJ";
  14. /**
  15. * 使用AK&SK初始化账号Client
  16. * @return Client
  17. * @throws Exception
  18. */
  19. public static AlibabaCloud.SDK.Ocr_api20210707.Client CreateClient()
  20. {
  21. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  22. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378671.html。
  23. AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
  24. {
  25. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  26. AccessKeyId = AccessKeyID,
  27. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  28. AccessKeySecret = AccessKeySecret,
  29. };
  30. // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
  31. config.Endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
  32. return new AlibabaCloud.SDK.Ocr_api20210707.Client(config);
  33. }
  34. public static string Check(string Url)
  35. {
  36. string result = "";
  37. AlibabaCloud.SDK.Ocr_api20210707.Client client = CreateClient();
  38. AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeBusinessLicenseRequest recognizeBusinessLicenseRequest = new AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeBusinessLicenseRequest
  39. {
  40. Url = Url,
  41. };
  42. AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
  43. try
  44. {
  45. // 复制代码运行请自行打印 API 的返回值
  46. var res = client.RecognizeBusinessLicenseWithOptions(recognizeBusinessLicenseRequest, runtime);
  47. result = res.Body.Data;
  48. }
  49. catch (TeaException error)
  50. {
  51. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  52. // 错误 message
  53. function.WriteLog(error.Message, "OCR异常");
  54. // 诊断地址
  55. function.WriteLog(error.Data["Recommend"].ToString(), "OCR异常");
  56. AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
  57. }
  58. catch (Exception _error)
  59. {
  60. TeaException error = new TeaException(new Dictionary<string, object>
  61. {
  62. { "message", _error.Message }
  63. });
  64. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  65. // 错误 message
  66. function.WriteLog(error.Message, "OCR异常");
  67. // 诊断地址
  68. function.WriteLog(error.Data["Recommend"].ToString(), "OCR异常");
  69. AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
  70. }
  71. return result;
  72. }
  73. }
  74. }