Java - XML中的图像编码 [英] Java - Image encoding in XML

查看:133
本文介绍了Java - XML中的图像编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我会比较容易地找到一个解决这个问题的办法,但是在这里我呼吁你们从上帝的帮助中把我从这个难题中解脱出来。



所以,我有一个图像,我想使用Java将它存储在一个XML文档中。我以前通过将图像保存为流,将流转换为数组,然后VB的xml类能够将数组编码为base64字符串,从而在VisualBasic中实现了此功能。但是,经过几个小时的淘汰网络,以获得Java的等效解决方案,我已经空手而归。我唯一的成功就是:

  import it.sauronsoftware.base64。*; 
import java.awt.image.BufferedImage;
import org.w3c.dom。*;

...

BufferedImage img;
元素节点;

...

java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream();

ImageIO.write(img,png,os);

byte [] array = Base64.encode(os.toByteArray());

String ss = arrayToString(array,,);

node.setTextContent(ss);

...

private static String arrayToString(byte [] a,String separator){
StringBuffer result = new StringBuffer();
if(a.length> 0){
result.append(a [0]); (int i = 1; i result.append(separator);

result.append(a [i]);
}
}
return result.toString();
}

我可以猜测,但是将该过程恢复到当我加载XML文件时,镜像被证明是不可能的。如果任何人有更好的方式对XML文件中的图像进行编码/解码,请向前迈进,即使它只是一个链接到另一个线程,这将是正常的。



提前提醒,



Hoopla。

解决方案

类似的东西(Base64中的编码和解码),它的作用就像一个魅力。以下是我认为您应该做的事情,使用课程来自Apache Commons项目的 Base64

  // ENCODING 
BufferedImage img = ImageIO.read(new File(image.png));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img,png,baos);
baos.flush();
String encodedImage = Base64.encodeToString(baos.toByteArray());
baos.close(); //应该在一个finally块内
node.setTextContent(encodedImage); //存储在节点

// DECODING
String encodedImage = node.getTextContent();
byte [] bytes = Base64.decode(encodedImage);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));

希望有帮助。


I thought I would find a solution to this problem relatively easily, but here I am calling upon the help from ye gods to pull me out of this conundrum.

So, I've got an image and I want to store it in an XML document using Java. I have previously achieved this in VisualBasic by saving the image to a stream, converting the stream to an array, and then VB's xml class was able to encode the array as a base64 string. But, after a couple of hours of scouring the net for an equivalent solution in Java, I've come back empty handed. The only success I have had has been by:

import it.sauronsoftware.base64.*;
import java.awt.image.BufferedImage;
import org.w3c.dom.*;

...

      BufferedImage img;
      Element node;

      ...

      java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream();

      ImageIO.write(img, "png", os);

      byte[] array = Base64.encode(os.toByteArray());

      String ss = arrayToString(array, ",");

      node.setTextContent(ss);

      ...

  private static String arrayToString(byte[] a, String separator) {
    StringBuffer result = new StringBuffer();
    if (a.length > 0) {
        result.append(a[0]);
        for (int i=1; i<a.length; i++) {
            result.append(separator);
            result.append(a[i]);
        }
    }
    return result.toString();
  }

Which is okay I guess, but reversing the process to get it back to an image when I load the XML file has proved impossible. If anyone has a better way to encode/decode an image in an XML file, please step forward, even if it's just a link to another thread that would be fine.

Cheers in advance,

Hoopla.

解决方案

I've done something similar (encoding and decoding in Base64) and it worked like a charm. Here's what I think you should do, using the class Base64 from the Apache Commons project:

 //  ENCODING
 BufferedImage img = ImageIO.read(new File("image.png"));    
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 ImageIO.write(img, "png", baos);    
 baos.flush();
 String encodedImage = Base64.encodeToString(baos.toByteArray());
 baos.close(); // should be inside a finally block
 node.setTextContent(encodedImage); // store it inside node

 // DECODING
 String encodedImage = node.getTextContent();
 byte[] bytes = Base64.decode(encodedImage);
 BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));

Hope it helps.

这篇关于Java - XML中的图像编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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