首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试在react本机中通过OCR space API提取文本,但收到错误,因为base64格式不正确。

尝试在react本机中通过OCR space API提取文本,但收到错误,因为base64格式不正确。
EN

Stack Overflow用户
提问于 2018-07-10 17:37:55
回答 1查看 461关注 0票数 0
代码语言:javascript
运行
复制
After clicking an image through android mobile and extracting base64 from the image,  I am sending it to the OCR space API to extract the text in the image. 

getTextByURL会在收到base64后调用该接口。

格式为data:image/png;base64,iVBORw0KGgoAAAANS...

为了获得格式,我将base64代码附加到'data:image/png;base64‘,它应该满足所需的POST调用条件。

但是错误仍然存在,我找不到错误是在格式中还是在base64代码中。

当我在POST调用中给出一个图像的base64代码,而不是从点击的图像生成的base64时,API调用是有效的。

所以不知道故障出在哪里。请帮帮我!

代码语言:javascript
运行
复制
getTextByUrl(base) {
    var url = "https://api.ocr.space/parse/image";
    var subscriptionKey = "xxxxxxxxxxxxxx";
    var data = new FormData();
    data.append("apikey", subscriptionKey);
    data.append("language", "eng");
    data.append("isOverlayRequired", "true");
    data.append("Base64Image", base)
    var xhr = new XMLHttpRequest();
    xhr.withCredentials = true;

    xhr.addEventListener("readystatechange", function () {
      if (this.readyState === 4) {
        alert(this.responseText);
      }
    });

    xhr.open("POST", "URL");
    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.send(data);

  }

  showCamera() {
    this.setState({
      showComponent: true,
      uri: ""
    })
  }

以下函数用于单击图像并从图像中提取base64。

代码语言:javascript
运行
复制
  takePicture() {
    const options = {};
    //options.location = ...
    this.camera.capture({ metadata: options })
      .then((data) => {
        let pathToImage = data.path;

        CompressImage.createCompressedImage(pathToImage, 'compress/images').then(

          ImgToBase64.getBase64String(pathToImage)
            .then(base64String => {
              **base64String = "data:image/jpg;base64," + base64String;**//This gives the base64 of the image

              alert(base64String.substring(0, 50) + typeof (base64String));


              this.setState({
                baa: base64String.substring(0, 200)
              })
              this.getTextByUrl(base64String)
            })
            .catch(err => alert(err))
        )


      })
      .catch(err => console.error(err));
  }

提前谢谢。

EN

Stack Overflow用户

发布于 2018-12-25 10:30:23

可以用"base64Image".替换"Base64Image“这对我很管用

代码语言:javascript
运行
复制
const data = await this.camera.takePictureAsync(options);
  const base64Str = 'data:image/jpg;base64,'+data.base64; var data = new FormData();  
data.append('apikey', key)
data.append('base64Image', base64)

const headers = { 
  'Accept': 'application/json',
  'Content-Type': 'multipart/form-data;',
 }
const config = { 
  method: 'POST', 
  headers,
  body: data
 };
const URL = 'https://api.ocr.space/parse/image';

fetch(URL, config )
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51261876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档