如何从OutputStream制作BufferedImage

问题描述 投票:0回答:5

我开始使用LLJTran进行JPEG图像的无损旋转。我对该库的唯一问题是它的iterface。

理想情况下,transform()将返回BufferedImage对象,但它不返回任何内容,而只是写入OutputStream。

这里是API。http://mediachest.sourceforge.net/mediautil/javadocs/mediautil/image/jpeg/LLJTran.html

有人知道我如何从OutputStream获取BufferedImage吗?

java bufferedimage
5个回答
2
投票

有人知道我如何从OutputStream获取BufferedImage吗?

  1. 创建ByteArrayOutputStream
  2. 从操作系统读取字节,然后写入BAOS。
  3. 完成后,BAOS将包含所有字节。用BAOS.toByteArray()获得byte[]
  4. byte[]作为参数返回给ByteArrayInputStream的构造函数。
  5. 将BAIS传递到ImageIO.read(InputStream)

0
投票

使用ImageIO.read(InputStream input)


0
投票

尝试并自行进行旋转,因为BufferedImage仍将包含未压缩的数据。因此,将JPEG文件解压缩为BufferedImage,然后应允许无损旋转(由于未压缩的数据)。

示例:

File unrotatedImageFile = ...;
BufferedImage srcImage = ImageIO.read( unrotatedImageFile );

AffineTransformOp t = new AffineTransformOp( 
    AffineTransform.getRotateInstance( Math.toRadians( 90.0 ) ), 
    AffineTransformOp.TYPE_NEAREST_NEIGHBOR );
BufferedImage rotatedImage = t.filter( srcImage, null );

0
投票

您是否尝试过使用BasicJpeg的子类LLJTran?它有一个getBufferedImage()方法,但是文档不是那么冗长...而且我也不知道API。

如果这不起作用,您可以让LLJTran写入连接到PipedOutputStreamPipedInputStream,并按照AlexR的建议将其馈送到ImageIO


0
投票
OutputStream os = new OutputStream();
InputStream is = new ByteArrayInputStream(os.toByteArray());
BufferedImage bi = ImageIO.read(is);

从字面上看只有3行代码。不知道为什么我们试图让彼此变得困难。

© www.soinside.com 2019 - 2024. All rights reserved.