使用 Java 和 jmf 从网络摄像头捕获实时视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17585917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:23:41  来源:igfitidea点击:

Capture live video from webcam using Java an jmf

javajmf

提问by balaraju

For capture the live video from web using java and jmf i have written the below code , I plugin the usb webcam also but it gives java.util.NoSuchElementException
anyone please help me

为了使用 java 和 jmf 从网络捕获实时视频,我编写了以下代码,我也插入了 USB 网络摄像头,但它给了java.util.NoSuchElementException
任何人请帮助我

import java.util.Vector;
import javax.media.*;
import javax.media.format.AudioFormat;
import javax.media.format.VideoFormat;
import javax.media.protocol.DataSource;
import javax.media.CaptureDeviceInfo;
import javax.media.format.YUVFormat;
import javax.media.protocol.FileTypeDescriptor;

public class CamRecorder {

    static VideoFormat videoFormat;
    static AudioFormat audioFormat;
    static CaptureDeviceInfo videoDevice;
    static CaptureDeviceInfo audioDevice;

    public static void main(String[] args) {
    try {
        Vector deviceList = CaptureDeviceManager.getDeviceList(new YUVFormat()); //get all media devices

        CaptureDeviceInfo device = (CaptureDeviceInfo) deviceList.firstElement(); //in this computer the only capture device is in=built webcam stays at 0th position
        Format[] formats = device.getFormats(); //get all formats

        for (int x = 0; x < formats.length; x++) {
            if (formats[x] != null && formats[x] instanceof VideoFormat) {
                videoFormat = (VideoFormat) formats[x]; //take the video format
                videoDevice = device;
            }
            if (formats[x] != null && formats[x] instanceof AudioFormat) {
                audioFormat = (AudioFormat) formats[x]; //take the audio format
                //audioDevice = device;
            }
        }
        //create data sources
        DataSource videoDataSource = Manager.createDataSource(device.getLocator());

        deviceList = CaptureDeviceManager.getDeviceList(new AudioFormat(null)); //get all media devices
        device = (CaptureDeviceInfo) deviceList.firstElement();

        DataSource audioDataSource=Manager.createDataSource(device.getLocator());

        DataSource[] dArray=new DataSource[2];
        dArray[0]=videoDataSource;
        dArray[1]=audioDataSource;

            DataSource mixedDataSource = Manager.createMergingDataSource(dArray);

        //format for output

            Format[] outputFormats=new Format[2];
            outputFormats[0]=new VideoFormat(VideoFormat.YUV);
            outputFormats[1]=new AudioFormat(AudioFormat.LINEAR);
        //output type
            FileTypeDescriptor outputType=new FileTypeDescriptor(FileTypeDescriptor.MSVIDEO);

        //settingup Processor
            ProcessorModel processorModel=new ProcessorModel(mixedDataSource, outputFormats, outputType);
            Processor processor=Manager.createRealizedProcessor(processorModel);

        //settingup sink
            DataSource outputDataSource=processor.getDataOutput();
            MediaLocator destination=new MediaLocator("file:.\testcam.avi");
            DataSink dataSink=Manager.createDataSink(outputDataSource, destination);
            dataSink.open();

            //start sink + processor
            Thread.sleep(4000);
            dataSink.start();
            processor.start();

            Thread.sleep(4000);

            dataSink.close();
            processor.stop();
            processor.close();


    } catch (Exception ex) {
        System.out.println(ex);

    }

}

}

}

回答by balaraju

yes, it is perfect but install the jmf and set the path and class path properly .

是的,它是完美的,但是安装 jmf 并正确设置路径和类路径。

jmf installation: http://www.stierlen2.homepage.t-online.de/modellbau/3dscanner/jmf_installation.htm

jmf 安装:http: //www.stierlen2.homepage.t-online.de/modellbau/3dscanner/jmf_installation.htm