• <small id='2I5WV'></small><noframes id='2I5WV'>

    <tfoot id='2I5WV'></tfoot>
    • <bdo id='2I5WV'></bdo><ul id='2I5WV'></ul>

    1. <legend id='2I5WV'><style id='2I5WV'><dir id='2I5WV'><q id='2I5WV'></q></dir></style></legend>
      <i id='2I5WV'><tr id='2I5WV'><dt id='2I5WV'><q id='2I5WV'><span id='2I5WV'><b id='2I5WV'><form id='2I5WV'><ins id='2I5WV'></ins><ul id='2I5WV'></ul><sub id='2I5WV'></sub></form><legend id='2I5WV'></legend><bdo id='2I5WV'><pre id='2I5WV'><center id='2I5WV'></center></pre></bdo></b><th id='2I5WV'></th></span></q></dt></tr></i><div id='2I5WV'><tfoot id='2I5WV'></tfoot><dl id='2I5WV'><fieldset id='2I5WV'></fieldset></dl></div>

        将 HTML 画布转换为二进制图像数据

        Converting HTML canvas to binary image data(将 HTML 画布转换为二进制图像数据)

            <tfoot id='OxI9M'></tfoot>

          • <i id='OxI9M'><tr id='OxI9M'><dt id='OxI9M'><q id='OxI9M'><span id='OxI9M'><b id='OxI9M'><form id='OxI9M'><ins id='OxI9M'></ins><ul id='OxI9M'></ul><sub id='OxI9M'></sub></form><legend id='OxI9M'></legend><bdo id='OxI9M'><pre id='OxI9M'><center id='OxI9M'></center></pre></bdo></b><th id='OxI9M'></th></span></q></dt></tr></i><div id='OxI9M'><tfoot id='OxI9M'></tfoot><dl id='OxI9M'><fieldset id='OxI9M'></fieldset></dl></div>

              <small id='OxI9M'></small><noframes id='OxI9M'>

                <bdo id='OxI9M'></bdo><ul id='OxI9M'></ul>
                    <tbody id='OxI9M'></tbody>
                1. <legend id='OxI9M'><style id='OxI9M'><dir id='OxI9M'><q id='OxI9M'></q></dir></style></legend>
                  本文介绍了将 HTML 画布转换为二进制图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要有关 HTML 画布元素的帮助.我是 HTML 新手.

                  I need help with HTML canvas element.I'm new to HTML.

                  我需要从网络摄像头捕获图像并将其发送到 Microsoft 认知 API 以查找图片中的情绪.

                  I need to capture image from the webcam and send it to Microsoft cognitive API to find the emotions in the picture.

                  API 接受八位字节流中的图像.API 是 https://westus.dev.cognitive.microsoft.com/docs/services/5639d931ca73072154c1ce89/operations/563b31ea778daf121cc3a5fa

                  The API accepts image in octet-stream. The API is https://westus.dev.cognitive.microsoft.com/docs/services/5639d931ca73072154c1ce89/operations/563b31ea778daf121cc3a5fa

                  代码是

                  canvas = document.getElementById("myCanvas");
                  ctx = canvas.getContext('2d');
                  dataUrl = canvas.toDataURL("image/png");
                  $.ajax({
                      url: apiUrl,
                      beforeSend: function(xhrObj) {
                          xhrObj.setRequestHeader("Content-Type", "application/octet-stream");
                          xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", apiKey);
                      },
                      type: "POST",
                      data: dataUrl,
                      processData: false
                  }).done(function(response) { 
                      ...
                  });
                  

                  HTTP响应码为400,响应信息如下.

                  HTTP Response code is 400 and the response message is as below.

                  {"error":{"code":"BadBody","message":"Invalid face image."}}
                  

                  记录 dataUrl 将数据显示为

                  logging the dataUrl shows the data as

                  data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAgAElEQVR4XqS92bOm2Vnlt7/pZGZVCU3IGkFCDhoxChDQBknQtGUBJtzd4Vvf+a7RUFklqVQC/xu.....
                  

                  我已尝试删除 data:image/png 但没有用.
                  我是对的 canvas.toDataURL 返回图像的 base64 编码数据的字符串形式.我可以在 html 页面上显示此图像.我也做了右键保存到本地驱动器,并用文件调用API,它可以工作.

                  I have tried by removing data:image/png but no use.
                  I'm right canvas.toDataURL returns string form of base64 encoded data of the image. I can display this image on html page. I also did a right click saved it to local drive, and called API with file, it works.

                  因此,问题在于将数据从画布元素以二进制格式移动到请求正文时.

                  So, the issue would be while moving the data from canvas element to the request body in binary format.

                  如何做到这一点?提前感谢您的帮助.

                  How can this be done ?. Thanks in advance for your help.

                  推荐答案

                  这可能行得通.尝试从您的 dataUrl 创建 Blob 和将其传递给ajax,如:

                  This might work. Try creating Blob from your dataUrl and pass it in to the ajax, as:

                  function createBlob(dataURL) {
                    var BASE64_MARKER = ';base64,';
                    if (dataURL.indexOf(BASE64_MARKER) == -1) {
                      var parts = dataURL.split(',');
                      var contentType = parts[0].split(':')[1];
                      var raw = decodeURIComponent(parts[1]);
                      return new Blob([raw], { type: contentType });
                    }
                    var parts = dataURL.split(BASE64_MARKER);
                    var contentType = parts[0].split(':')[1];
                    var raw = window.atob(parts[1]);
                    var rawLength = raw.length;
                  
                    var uInt8Array = new Uint8Array(rawLength);
                  
                    for (var i = 0; i < rawLength; ++i) {
                      uInt8Array[i] = raw.charCodeAt(i);
                    }
                  
                    return new Blob([uInt8Array], { type: contentType });
                  }
                  

                  在ajax中,传递blobData,

                  in ajax, pass the blobData,

                  ...
                  var blobData = createBlob(dataUrl);
                  $.ajax({
                      url : apiUrl,
                      beforeSend : function(xhrObj) {
                        xhrObj.setRequestHeader("Content-Type", "application/octet-stream");
                        xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", apiKey);
                      },
                      type : "POST",
                      data : blobData,
                      processData : false
                  }).done(function(response) {...}
                  

                  这篇关于将 HTML 画布转换为二进制图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                  How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                  Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                  SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                  Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)
                  Azure Functions - Import Custom Node Module(Azure Functions - 导入自定义节点模块)
                2. <legend id='anUzC'><style id='anUzC'><dir id='anUzC'><q id='anUzC'></q></dir></style></legend>

                  <small id='anUzC'></small><noframes id='anUzC'>

                            <tbody id='anUzC'></tbody>
                          <tfoot id='anUzC'></tfoot>
                          <i id='anUzC'><tr id='anUzC'><dt id='anUzC'><q id='anUzC'><span id='anUzC'><b id='anUzC'><form id='anUzC'><ins id='anUzC'></ins><ul id='anUzC'></ul><sub id='anUzC'></sub></form><legend id='anUzC'></legend><bdo id='anUzC'><pre id='anUzC'><center id='anUzC'></center></pre></bdo></b><th id='anUzC'></th></span></q></dt></tr></i><div id='anUzC'><tfoot id='anUzC'></tfoot><dl id='anUzC'><fieldset id='anUzC'></fieldset></dl></div>
                          • <bdo id='anUzC'></bdo><ul id='anUzC'></ul>