AWS Rekognition JS SDK 无效图像编码错误

Yur*_*kiy 5 amazon-web-services reactjs amazon-rekognition

使用 React 构建一个简单的 AWS Rekognition 演示,使用 <input type="file">

获取Invalid image encoding错误。

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
  });
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

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

sri*_*ttu 6

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;
      }
Run Code Online (Sandbox Code Playgroud)

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

 Bytes: getBinary(reader.result),
Run Code Online (Sandbox Code Playgroud)