java调用摄像头录像,如何使用JavaCV从网络摄像头捕获和录制视频

I'm new to JavaCV and I have difficult time finding good tutorials about different issues on the topics that I'm interested in. I've succeed to implement some sort of real time video streaming from my webcam but the problem is that I use this code snippet which I found on the net :

@Override

public void run() {

FrameGrabber grabber = new VideoInputFrameGrabber(0); // 1 for next

// camera

int i = 0;

try {

grabber.start();

IplImage img;

while (true) {

img = grabber.grab();

if (img != null) {

cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise

cvSaveImage((i++) + "-aa.jpg", img);

// show image on window

canvas.showImage(img);

}

that results in multiple jpg files.

What I really want to do is capture my webcam input and along with showing it I want to save it in a proper video file. I find out about FFmpegFrameRecorder but don't know how to implement it. Also I've been wondering what are the different options for the format of the video file, because flv maybe would be more useful for me.

解决方案

It's been quite a journey. Still a few things that I'm not sure what's the meaning behind them, but here is a working example for capturing and recording video from a webcam using JavaCV:

import com.googlecode.javacv.CanvasFrame;

import com.googlecode.javacv.FFmpegFrameRecorder;

import com.googlecode.javacv.OpenCVFrameGrabber;

import com.googlecode.javacv.cpp.avutil;

import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class CameraTest {

public static final String FILENAME = "output.mp4";

public static void main(String[] args) throws Exception {

OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);

grabber.start();

IplImage grabbedImage = grabber.grab();

CanvasFrame canvasFrame = new CanvasFrame("Cam");

canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());

System.out.println("framerate = " + grabber.getFrameRate());

grabber.setFrameRate(grabber.getFrameRate());

FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(FILENAME, grabber.getImageWidth(),grabber.getImageHeight());

recorder.setVideoCodec(13);

recorder.setFormat("mp4");

recorder.setPixelFormat(avutil.PIX_FMT_YUV420P);

recorder.setFrameRate(30);

recorder.setVideoBitrate(10 * 1024 * 1024);

recorder.start();

while (canvasFrame.isVisible() && (grabbedImage = grabber.grab()) != null) {

canvasFrame.showImage(grabbedImage);

recorder.record(grabbedImage);

}

recorder.stop();

grabber.stop();

canvasFrame.dispose();

}

}

It was somewhat hard for me to make this work so in addition to those that may have the same issue, if you follow the official guide about how to setup JavaCV on Windows 7/64bit and want to capture video using the code above you should create a new directory in C:\ : C:\ffmpeg and extract the files from the ffmped release that you've been told to download in the official guide. Then you should add C:\ffmpeg\bin to your Enviorment variable PATH and that's all. About this step all credits go to karlphillip

and his post here

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值