Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.io.Image到InputStream_Java_Image_Inputstream - Fatal编程技术网

java.io.Image到InputStream

java.io.Image到InputStream,java,image,inputstream,Java,Image,Inputstream,我正在调整图像大小,需要返回InputStream对象 public InputStream resize(InputStream input, int maxSize){ BufferedImage image = ImageIO.read(input); double scale = (double) image.getWidth()/maxSize; Image scaledImage = image.getScaledInstance( (int) (image.get

我正在调整图像大小,需要返回InputStream对象

public InputStream resize(InputStream input, int maxSize){
   BufferedImage image = ImageIO.read(input);
   double scale = (double) image.getWidth()/maxSize;
   Image scaledImage = image.getScaledInstance( (int) (image.getWidth() * scale), (int) (image.getHeight() * scale), Image.SCALE_SMOOTH);
   InputStream ret = (InputStream) scaledImage;//this is wrong cast
   retrun ret;
}

如何将图像转换为输入流?

您可以使用以下代码进行转换:

 BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),    BufferedImage.TYPE_INT_RGB);
//bufferedImage is the RenderedImage to be written
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, null, null);

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", outStream); 
InputStream is = new ByteArrayInputStream(outStream.toByteArray());

对不起,我写错了。我想转换的是BushledImage,是一个java.io。Image@xedo例如我改变了我的答案。首先将您的
图像
转换为缓冲图像