gpt4 book ai didi

java - 如何使用来自图像的数据 URI 作为 InputStream?

转载 作者:搜寻专家 更新时间:2023-10-31 20:20:54 24 4
gpt4 key购买 nike

我已经从 html5 Canvas 中检索到 base64 数据 uri。在我的 servlet 中,我想解码数据 uri 并将其用作输入流,如下面的“xxx”所示。下面的代码是我将 html5 Canvas 中的图像发布到我的 facebook 帐户中。我正在使用 restfb。

FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
BinaryAttachment.with("test.jpeg", getClass().getResourceAsStream("xxx")),
Parameter.with("message", "Test"));

我怎样才能做到这一点?谢谢。

已更新越来越近但仍然无法正常工作!

在我的jsp中:

var d = document.getElementById('img').src;
window.location.href = "upload?src=" + d;

在我的 servlet 中:

String d = req.getParameter("src");
String head = "data:image/jpeg;base64,";
String base64 = d.substring(head.length()-1);

byte[] buf = DatatypeConverter.parseBase64Binary(base64);
ByteArrayInputStream is = new ByteArrayInputStream(buf);

FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
BinaryAttachment.with("test.jpeg", is),
Parameter.with("message", "Test"));

我的编码是否有任何错误,因为它似乎在 servlet 中的某处发生错误。我无法查看错误,因为它正在服务器上运行。

最佳答案

这与 this answer 几乎完全相反!或者至少,它的反面。它回答 Image 到 base 64 String,而这个用例是 StringImage

查看javax.xml.bind.DatatypeConverter.parseBase64Binary(String)获取 Stringbyte[]

使用byte[] 构造ByteArrayInputStream .

关于java - 如何使用来自图像的数据 URI 作为 InputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19239341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com