从字节数组中读取图片 [英] Reading picture from byte array
问题描述
我正在编写一个Java小程序,需要读取ZIP文件中的一些XML和图像文件。 Zip文件将通过HTTP下载,applet是无符号的,因此我需要使用 java.util.zip.ZipInputStream
来操作数据。当我尝试读取Zip文件中的PNG文件时出现问题。
I'm writing a Java applet that need to read some XML and image files inside a ZIP file. The Zip file will be download through HTTP and the applet is unsigned, so I need to use java.util.zip.ZipInputStream
to manipulated the data. There's a problem when I'm trying to read a PNG file inside the Zip file.
我处理Zip文件的步骤:
The steps I handle the Zip file:
-
通过Http下载Zip
Download the Zip through Http
URL resourceURL = new URL(source);
HttpURLConnection httpConnection= (HttpURLConnection) resourceURL.openConnection();
httpConnection.connect();
InputStream resourceFileIn = httpConnection.getInputStream();
使用ZipInputStream保存下载的数据
Use ZipInputStream to hold the data downloaded
ZipInputStream resourceZipIn = new ZipInputStream(resourceFileIn);
遍历每个ZipEntry并提取相应的字节数组
Iterate through every ZipEntry and extract the corresponding byte array
ArrayList<ExtractedEntry> extractedList = new ArrayList<ExtractedEntry>();
ZipEntry currentEntry;
while ((currentEntry = resourceZipIn.getNextEntry()) != null) {
byte[] byteHolder = new byte[(int) currentEntry.getSize()];
resourceZipIn.read(byteHolder, 0, byteHolder.length);
extractedList.add(new ExtractedEntry(currentEntry.getName(), byteHolder));
}
备注:提取的每个ZipEntry都由以下类保存
remarks: every ZipEntry extracted is hold by the following class
public class ExtractedEntry {
private String name;
private byte[] byteArray;
public ExtractedEntry(String name, byte[] byteArray) {
super();
this.name = name;
this.byteArray = byteArray;
}
public String getName() {
return name;
}
public byte[] getByteArray() {
return byteArray;
}
}
找到我要读取的文件在提取的列表中
Find the file I want to read from in the extracted list
ExtractedEntry bgEntry = null;
for (int j = 0; j < extractedList.size() && bgEntry == null; j++) {
if (extractedList.get(j).getName().equals("background.png")) {
bgEntry = extractedList.get(j);
}
}
根据需要执行特定操作
Perform specific action according to need
InputStream mapBgIn = new ByteArrayInputStream(bgEntry.getByteArray());
BufferedImage bgEntry = ImageIO.read(bgIn);
<我只列出了读取PNG文件的动作,因为这是我遇到问题的地方。当我尝试以上述方式读取图像时,我总是在第5步的最后一行收到以下错误。
I list out only the action of reading the PNG file as that is where I encounter the problem. When I try to read the image in the way mentioned above, I always receive the following error at the last line of step 5.
javax.imageio.IIOException: Error reading PNG image data at com.sun.imageio.plugins.png.PNGImageReader.readImage(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at com.quadd.soft.game.wtd.lib.resource.ResourceManager.loadHttpZipRes(ResourceManager.java:685)
Caused by: javax.imageio.IIOException: Unknown row filter type (= 7)!
at com.sun.imageio.plugins.png.PNGImageReader.decodePass(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(Unknown Source)
... 29 more
但是,如果我读了图像从下面的步骤3开始,没有问题。
However, if I read the image in the following way starting from step 3, there's no problem.
ZipInputStream resourceZipIn = new ZipInputStream(resourceFileIn);
ZipEntry testEntry;
while ((testEntry = resourceZipIn.getNextEntry()) != null) {
if (testEntry.getName().equals("background.png")) {
BufferedImage bgEntry = ImageIO.read(resourceZipIn);
}
}
因此,我猜我的代码在从 java.util.zip.ZipInputStream
中提取字节时出现了一些问题,并将其放回以读取图像。但是我对操纵流很陌生,所以我无法弄清楚究竟是什么问题。我想问一下是否有人可以指出我在代码中犯了哪些错误导致错误。
Therefore, I guess that there's some problem with my code at extracting the bytes from java.util.zip.ZipInputStream
and put it back for reading the image. But I'm quite new to manipulating stream so I just couldn't figure out what exactly is the problem. I would like to ask if anyone could point out what mistake I've made in the code which cause the error.
推荐答案
read
方法不保证它填充字节数组;它只读取一个小块并返回它读取的字节数。你需要一个循环,或者使用一个填充数组的辅助类。
The read
method does not guarantee that it fills the byte array; it reads only a small block and returns the number of bytes that it read. You need a loop, or use a helper class that does fill the array.
例如
DataInputStream in = new DataInputStream(resourceZipIn);
in.readFully(byteHolder);
in.close();
这篇关于从字节数组中读取图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!