zookeeper python接口实例详解
634
2022-12-14
Java解码H264格式视频流中的图片
本文实例为大家分享了java解码H264格式视频流中的图片,供大家参考,具体内容如下
引入依赖
需要注意这两个包之间的版本有一个对应关系,如果版本不对应可能会出问题
解码过程
实现的一个简单功能是:
从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 errCXZibyYrlor, exit ...");
logger.info(e.getMessage());
}
}
private void setBufferedImage(BufferedImage image) {
bufferedImage = image;
}
BufferedImaghttp://e getBufferedImage() {
return bufferedImage;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~