JAVA中,把BufferedImage变成inputstream的过程解释
网上说的过程是这样的:ByteArrayOutputStreambs=newByteArrayOutputStream();ImageOutputStreamimOut=...
网上说的过程是这样的:
ByteArrayOutputStream bs =new ByteArrayOutputStream();
ImageOutputStream imOut =ImageIO.createImageOutputStream(bs);
ImageIO.write(scaledImage1,"jpg",imOut); //scaledImage1为BufferedImage,jpg为图像的类型
InputStream is =new ByteArrayInputStream(bs.toByteArray());
我想问一下为什么中间要通过一个OutputStream?不能直接由BufferedImage转成InputStream吗? 展开
ByteArrayOutputStream bs =new ByteArrayOutputStream();
ImageOutputStream imOut =ImageIO.createImageOutputStream(bs);
ImageIO.write(scaledImage1,"jpg",imOut); //scaledImage1为BufferedImage,jpg为图像的类型
InputStream is =new ByteArrayInputStream(bs.toByteArray());
我想问一下为什么中间要通过一个OutputStream?不能直接由BufferedImage转成InputStream吗? 展开
1个回答
推荐于2016-07-20 · 知道合伙人数码行家
关注
展开全部
你好,这个不可以的,因为只有转换成流对象才能进行操作,一般图片都是二进制流。
public InputStream getImageStream(String layerName,List<Color> colors,String[] pixels){
InputStream is = null;
BufferedImage bi = createImage(layerName, colors, pixels);
bi.flush();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut;
try {
imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(bi, "png",imOut);
is= new ByteArrayInputStream(bs.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
public InputStream getImageStream(String layerName,List<Color> colors,String[] pixels){
InputStream is = null;
BufferedImage bi = createImage(layerName, colors, pixels);
bi.flush();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut;
try {
imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(bi, "png",imOut);
is= new ByteArrayInputStream(bs.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询