[英]How to get the compressed image data from JPEG encoder in Java
I want to compress an image using JPEG encoder and instead of writing it to a file I want to pass the compressed data to another application. 我想使用JPEG编码器压缩图像,而不是将其写入文件,而是将压缩后的数据传递给另一个应用程序。 My problem is that I can compress the data but don't know how to get that compressed image data.
我的问题是我可以压缩数据,但不知道如何获取压缩的图像数据。 I am using this code :
我正在使用此代码:
out = new FileOutputStream ( filename );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bi );
param.setQuality ( 0.3f, false );
encoder.setJPEGEncodeParam ( param );
encoder.encode ( bi );
out.close();
A ByteArrayOutputStream will give you access to the bytes. ByteArrayOutputStream将使您可以访问字节。
ByteArrayOutputStream out = new ByteArrayOutputStream ( );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bi);
param.setQuality ( 0.3f, false );
encoder.setJPEGEncodeParam ( param );
encoder.encode (bi);
out.close(); // a no-op on ByteArrayOutputStream
byte[] data = out.toByteArray();
Use a ByteArrayOutputStream
instead of a FileOutputStream
. 使用
ByteArrayOutputStream
代替FileOutputStream
。 Then, you can get the bytes via toByteArray()
. 然后,您可以通过
toByteArray()
获得字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.