/** * Encode JPEG to buffer * * @param target Bytebuffer target. Position will be set to 0 and limit to compressed data length. * @param picture Picture to encode. * @param quality JPEG quality factor in 0-100 * @throws IOException */ public void encode(ByteBuffer target, YUVPicture picture, int quality) throws IOException { int maxSize = (int) maxSize(picture); if (!target.isDirect()) { throw new RuntimeException("Buffer must be allocated direct."); } if (target.capacity() < maxSize) { throw new IOException("Buffer is not large enough. Maximum compressed data size is " + maxSize + "bits"); } int size = encode(picture, target, target.capacity(), quality); if (size < 0) { throw new IOException("Cannot encode picture"); } target.limit(size); } }
/** * Encode JPEG to buffer * * @param target Bytebuffer target. Position will be set to 0 and limit to compressed data length. * @param picture Picture to encode. * @param quality JPEG quality factor in 0-100 * @throws IOException */ public void encode(ByteBuffer target, YUVPicture picture, int quality) throws IOException { int maxSize = (int) maxSize(picture); if (!target.isDirect()) { throw new RuntimeException("Buffer must be allocated direct."); } if (target.capacity() < maxSize) { throw new IOException("Buffer is not large enough. Maximum compressed data size is " + maxSize + "bits"); } int size = encode(picture, target, target.capacity(), quality); if (size < 0) { throw new IOException("Cannot encode picture"); } target.limit(size); } }
/** * Encode JPEG to buffer * * @param picture Picture to encode. * @param quality JPEG quality factor in 0-100 * @return Buffer with the JPEG data. Buffer.limit() equals the compressed length. This buffer gets re-used on subsequent calls to this function * @throws IOException */ public ByteBuffer encode(YUVPicture picture, int quality) throws IOException { ByteBuffer target = byteBufferProvider.getOrCreateBuffer((int) maxSize(picture)); int size = encode(picture, target, target.capacity(), quality); if (size < 0) { throw new IOException("Cannot encode picture"); } target.limit(size); return target; }
/** * Encode JPEG to buffer * * @param picture Picture to encode. * @param quality JPEG quality factor in 0-100 * @return Buffer with the JPEG data. Buffer.limit() equals the compressed length. This buffer gets re-used on subsequent calls to this function * @throws IOException */ public ByteBuffer encode(YUVPicture picture, int quality) throws IOException { ByteBuffer target = byteBufferProvider.getOrCreateBuffer((int) maxSize(picture)); int size = encode(picture, target, target.capacity(), quality); if (size < 0) { throw new IOException("Cannot encode picture"); } target.limit(size); return target; }
public static void main(String[] args) throws IOException { JPEGDecoder decoder = new JPEGDecoder(); YUVPicture jpeg = decoder.readJPEG(JPEGExample.class.getClassLoader().getResource("testImage.jpg")); jpeg.scale(jpeg.getWidth() * 2, jpeg.getHeight() * 2, FilterModeEnum.kFilterBilinear); JPEGEncoder encoder = new JPEGEncoder(); int maxSize = (int) encoder.maxSize(jpeg); ByteBuffer buffer = ByteBuffer.allocateDirect((int) maxSize); int size = encoder.encode(jpeg, buffer, maxSize, 90); buffer.limit(size); RandomAccessFile file = new RandomAccessFile("test.jpg", "rw"); FileChannel channel = file.getChannel(); channel.write(buffer); file.close(); } }
public static void main(String[] args) throws IOException { JPEGDecoder decoder = new JPEGDecoder(); YUVPicture jpeg = decoder.readJPEG(JPEGExample.class.getClassLoader().getResource("testImage.jpg")); jpeg.scale(jpeg.getWidth() * 2, jpeg.getHeight() * 2, FilterModeEnum.kFilterBilinear); JPEGEncoder encoder = new JPEGEncoder(); int maxSize = (int) encoder.maxSize(jpeg); ByteBuffer buffer = ByteBuffer.allocateDirect((int) maxSize); int size = encoder.encode(jpeg, buffer, maxSize, 90); buffer.limit(size); RandomAccessFile file = new RandomAccessFile("test.jpg", "rw"); FileChannel channel = file.getChannel(); channel.write(buffer); file.close(); } }