本文实例为大家分享了Java解码H264格式视频流中的图片,供大家参考,具体内容如下
引入依赖
org.bytedecojavacv1.4.3org.bytedeco.javacpp-presetsffmpeg-platform4.0.2-1.4.3
需要注意这两个包之间的版本有一个对应关系,如果版本不对应可能会出问题
解码过程
实现的一个简单功能是:
从Socket获取视频输入流(H264格式)获取视频流中的帧把每一帧转换成 BufferedImage
package com.amwalle.walle.raspi.camera;import org.bytedeco.javacv.FFmpegFrameGrabber;import org.bytedeco.javacv.Frame;import org.bytedeco.javacv.Java2DFrameConverter;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.InputStream;import java.net.Socket;public class CameraHandler implements Runnable { private static final Logger logger = LoggerFactory.getLogger(CameraHandler.class); private final String LOCK = "LOCK"; private Socket cameraSocket; private static BufferedImage bufferedImage; CameraHandler(Socket socket) throws IOException { this.cameraSocket = socket; } String getLock() { return LOCK; } @Override public void run() { try { InputStream cameraStream = cameraSocket.getInputStream(); FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(cameraStream); frameGrabber.setFrameRate(30); frameGrabber.setFormat("h264"); frameGrabber.setVideoBitrate(15); frameGrabber.setVideoOption("preset", "ultrafast"); frameGrabber.setNumBuffers(25000000); frameGrabber.start(); Frame frame = frameGrabber.grab(); Java2DFrameConverter converter = new Java2DFrameConverter(); while (frame != null) { BufferedImage bufferedImage = converter.convert(frame); setBufferedImage(bufferedImage); synchronized (LOCK) { LOCK.notifyAll(); } frame = frameGrabber.grab(); } } catch (IOException e) { logger.info("Video handle error, exit ..."); logger.info(e.getMessage()); } } private void setBufferedImage(BufferedImage image) { bufferedImage = image; } BufferedImage getBufferedImage() { return bufferedImage; }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
以上就上有关Java解码H264格式视频流中的图片的相关介绍,要了解更多java, 解码, 视频流内容请登录学步园。