如何使用Image中的Data URI作为InputStream? [英] How to use Data URI from Image as an InputStream?

查看:211
本文介绍了如何使用Image中的Data URI作为InputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从html5画布检索了base64数据uri。在我的servlet中,我想解码数据uri并将其用作输入流,如下面的xxx所示。以下编码是让我将html5画布中的图像发布到我的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中的某个地方发生错误。我无法查看在服务器上运行的错误。

解决方案

这与几乎完全相反的这个答案!或至少,反之亦然。它以 Image 为基础64 String ,而这个用例是 String to Image



查看 javax.xml.bind.DatatypeConverter .parseBase64Binary(String) 以获取 String >。



使用 byte [] 构造一个 ByteArrayInputStream


I have retrieved the base64 data uri from a html5 canvas. Within my servlet, I would like to decode the data uri and use it as an input stream as shown in "xxx" below. The following coding is for me to post the image in the html5 canvas into my facebook account. I am using restfb.

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

How can I achieve that? Thanks.

Updated Getting closer but still not working!

In my jsp:

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

In my 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"));

Is there any errors in my coding as it seems to hit error somewhere within the servlet. I can't view the errors as it is running on a server.

解决方案

This needs almost the exact opposite to this answer! Or at least, the reverse of it. It answers Image to base 64 String, whereas this use-case is String to Image.

Look to javax.xml.bind.DatatypeConverter.parseBase64Binary(String) to get a byte[] of the String.

Use the byte[] to construct a ByteArrayInputStream.

这篇关于如何使用Image中的Data URI作为InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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