windows7上java编程的问题(在windows xp下运行良好) [英] Problem in java programming on windows7 (working well in windows xp)

查看:23
本文介绍了windows7上java编程的问题(在windows xp下运行良好)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从连接到 PC 的网络摄像头捕获视频.我使用以下代码执行此操作:

I am capturing video from webcam connected to pc.I am using the following code to do so:

import java.util.*;
import javax.media.*;
import javax.media.protocol.*;
import javax.media.control.*;
import javax.media.format.*;
import java.awt.*;
/**
* This is the primary class to run. It gathers an image stream and drives the processing.
*
*/
public class jmfcam05v
{
    DataSource dataSource;
    PushBufferStream pbs;

    Vector camImgSize = new Vector();
    Vector camCapDevice = new Vector();
    Vector camCapFormat = new Vector();

    int camFPS;
    int camImgSel;

    Processor processor = null;
    DataSink datasink = null;

    /**
    * Main method to instance and run class
    *
    */
    public static void main(String[] args)
    {
        jmfcam05v jmfcam = new jmfcam05v();
    }

    /**
    * Constructor and processing method for image stream from a cam
    *
    */
    public jmfcam05v()
    {
        // Select webcam format
        fetchDeviceFormats();
        camImgSel=0;    // first format, or otherwise as desired
        camFPS = 20;    // framerate

        // Setup data source
        fetchDeviceDataSource();
        createPBDSource();
        createProcessor(dataSource);
        startCapture();
        try{Thread.sleep(90000);}catch(Exception e){}   // 20 seconds
        stopCapture();
    }

    /**
    * Gathers info on a camera
    *
    */
    boolean fetchDeviceFormats()
    {
        Vector deviceList = CaptureDeviceManager.getDeviceList(new VideoFormat(null));
        CaptureDeviceInfo CapDevice = null;
        Format CapFormat = null;
        String type = "N/A";

        CaptureDeviceInfo deviceInfo=null;boolean VideoFormatMatch=false;
        for(int i=0;i<deviceList.size();i++)
        {
            // search for video device
            deviceInfo = (CaptureDeviceInfo)deviceList.elementAt(i);
            if(deviceInfo.getName().indexOf("vfw:")<0)continue;

            Format deviceFormat[] = deviceInfo.getFormats();
            for (int f=0;f<deviceFormat.length;f++)
            {
                if(deviceFormat[f] instanceof RGBFormat)type="RGB";
                if(deviceFormat[f] instanceof YUVFormat)type="YUV";
                if(deviceFormat[f] instanceof JPEGFormat)type="JPG";

                Dimension size = ((VideoFormat)deviceFormat[f]).getSize();
                camImgSize.addElement(type+" "+size.width+"x"+size.height);

                CapDevice = deviceInfo;
                camCapDevice.addElement(CapDevice);
                //System.out.println("Video device = " + deviceInfo.getName());

                CapFormat = (VideoFormat)deviceFormat[f];
                camCapFormat.addElement(CapFormat);
                //System.out.println("Video format = " + deviceFormat[f].toString());

                VideoFormatMatch=true;  // at least one
            }
        }
        if(VideoFormatMatch==false)
        {
            if(deviceInfo!=null)System.out.println(deviceInfo);
            System.out.println("Video Format not found");
            return false;
        }

        return true;
    }

    /**
    * Finds a camera and sets it up
    *
    */
    void fetchDeviceDataSource()
    {
        CaptureDeviceInfo CapDevice = (CaptureDeviceInfo)camCapDevice.elementAt(camImgSel);
        System.out.println("Video device = " + CapDevice.getName());
        Format CapFormat = (Format)camCapFormat.elementAt(camImgSel);
        System.out.println("Video format = " + CapFormat.toString());

        MediaLocator loc = CapDevice.getLocator();
        try
        {
            dataSource = Manager.createDataSource(loc);
        }
        catch(Exception e){}

        try
        {
            // ensures 30 fps or as otherwise preferred, subject to available cam rates but this is frequency of windows request to stream
            FormatControl formCont=((CaptureDevice)dataSource).getFormatControls()[0];
            VideoFormat formatVideoNew = new VideoFormat(null,null,-1,null,(float)camFPS);
            formCont.setFormat(CapFormat.intersects(formatVideoNew));
        }
        catch(Exception e){}
    }

    /**
    * Gets a stream from the camera (and sets debug)
    *
    */
    void createPBDSource()
    {
        try
        {
            pbs=((PushBufferDataSource)dataSource).getStreams()[0];
        }
        catch(Exception e){}
    }

    public void createProcessor(DataSource datasource)
    {
        FileTypeDescriptor ftd = new FileTypeDescriptor(FileTypeDescriptor.MSVIDEO);
        Format[] formats = new Format[] {new VideoFormat(VideoFormat.INDEO50)};
        ProcessorModel pm = new ProcessorModel(datasource, formats, ftd);
        try
        {
            processor = Manager.createRealizedProcessor(pm);
        }
        catch(Exception me)
        {
            System.out.println(me);
            // Make sure the capture devices are released
            datasource.disconnect();
            return;
        }
    }

    private void startCapture()
    {
        // Get the processor's output, create a DataSink and connect the two.
        DataSource outputDS = processor.getDataOutput();
        try
        {
            MediaLocator ml = new MediaLocator("file:capture.avi");
            datasink = Manager.createDataSink(outputDS, ml);
            datasink.open();
            datasink.start();
        }catch (Exception e)
        {
            System.out.println(e);
        }
        processor.start();
        System.out.println("Started saving...");
    }

    private void pauseCapture()
    {
        processor.stop();
    }

    private void resumeCapture()
    {
        processor.start();
    }

    private void stopCapture()
    {
        // Stop the capture and the file writer (DataSink)
        processor.stop();
        processor.close();
        datasink.close();
        processor = null;
        System.out.println("Done saving.");
    }
}

这个程序在 windows xp(桌面)上运行良好,我尝试在 windows7(笔记本电脑)上使用它,它给了我以下错误:

this program works well on windows xp(desktop) and wen i try to use it on windows7(laptop) it gives me the following error:

    run: Video Format not found 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
            at java.util.Vector.elementAt(Vector.java:427)
            at jmfcam05v.fetchDeviceDataSource(jmfcam05v.java:112)
            at jmfcam05v.<init>(jmfcam05v.java:49)
            at jmfcam05v.main(jmfcam05v.java:34) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

我的程序没有检测到笔记本电脑上的内置网络摄像头,也没有检测到外部网络摄像头.我正在使用 jmf 捕获视频,并且我的所有网络摄像头都支持 vfw.请帮我解决这个问题.

my program is not detectin my inbuild webcam on laptop nor it is detecting external web cam.i am using jmf to capture the video and all my webcam's are vfw supported. Please help me to solve this issue.

推荐答案

您是否混合了 32 位和 64 位安装?我在 Windows 7 下遇到了类似的问题,这是由于 Windows 7、JRE 和 JMF 之间的 64 位不兼容.简而言之,JMF 只有 32 位,如果您的 JRE 是 64 位,则无法识别设备.

Are you mixing 32 and 64-bit installs? I had a similar problem under Windows 7, and it was due to 64-bit incompatibilities between Windows 7, JRE and JMF. In short, JMF is only 32-bit and won't recognize devices if your JRE is 64-bit.

按照这些说明后,我能够识别我的相机并避免找不到视频格式"以及 jmfstudio 未检测到视频捕获设备.

After following these instructions, I was able to recognize my camera and avoid the "Video Format not found" as well as the jmfstudio not detecting the video capture device.

这篇关于windows7上java编程的问题(在windows xp下运行良好)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆