简体   繁体   English

获取从 blob URL 生成的黑色图像

[英]Getting a black image generated from a blob URL

I am using a library react-avatar-editor for creating profile picture of the user.我正在使用库react-avatar-editor来创建用户的个人资料图片。 Below is the code which I am using the send the cropped image to the server下面是我使用的将裁剪后的图像发送到服务器的代码

const canvasScaled = this.editor.getImageScaledToCanvas(); //a canvas object
canvasScaled.toBlob((blob) => {
  this.setState({
      preview: blob
    },
    () => {
      let data = new FormData();
      const file = new File([blob], "my-image", {
        type: "image/png"
      });
      data.append("file", file);
      axios({
        method: "POST",
        data: data,
        url: "/media",
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then().catch()
    }
  );
})

But the image generated at the server is a black image.但是在服务器生成的图像是黑色图像。 Tested with many files but result is same - a black image.测试了许多文件,但结果相同 - 黑色图像。

My backend service is written in JAVA so I tested if the backend service handling the image correctly or not, I wrote a simple file selected我的后端服务是用 JAVA 编写的,所以我测试了后端服务是否正确处理图像,我选择了一个简单的文件

<input type="file" accept={"image/png"} onChange={onSelect}/>

<button onClick={() => {
        const data = new FormData();
        data.append("file", this.state.file);
        axios({
            method: "POST",
            data: data,
            url: "/media",
            headers: {
              'Content-Type': 'multipart/form-data'
            }
        }).then().catch()
}}>Send<button>

And yes, the service side code is working fine with above POST request.是的,服务端代码在上面的POST请求中工作正常。 Which means, I have definitely something wrong written at the react-avatar-editor handling code.这意味着,我在 react-avatar-editor 处理代码中肯定写了一些错误。

Is it the blob causing the issue?是导致问题的blob吗? How the image is sent in the case of a file-selector?在文件选择器的情况下如何发送图像? Base64 or blob? Base64 还是 Blob?

Update: I noticed a strange behavior with an image.更新:我注意到一个图像的奇怪行为。 If I upload this file如果我上传这个文件在此处输入图片说明 This gets converted to这被转换为不全黑 which is not a complete black image.这不是一个完整的黑色图像。 Something fishy is going on with colors.颜色发生了一些可疑的事情。

To check if the library itself is creating the black image, I check it using canvasScaled.toDataURL("image/png") and used this into an image, and yes there is no issue with the created canvas, image is getting rendered.要检查库本身是否正在创建黑色图像,我使用canvasScaled.toDataURL("image/png")检查它并将其用于图像,是的,创建的画布没有问题,图像正在渲染。

Try data.append("file", blob);试试data.append("file", blob); without calling new File .无需调用new File According to MDN,根据 MDN,

A File object is a specific kind of a Blob, and can be used in any context that a Blob can. File 对象是一种特定类型的 Blob,可以在 Blob 可以使用的任何上下文中使用。 In particular, FileReader, URL.createObjectURL(), createImageBitmap(), and XMLHttpRequest.send() accept both Blobs and Files.特别是,FileReader、URL.createObjectURL()、createImageBitmap() 和 XMLHttpRequest.send() 接受 Blob 和文件。

add this in second line after defining canvasScaled在定义canvasScaled后在第二行中添加它

var ctx = canvasScaled.getContext("2d");
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);

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

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