gpt4 book ai didi

java - 用Java读取图像

转载 作者:行者123 更新时间:2023-11-30 03:20:52 25 4
gpt4 key购买 nike

我正在尝试使用以下代码读取图像,但我无法弄清楚为什么会发生这种情况。如果我在下面的代码中做错了什么,请告诉我。

System.out.println("Image Bytes ::"+imageBytes);

InputStream in = new ByteArrayInputStream(imageBytes);
BufferedImage img = ImageIO.read(in);

System.out.println("Buff Image :: "+img);

输出如下:

Image Bytes ::[B@4554617c
Buff Image :: null

最佳答案

由于您的 imageByte 的来源未知,因此很难说出了什么问题。但是如果您正在创建该 byteSource,那么下面的代码可能会对您有所帮助,因为来自 ImageIO.read() 的 Javadocs

Returns a BufferedImage as the result of decoding a supplied File with an ImageReader chosen automatically from among those currently registered. The File is wrapped in an ImageInputStream. If no registered ImageReader claims to be able to read the resulting stream, null is returned.

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

/**
* Created by ankur on 13/7/15.
* The Following program will read an image file.
* convert it into byte array, and then reuse the
* converted byte array, and convert it back to new BufferedImage
*
*/
public class ImageToBuf {

public static void main(String... strings) throws IOException {
byte[] imageInByte;
//read the image
BufferedImage originalImage = ImageIO.read(new File("/home/ankur/Pictures/BlpRb.png"));
//convert BufferedImage to byte array
ByteArrayOutputStream byteOutS = new ByteArrayOutputStream();
ImageIO.write(originalImage, "png", byteOutS);
byteOutS.flush();
imageInByte = byteOutS.toByteArray();
byteOutS.close();

//convert byte array back to BufferedImage
InputStream readedImage = new ByteArrayInputStream(imageInByte);
BufferedImage bfImage = ImageIO.read(readedImage);
System.out.println(bfImage);
}
}

输出(在我的机器上):

BufferedImage@21b8d17c: type = 13 IndexColorModel: #pixelBits = 8 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@6433a2 transparency = 1 transIndex   = -1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 4959 height = 3505 #numDataElements 1 dataOff[0] = 0

关于java - 用Java读取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31370212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com