/** * 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(); } }
@Override public void encodeFrame(YUVPicture frame) throws IOException { ByteBuffer buffer = encoder.encode(frame, quality); ByteBuffer heapBuffer = ByteBuffer.allocate(buffer.remaining()); heapBuffer.put(buffer); heapBuffer.flip(); encodeFrame(heapBuffer); }
@Override public void close() throws IOException { Size size = new Size(width, height); SampleEntry sampleEntry = MP4Muxer.videoSampleEntry("jpeg", size, "IHMCVideoCodecs"); track.addSampleEntry(sampleEntry); muxer.writeHeader(); channel.close(); encoder.delete(); }
@Override public void encodeFrame(YUVPicture frame) throws IOException { ByteBuffer buffer = encoder.encode(frame, quality); ByteBuffer heapBuffer = ByteBuffer.allocate(buffer.remaining()); heapBuffer.put(buffer); heapBuffer.flip(); encodeFrame(heapBuffer); }
@Override public void close() throws IOException { Size size = new Size(width, height); SampleEntry sampleEntry = MP4Muxer.videoSampleEntry("jpeg", size, "IHMCVideoCodecs"); track.addSampleEntry(sampleEntry); muxer.writeHeader(); channel.close(); encoder.delete(); }
/** * 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 byte[] convertBufferedImageToJPEGData(BufferedImage bufferedImage) { try { YUVPicture picture = converter.fromBufferedImage(bufferedImage, YUVSubsamplingType.YUV420); ByteBuffer buffer = encoder.encode(picture, 75); byte[] data = new byte[buffer.remaining()]; buffer.get(data); return data; } catch (IOException e) { e.printStackTrace(); return null; } } }
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(); } }
/** * 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); } }
@Override public void updateImage(VideoSource videoSource, BufferedImage bufferedImage, long timeStamp, Point3d cameraPosition, Quat4d cameraOrientation, IntrinsicParameters intrinsicParameters) { YUVPicture picture = converter.fromBufferedImage(bufferedImage, YUVSubsamplingType.YUV420); try { ByteBuffer buffer; synchronized (hackyLockBecauseJPEGEncoderIsNotThreadsafe) { buffer = encoder.encode(picture, 75); } byte[] data = new byte[buffer.remaining()]; buffer.get(data); handler.newVideoPacketAvailable(videoSource, timeStamp, data, cameraPosition, cameraOrientation, intrinsicParameters); } catch (IOException e) { e.printStackTrace(); } picture.delete(); }
/** * 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); } }
ByteBuffer buffer = encoder.encode(yuv, 90); if(buffer.remaining() <= MAXIMUM_IMAGE_DATA_SIZE)
ByteBuffer buffer = encoder.encode(yuv, 90); int dataLength = buffer.remaining(); ByteBuffer sendBuffer = getOrCreateBuffer(dataLength + LogDataHeader.length());
ByteBuffer buffer = encoder.encode(yuv, 90); int dataLength = buffer.remaining(); ByteBuffer sendBuffer = getOrCreateBuffer(dataLength + LogDataHeader.length());