如何获取 ImageReader 的 Base64 编码内容?

发布于 2024-09-06 00:15:44 字数 428 浏览 2 评论 0原文

如何通过 ImageReader 将图像读入 Base64 编码的字符串?

以下是使用 HtmlUnit 的示例源代码。我想获取img的base64字符串:

  WebClient wc = new WebClient();
  wc.setThrowExceptionOnFailingStatusCode(false);
  wc.setThrowExceptionOnScriptError(false);
  HtmlPage p = wc.getPage("http://flickr.com");
  HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
  System.out.println(img.getImageReader().getFormatName());

How do I read an image into a base64 encoded string by its ImageReader?

Here's example source code using HtmlUnit. I want to get the base64 String of img:

  WebClient wc = new WebClient();
  wc.setThrowExceptionOnFailingStatusCode(false);
  wc.setThrowExceptionOnScriptError(false);
  HtmlPage p = wc.getPage("http://flickr.com");
  HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
  System.out.println(img.getImageReader().getFormatName());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人如玉 2024-09-13 00:15:44

HtmlUnit 的 HtmlImage#getImageReader () 返回 < code>javax.imageio.ImageReader 是标准 Java 2D API。您可以获得 BufferedImage中,您可以使用 ImageIO#write( )

Apache Commons Codec 有一个 Base64OutputStream 您可以装饰您的 OutputStream 与。

HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
ImageReader imageReader = img.getImageReader();
BufferedImage bufferedImage = imageReader.read(0);
String formatName = imageReader.getFormatName();
ByteArrayOutputStream byteaOutput = new ByteArrayOutputStream();
Base64OutputStream base64Output = new base64OutputStream(byteaOutput);
ImageIO.write(bufferedImage, formatName, base64output);
String base64 = new String(byteaOutput.toByteArray());

或者如果你想直接将其写入文件:

// ...
FileOutputStream fileOutput = new FileOutputStream("/base64.txt");
Base64OutputStream base64Output = new base64OutputStream(fileOutput);
ImageIO.write(bufferedImage, formatName, base64output);

The HtmlUnit's HtmlImage#getImageReader() returns javax.imageio.ImageReader which is part of standard Java 2D API. You can get an BufferedImage out of it which you in turn can write to an OutputStream of any flavor using ImageIO#write().

The Apache Commons Codec has a Base64OutputStream which you can just decorate your OutputStream with.

HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
ImageReader imageReader = img.getImageReader();
BufferedImage bufferedImage = imageReader.read(0);
String formatName = imageReader.getFormatName();
ByteArrayOutputStream byteaOutput = new ByteArrayOutputStream();
Base64OutputStream base64Output = new base64OutputStream(byteaOutput);
ImageIO.write(bufferedImage, formatName, base64output);
String base64 = new String(byteaOutput.toByteArray());

Or if you want to write it to file directly:

// ...
FileOutputStream fileOutput = new FileOutputStream("/base64.txt");
Base64OutputStream base64Output = new base64OutputStream(fileOutput);
ImageIO.write(bufferedImage, formatName, base64output);
纵情客 2024-09-13 00:15:44

我不太确定你到底想要什么。

但是创建您自己的包含 Base64 内容的 Reader(请参阅 javax.imageio.stream.ImageInputStreamImpl)怎么样?
也许这个外部免费Base64Encoder可以帮助你。

到底有什么东西可以这么用?

WebClient wc = new WebClient(); 
wc.setThrowExceptionOnFailingStatusCode(false); 
wc.setThrowExceptionOnScriptError(false); 

HtmlPage p = wc.getPage("http://flickr.com"); 
HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3); 

MyBase64EncodingReader reader = new MyBase64EncodingReader(img);
System.out.println(reader.toString());

I'm not quite sure what exactly you want.

But what about creating your own Reader (see javax.imageio.stream.ImageInputStreamImpl), containing the Base64-stuff?
Maybe this external free Base64Encoder can help you out.

Something that could be used like this in the end?

WebClient wc = new WebClient(); 
wc.setThrowExceptionOnFailingStatusCode(false); 
wc.setThrowExceptionOnScriptError(false); 

HtmlPage p = wc.getPage("http://flickr.com"); 
HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3); 

MyBase64EncodingReader reader = new MyBase64EncodingReader(img);
System.out.println(reader.toString());
霓裳挽歌倾城醉 2024-09-13 00:15:44

您可以使用 encodeBase64 方法之一

来自 apache commons 编解码器

并使用 String(bytes[]) 构造函数从结果字节数组创建一个字符串。

You could use one of the encodeBase64 methods

from apache commons codec.

and create a string from the resulting byte array using the String(bytes[]) constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文