reactjs - 无法将图像发送到azure API

标签 reactjs azure react-native ocr

我正在使用亚马逊制作 OCR 应用程序。我正在使用 native react 做的应用程序。我在发送数据时出现错误。

错误:

{ "code": "InvalidImageUrl", "requestId": "c495b0d7-a65a-4138-97a9-2b1cb25dced8", "message": "Image URL is badly formatted." }

为什么?我究竟做错了什么?代码:

// ...

  selectImage() {
    ImagePicker.showImagePicker(options, (response) => {
      if (response.didCancel) {
        console.log('User cancelled image picker');
      } else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
      } else if (response.customButton) {
        console.log('User tapped custom button: ', response.customButton);
      } else {
        const source = { uri: response.uri };
        this.setState({ imageSource: source });
    
        this.extractText(response.uri);
      }
    });
  }

  extractText = async (imageSource) => {

    // alert(imageSource)
  
    let subscriptionKey = ['CODE'];
    let endpoint = ['ENDPOINT']
    if (!subscriptionKey) { throw new Error('Set your environment variables for your subscription key and endpoint.'); }
    
    var uriBase = endpoint + "vision/v2.1/ocr";

    // Request parameters.
 

    // Display the image.
    var sourceImageUrl = imageSource;

    const data = new FormData();
    data.append(imageSource);

fetch(uriBase + "?" + {
        "language": "unk",
        "detectOrientation": "true",
    },
{
 
 method: 'POST',
 headers: 
 {
     'Content-Type': 'application/json',
     'Ocp-Apim-Subscription-Key': subscriptionKey,
 },
 body: '{"url": ' + '"' + data + '"}',


}).then((response) => response.json()).then((data) =>
{

  console.log(JSON.stringify(data, null, 2));

}).catch((error) =>
{
 console.log(error);

});
  };  
}

export default ImagePickerScreen;

最佳答案

根据您的代码,您的数据有问题,它应该是一个图像 URL,以便 Azure 版本服务可以访问它。我不太确定如何在自定义逻辑中获取数据。但无论如何,下面的代码片段是有效的,请尝试一下:

const data = 'https://stanstroage.blob.core.windows.net/container2/personPic.jpg';

let subscriptionKey = '<key>';
let endpoint = '<endpoint>';
if (!subscriptionKey) { throw new Error('Set your environment variables for your subscription key and endpoint.'); }

var uriBase = endpoint + "vision/v2.1/ocr";

fetch(uriBase + "?" + {
    "language": "unk",
    "detectOrientation": "true",
},
{
method: 'POST',
headers: 
    {
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': subscriptionKey,
    },
body: '{"url": ' + '"' + data + '"}',
}).then((response) => response.json()).then((data) =>
{
console.log(JSON.stringify(data, null, 2));
}).catch((error) =>
{
console.log(error);
});

结果: enter image description here

如果您想上传本 map 片,您应该使用application/octet-stream作为请求content-type并将图片内容缓冲区设置为请求正文。 您可以使用react-native-fs读取本地镜像内容并使用 buffer要获取图像内容缓冲区并将其发布到 Azure 端,请尝试下面的代码片段:

let subscriptionKey = '<key>';
let endpoint = '<endpoint>';
let fileUri = '<fileUri>';
let base64 =  await fs.readFile(fileUri, 'base64');
let data = Buffer.from(base64, 'base64');

console.log(data);

if (!subscriptionKey) { throw new Error('Set your environment variables for your subscription key and endpoint.'); }

var uriBase = endpoint + "vision/v2.1/ocr";

fetch(uriBase + "?" + {
    "language": "unk",
    "detectOrientation": "true",
},
{
method: 'POST',
headers: 
    {
    'Content-Type': 'application/octet-stream',
    'Ocp-Apim-Subscription-Key': subscriptionKey,
    },
body: data,
}).then((response) => response.json()).then((data) =>
{
console.log(JSON.stringify(data, null, 2));
}).catch((error) =>
{
console.log(error);
});

结果:

enter image description here

关于reactjs - 无法将图像发送到azure API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59164983/

相关文章:

azure - 在 EC2 与 Windows Azure 上部署项目

react-native - Windows 8.1 上的错误 : Command failed: gradlew. bat installDebug

ios - CDN:运行 "pod install"时,某些 React Native 项目无法下载主干 URL?

javascript - 当你使用 set 函数时,React 会克隆你的对象吗?

javascript - React : Uncaught Invariant Violation: ReactDOM. render():无效的组件元素

javascript - 带有 preventDefault 的 React/Unit Test (Jest) 单击方法

java - 以编程方式访问 React Native 中的“主页”按钮

reactjs - Material UI slider - 标签格式

c# - 无法加载文件或程序集“System.Runtime.InteropServices.RuntimeInformation”

c# - 缩小 Azure Web 应用程序时打开套接字会发生什么情况?