简体   繁体   English

如何将base64图像发送到Microsoft-ocr api?

[英]How do I send a base64 image to Microsoft-ocr api?

I'm trying to use Microsoft Azure OCR API service to extract some text from an image. 我正在尝试使用Microsoft Azure OCR API服务从图像中提取一些文本。

The image I have for sending to the API service has a "data:image/png; base64, " structure and therefore I can't send it with content-type "application/json". 我发送给API服务的图像有一个“data:image / png; base64”结构,因此我不能用内容类型“application / json”发送它。

I tried sending it with content-type "multipart/form-data" or "application/octet-stream", but it also fails... 我尝试使用内容类型“multipart / form-data”或“application / octet-stream”发送它,但它也失败了......

// this "url" gives me the "data:data:image/png;base64, " code
var sourceImageUrl = document.getElementById("myImage").src;

    // Perform the REST API call.
    $.ajax({
        url: uriBase + "?" + $.param(params),

        // Request headers.
        beforeSend: function(jqXHR){
            jqXHR.setRequestHeader("Content-Type","multipart/form-data");
            jqXHR.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
        },

        type: "POST",

        // Request body.
        data: [sourceImageUrl]
    })

    .done(function(data) {
        // Show formatted JSON on webpage.
        $("#responseTextArea").val(JSON.stringify(data, null, 2));
    })

    .fail(function(jqXHR, textStatus, errorThrown) {
        // Display error message.
        var errorString = (errorThrown === "") ?
            "Error. " : errorThrown + " (" + jqXHR.status + "): ";
        errorString += (jqXHR.responseText === "") ? "" :
            (jQuery.parseJSON(jqXHR.responseText).message) ?
                jQuery.parseJSON(jqXHR.responseText).message :
                jQuery.parseJSON(jqXHR.responseText).error.message;
        alert(errorString);
    });

I am bit confused about how I should be sending the image or if I should do some transformations. 我对如何发送图像或者是否应该进行一些转换感到有点困惑。

Which content-type should I be using to do a proper request? 我应该使用哪种内容类型来做出正确的请求? Should I change the encoding of the image source? 我应该更改图像源的编码吗? How? 怎么样?

Thank you all! 谢谢你们!

I finally got it working by adding a makeBlob function that returns a blob out of a base64 code. 我终于通过添加一个makeBlob函数来工作,该函数从base64代码返回一个blob。 I also set the content-type to "application/octet-stream". 我还将content-type设置为“application / octet-stream”。

Final code looks like this: 最终代码如下所示:

    function makeblob(b64Data, contentType, sliceSize) {
     contentType = contentType || '';
     sliceSize = sliceSize || 512;

     var byteCharacters = atob(b64Data);
     var byteArrays = [];

     for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
         var slice = byteCharacters.slice(offset, offset + sliceSize);

         var byteNumbers = new Array(slice.length);
         for (var i = 0; i < slice.length; i++) {
             byteNumbers[i] = slice.charCodeAt(i);
         }

         var byteArray = new Uint8Array(byteNumbers);

         byteArrays.push(byteArray);
     }

     var blob = new Blob(byteArrays, { type: contentType });
     return blob;
 }


function recognizeText() {
    imageToSend = image.src;

    binDataImage = imageToSend.replace("data:image/png;base64,","");

        // Perform the REST API call.
        $.ajax({
            url: uriBase + "?" + $.param(params),

            // Request headers.
            beforeSend: function(jqXHR){
                jqXHR.setRequestHeader("Content-Type","application/octet-stream");
                jqXHR.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
            },

            type: "POST",

            // Request body.
            data: makeblob(binDataImage, 'image/jpeg'),
            cache: false,
            processData: false
        })

        .done(function(data) {
            // Show formatted JSON on webpage.
            $("#responseTextArea").val(JSON.stringify(data, null, 2));
        })

        .fail(function(jqXHR, textStatus, errorThrown) {
            // Display error message.
            var errorString = (errorThrown === "") ?
                "Error. " : errorThrown + " (" + jqXHR.status + "): ";
            errorString += (jqXHR.responseText === "") ? "" :
                (jQuery.parseJSON(jqXHR.responseText).message) ?
                    jQuery.parseJSON(jqXHR.responseText).message :
                    jQuery.parseJSON(jqXHR.responseText).error.message;
            alert(errorString);
        });
  };

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

相关问题 如何base64编码从Ionic2内的API接收的图像blob? - How do I base64 encode an image blob received from an API inside Ionic2? 在将图像作为Base64 Xamarin表单发送到API之前将其翻转 - flip image in before send it to API as Base64 Xamarin forms 如何通过ajax发送大的base64图像字符串? - How to send large base64 image string through ajax? 如何将base64 png图像发送到PHP - How to send base64 png image to PHP 如何连接base64图像并使用多部分发送? - how to connect base64 image and send it using multipart? Ajax发送多个base64图像 - Ajax send multiple base64 image 将base64字符串作为图像发送到客户端 - Send base64 string as image to the client 在我的Node.js + Express API中,我有一个图像作为base64字符串,如何将其作为multipart / form-data发送到其他api - In my Node.js + Express API I have an image as base64 string, how to send it to other api as multipart/form-data 如何在Tkinter标签中使用base64编码的图像字符串? - How do I use the base64 encoded image string in Tkinter label? 我应该如何将从pouchdb检索到的base64图像字符串作为文件上传到另一个api? - How should I upload a base64 image string retrieved from pouchdb to another api as a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM