AWS Rekognition JS SDK无效的图像编码错误 [英] AWS Rekognition JS SDK Invalid image encoding error

查看:96
本文介绍了AWS Rekognition JS SDK无效的图像编码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用< input type = file>

获取无效的图像编码错误。

let file = e.target.files[0];
let reader = new FileReader();

reader.readAsDataURL(file);

reader.onloadend = () => {
  let rekognition = new aws.Rekognition();

  var params = {
    Image: { /* required */
      Bytes: reader.result,
    },
    MaxLabels: 0,
    MinConfidence: 0.0
  };

  rekognition.detectLabels(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
  });

GitHub存储库: https://github.com/html5cat/vision-test/

GitHub repo: https://github.com/html5cat/vision-test/

GitHub问题: https://github.com/html5cat/vision-test/issues/1

GitHub Issue: https://github.com/html5cat/vision-test/issues/1

推荐答案

您可以尝试将reader.result转换为二进制字节。

You can try converting the reader.result into binary bytes.

function getBinary(encodedFile) {
        var base64Image = encodedFile.split("data:image/jpeg;base64,")[1];
        var binaryImg = atob(base64Image);
        var length = binaryImg.length;
        var ab = new ArrayBuffer(length);
        var ua = new Uint8Array(ab);
        for (var i = 0; i < length; i++) {
          ua[i] = binaryImg.charCodeAt(i);
        }

        var blob = new Blob([ab], {
          type: "image/jpeg"
        });

        return ab;
      }

您基本上可以为字节设置上述方法的响应:

You can essentially set the response of the above method for Bytes:

 Bytes: getBinary(reader.result),

这篇关于AWS Rekognition JS SDK无效的图像编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆