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调用是有效的。
所以不知道故障出在哪里。请帮帮我!
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。
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));
}提前谢谢。
发布于 2018-12-25 10:30:23
可以用"base64Image".替换"Base64Image“这对我很管用
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 )https://stackoverflow.com/questions/51261876
复制相似问题