亲宝软件园·资讯

展开

Java解码视频流图片 Java解码H264格式视频流中的图片

weixin_42534940 人气:0

引入依赖

<dependency>
  <groupId>org.bytedeco</groupId>
  <artifactId>javacv</artifactId>
  <version>1.4.3</version>
 </dependency>
 
 <dependency>
  <groupId>org.bytedeco.javacpp-presets</groupId>
  <artifactId>ffmpeg-platform</artifactId>
  <version>4.0.2-1.4.3</version>
</dependency>

需要注意这两个包之间的版本有一个对应关系,如果版本不对应可能会出问题 

解码过程

实现的一个简单功能是:

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;
 }
}

加载全部内容

相关教程
猜你喜欢
用户评论