How to make BufferedImage from OutputStream
I started to use LLJTran to make lossless rotation of JPEG image. The only开发者_开发百科 problem I have with this library is its iterface.
Ideally the transform() would return BufferedImage object but instead it does not return anything and just writes the the OutputStream.
Here is the API. http://mediachest.sourceforge.net/mediautil/javadocs/mediautil/image/jpeg/LLJTran.html
Does anyone know how I can get BufferedImage from the OutputStream?
Does anyone know how I can get BufferedImage from the OutputStream?
- Create a
ByteArrayOutputStream
- Read bytes from the OS, write to BAOS.
- When done, the BAOS will contain all the bytes. Use
BAOS.toByteArray()
to get thebyte[]
. - Hand the
byte[]
returned as an argument to the constructor of aByteArrayInputStream
. - Pass the BAIS to
ImageIO.read(InputStream)
.
OutputStream os = new FileStream(strFilePath, FileMode.Create);
InputStream is = new ByteArrayInputStream(os.toByteArray());
BufferedImage bi = ImageIO.read(is);
Literally just 3 lines of code. Don't know why we try to make things difficult for each other.
use ImageIO.read(InputStream input)
Try and do the rotation yourself, since BufferedImage
would contain uncompressed data anyways.
Thus decompress the JPEG file into a BufferedImage
, which then should allow lossless rotation (due to the uncompressed data).
Example:
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 );
Have you tried using BasicJpeg
, a subclass of LLJTran
?
It has a getBufferedImage()
method, but the documentation is not that verbose... and I don't know the API.
If this does not work, you can let LLJTran
write to a PipedOutputStream
connected to a PipedInputStream
that will feed the ImageIO
as suggested by AlexR.
精彩评论