简体   繁体   English

从jquery .ajax到新的ajax调用的图像数据响应?

[英]image data response from jquery .ajax into new ajax call?

I'm trying to chain together the ImageOptim API with the OCR.space API . 我正在尝试将ImageOptim APIOCR.space API链接在一起。 Both great API's by the way, I highly recommend them! 顺便提一下,这两个很棒的API都非常值得推荐! The issue at hand though is that the OCR api does not accept images over 1 mb or 2600x2600 px in the free tier and thus many sources will need to be optimised before being sent. 但是,当前的问题是OCR api在免费层中不接受超过1 mb或2600x2600 px的图像,因此在发送之前,许多源都需要进行优化。

Im running this jQuery ajax call to ImageOptim from a cordova wrapped html file: 我从科尔多瓦包装的html文件运行对ImageOptim的jQuery ajax调用:

var compress = function(image) {
console.log("starting compress");
$.ajax({
    url: "https://im2.io/eX4mp1E4pI/2600x2600,quality=low",
    method: "POST",
    data: {
        file: image
    },
    processData: false,
    contentType: false,
    crossDomain: true
}).done(function(res) {
    window.compressedImg = res;
    formData.append("file", res);
    runOCR();
}).fail(function(jqXHR, textStatus) {
    console.log("Request failed: " + textStatus);
});
}; 

Please note: 请注意:

  1. this (in my experience), will fail in the browser due to cross domain calls being blocked in the browser but not from cordova. (以我的经验),由于跨域调用在浏览器中被阻止,而不是在科尔多瓦,在浏览器中将失败。
  2. OCR compatible compression not added in yet (but would require a file size as well as dimension argument) 尚未添加与OCR兼容的压缩(但需要文件大小以及维度参数)

The output from this call is a raw png as a string, ie what you get when you open a .png file in a text editor. 该调用的输出是作为字符串的原始png,即在文本编辑器中打开.png文件时得到的结果。 I've tried loads of ways to handle this but cannot understand how to use this data in the next ajax call (below), does it need to be saved to disk and then uploaded, if so - how? 我已经尝试了许多方法来处理此问题,但无法理解如何在下一个ajax调用(如下)中使用此数据,是否需要将其保存到磁盘然后上传,如果这样-怎么做? (because I tried writing it to localstorage but it would still be treated as a string). (因为我尝试将其写入localstorage,但仍将其视为字符串)。

The OCR.space call; OCR.space调用;

var formData = new FormData();
formData.append("language", "MYLANGUAGE");
formData.append("apikey", "MYAPIKEY");
formData.append("isOverlayRequired", false);
function runOCR2() {
jQuery.ajax({
    url: 'https://api.ocr.space/parse/image',
    data: formData,
    dataType: 'form/multipart',
    cache: false,
    contentType: false,
    processData: false,
    method: 'POST',
    success: function(ocrParsedResult) {
        console.log(ocrParsedResult);
    }
});
}

Please note; 请注意; Vars are not set here but I keep them together in this question for clarity. 此处未设置变量,但为了清楚起见,在此问题中将它们放在一起。

The response from this call is: 此调用的响应为:

responseText: "{\"ParsedResults\":null,\"OCRExitCode\":99,\"IsErroredOnProcessing\":true,\"ErrorMessage\":\"No file uploaded or UR…"

ie the call works but the image parameter is not a valid image. 也就是说,该调用有效,但是image参数不是有效的图像。

Any ideas on how to trea the returned string so that it is readable as an image for the next api call? 关于如何处理返回的字符串,以便将其作为下一个api调用的图像可读的任何想法?

Usually when you are uploading files using formData you just pass file reference like form.append('myfile',$("#fileInput").files[0]) and browser handles the encoding stuff behind the screens .It manually converts file to byte-stream and prepares appropriate boundary to help server distinguish where image begins and ends 通常,当您使用formData上传文件时,您只需传递诸如form.append('myfile',$("#fileInput").files[0])类的文件引用,浏览器就会处理屏幕后面的编码内容。它会手动将文件转换为字节流并准备适当的边界以帮助服务器区分图像的开始和结束位置

but here scenario is different you don't have the file bound to any physical file control instead its created dynamically and you get a bytestream of that .To account for the above fact you need to tell browser explicitly that it's a independent raw binary stuff and should be treated as such 但是这里的情况有所不同,您没有将文件绑定到任何物理文件控件,而是动态创建了该文件,并获得了该字节流。要解决上述事实,您需要明确告诉浏览器这是一个独立的原始二进制文件,应该这样对待

A Blob object represents a file-like object of immutable, raw data. Blob对象代表不可变的原始数据的类似文件的对象。 Blobs represent data that isn't necessarily in a JavaScript-native format. Blob表示的数据不一定是JavaScript原生格式。

var blob = new Blob([res], {type : 'image/png'}); //res is the converted image ImageOptim API

var formData = new FormData();
var fileName = 'myimage.png';    //filename that server will see it as
formData.append('anything', blob, fileName);
formData.append("language", "MYLANGUAGE");
formData.append("apikey", "MYAPIKEY");
formData.append("isOverlayRequired", false);

function runOCR2() {
    $.ajax({
      url: "https://api.ocr.space/parse/image",
      type: "POST",
      cache: false,
      contentType: false,
      processData: false,
      data: formData,
      success: function(response){alert(response);}
    }); 
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM