简体   繁体   English

如何从InputStream中读取ImageIO:Java

[英]How to make ImageIO read from InputStream :Java

I have created executable jar file(using Eclipse) , there are a set of image (.png) files that is to be inculded in the jar. 我已经创建了可执行jar文件(使用Eclipse),有一组图像(.png)文件将被包含在jar中。 So I have added a source folder with all the images inside /images folder in the project . 所以我添加了一个源文件夹,其中包含项目中/images文件夹中的所有图像。 Code has to access these file to create BufferedImage using ImageIO.read(new File(path); 代码必须访问这些文件才能使用ImageIO.read(new File(path);创建BufferedImage ImageIO.read(new File(path);

Earlier, To get the path I used ClassName.class.getResource(/image/test.png).toURI(); 早些时候,为了获得路径,我使用了ClassName.class.getResource(/image/test.png).toURI();

On executing jar , it throw error URI is not hierarchical 在执行jar时,它抛出错误URI不是分层的

So now I am using ClassName.class.getResourceAsStream(/image/test.png); 所以现在我使用ClassName.class.getResourceAsStream(/image/test.png);

But how to make ImageIO read from Inputstream ? 但是如何从Inputstream读取ImageIO? I tried cast as follows 我尝试演员如下

InputStreamReader resourceBuff=ClassName.class.getResourceAsStream(/image/test.png);
ImageIO.read((ImageInputStream) new InputStreamReader(resourceBuff));

It throws error InputStreamReader cannot be cast to ImageInputStream 它抛出错误InputStreamReader无法强制转换为ImageInputStream

ImageIO.read() takes InputStream as a parameter so there is no meaning of casting it to ImageInputStream . ImageIO.read()InputStream作为参数,因此没有意义将其强制转换为ImageInputStream

Secondly you can not cast an InputStreamReader object to ImageInputStream because ImageInputStream has nothing to do with InputStreamReader which you thought of. 其次,您无法将InputStreamReader对象ImageInputStreamImageInputStream因为ImageInputStream与您想到的InputStreamReader无关。

Moreover getResourceAsStream() returns InputStream . 此外, getResourceAsStream()返回InputStream So you can directly do it like this. 所以你可以直接这样做。

InputStream resourceBuff = YourClass.class.getResourceAsStream(filepath);
BufferedImage bf = ImageIO.read(resourceBuff);

The ImageIO class has a utility method to read an InputStream and create a BufferedImage . ImageIO类有一个实用程序方法来读取 InputStream并创建BufferedImage

There is also a utility method to create an ImageInputStream from an InputStream . 还有一种实用工具方法可以从InputStream 创建 ImageInputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用Java ImageIO从InputStream读取多个图像? - Is it possible to read multiple images from an InputStream using Java ImageIO? ImageIO交替读取InputStream到BufferedImage - ImageIO Alternitive to read for InputStream to BufferedImage 如何从java中的inputStream中读取每个字符串 - how to read each string from the inputStream in java Java - 如何从inputStream(socket / socketServer)读取未知数量的字节? - Java — How to read an unknown number of bytes from an inputStream (socket/socketServer)? Java-如何扩展InputStream以从JTextField读取? - Java-How to extend InputStream to read from a JTextField? 如何从Java中的InputStream读取并转换为字节数组? - How do you read from an InputStream in Java and convert to byte array? 在Java中,如何从输入流中读取固定长度并将其另存为文件? - In java, how to read a fixed length from the inputstream and save as a file? ImageIO什么时候应该停止从InputStream读取? - When should ImageIO stop reading from InputStream? 从 Java 服务器读取 Swift 中的 InputStream - Read from InputStream in Swift from Java Server Java ImageIO:如何从文件读取BufferedImage,以便它使用DataBufferFloat? - Java ImageIO: How can I read a BufferedImage from file, so that it uses DataBufferFloat?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM