get-ocr-result.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (C) 2020 Tencent Cloud.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. 'use strict';
  17. const { request } = require('./utils');
  18. /**
  19. * 获取OCR识别结果
  20. * @param {object} params - 参数包装对象
  21. * @param {string} params.name - 对应OCR API的Action值 https://cloud.tencent.com/document/api/866/34938
  22. * @param {object} params.payload - API需要的参数
  23. * @param {string} [params.payload.ImageBase64] - 图片的 Base64 值。支持的图片格式:PNG、JPG、JPEG,暂不支持 GIF 格式。支持的图片大小:所下载图片经 Base64 编码后不超过 3M。图片下载时间不超过 3 秒。
  24. * @param {string} [params.payload.ImageUrl] - 图片的 Url 地址。支持的图片格式:PNG、JPG、JPEG,暂不支持 GIF 格式。支持的图片大小:所下载图片经 Base64 编码后不超过 3M。图片下载时间不超过 3 秒。
  25. * @returns {object} API返回的有效数据
  26. */
  27. async function getOcrResult(params) {
  28. if (!params.name) {
  29. throw new Error('缺少API Action参数');
  30. }
  31. if (!(params.payload.ImageBase64 || params.payload.ImageUrl)) {
  32. throw new Error('ImageUrl 和 ImageBase64 必须有一个不为空');
  33. }
  34. // 调用OCR识别接口
  35. try {
  36. const result = await request(params.name, params.payload);
  37. return result;
  38. } catch (e) {
  39. throw new Error(e);
  40. }
  41. }
  42. module.exports = getOcrResult;