`
C_J
  • 浏览: 127877 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java Media Framework本地玩转摄像头

阅读更多

1、简介
The JavaTM Media Framework (JMF) is an application programming interface (API) for incorporating media data such as audio and video into
Java applications and applets. It is specifically designed to take advantage of Java platform features.

这个框架是SUN公司出的,最新更新至2003年,用JMF来处理图像和音频设备并直接应用到JAVA应用程序中,虽然年代有点久远,但却还是可以用的。

 

The JMF 2.1.1 Reference Implementation supports SunVideo / SunVideoPlus capture devices on Solaris. On Windows, most capture devices
 that have VFW drivers are supported. On Linux, devices that have a Video4Linux driver are expected to work, but not extensively tested.
今天小试了下,自己的laptop也有个摄像头,录制了一段视频到本地,第一次玩这个东西:)

 

2、模型

JMF的模型如下:


基本上是摄像头数据、文件数据、网络媒体流通过process输出到设备、文件或网络中。

 

Design Goals for JMF:

1、Be easy to program

2、Support capturing media data

3、Enable the development of media streaming and conferencing applications in Java

4、Enable advanced developers and technology providers to implement custom solutions based on the existing API and easily integrate new features with the existing framework

5、Provide access to raw media data

6、Enable the development of custom, downloadable demultiplexers, codecs, effects processors, multiplexers, and renderers (JMF plug-ins)

 

Design Goals for RTP:(RTP是JMF数据流在网络上的传输协议)

1、Enable the development of media streaming and conferencing applications in Java

2、Support media data reception and transmission using RTP and RTCP

3、Support custom packetizer and depacketizer plug-ins through the JMF 2.0 plug-in architecture.

4、Be easy to program

 

 3、媒体数据展现

 Streaming Media

 媒体流,可以存储为多种文件格式或通过HTTP,RTP等协议传输在网络上。

 

Media Presentation

Most time-based media is audio or video data that can be presented through output devices such as speakers and monitors. Such devices are the

most common destination for media data output. Media streams can also be sent to other destinations--for example, saved to a file or transmitted

across the network. An output destination for media data is sometimes referred to as a data sink.

 

Media Data Storage and Transmission

 
A DataSink is used to read media data from a DataSource and render the media to some destination--generally a destination other

than a presentation device. A particular DataSink might write data to a file, write data across the network, or function as an RTP broadcaster.

 

DataSink用来做数据存储和传输。

 

开始presenting time-based media

 

Presenting Time-Based Media

To present time-based media such as audio or video with JMF, you use a Player. Playback can be controlled programmatically, or you can display a control-panel component that enables the user to control playback interactively. If you have several media streams that you want to play,

you need to use a separate Player for each one. to play them in sync, you can use one of the Player objects to control the operation of the others.

 

基本上是说播放实时多媒体需要用到Player,且每个单独的媒体需要单独的Player。

代码如下:

// create processor   
        ProcessorModel processorModel = new ProcessorModel(mixedDataSource, outputFormat, outputType);    
// 创建model,类似JTable控件的 JModel,The ProcessorModel defines the input and output requirements for the Processor    
        Processor processor = null;    
        try   
        {   
            processor = Manager.createRealizedProcessor(processorModel);// 创建model,类似JTable控件的 JModel
        }   
        catch (IOException e) { Stdout.logAndAbortException(e); }    
        catch (NoProcessorException e) { Stdout.logAndAbortException(e); }    
        catch (CannotRealizeException e) { Stdout.logAndAbortException(e); } 

 

创建processor后,可以装进不同的compont里来present。

 

For example, you can call getControls to determine if a Player supports the CachingControl interface.

 

Control[] controls = player.getControls(); 

for (int i = 0; i < controls.length; i++) 
{ 
  if (controls[i] instanceof CachingControl) 
   { cachingControl = (CachingControl) controls[i]; 
   } 
}

 

JMF提供了很多“播放器”,当然也提供了基本的控制,比如播放、停止以及播放信息等等。

控制产生事件。

Responding to Media Events

ControllerListener is an asynchronous interface for handling events generated by Controller objects.

for example:

if (event instanceof EventType){   
 ...   
 } else if (event instanceof OtherEventType){    
 ...   
 }  
if (event instanceof EventType){ ... } else if (event instanceof OtherEventType){ ... } 

  JMF提供ControllerAdapter:

player.addControllerListener(new ControllerAdapter() {
    public void endOfMedia(EndOfMediaEvent e) {
       Controller controller = e.getSource();
       controller.stop();
       controller.setMediaTime(new Time(0));
       controller.deallocate();
    }
 })

 

ControllerAdapter automatically dispatches the event to the appropriate event method, filtering out the events that you're not

 interested in.自动为您分发事件并过滤不需要的事件。

 

There is a movie in Applet...展现到applet

import java.applet.*;   
import java.awt.*;    
import java.net.*;    
import javax.media.*;    
  
public class PlayerApplet extends Applet implements ControllerListener {    
   Player player = null;    
   public void init() {    
      setLayout(new BorderLayout());    
      String mediaFile = getParameter("FILE");    
      try {    
         URL mediaURL = new URL(getDocumentBase(), mediaFile);    
         player = Manager.createPlayer(mediaURL);   
         player.addControllerListener(this);    
      }    
      catch (Exception e) {    
         System.err.println("Got exception "+e);    
      }   
   }   
   public void start() {    
      player.start();   
   }   
   public void stop() {    
      player.stop();   
      player.deallocate();   
   }   
   public void destroy() {    
      player.close();   
   }   
   public synchronized void controllerUpdate(ControllerEvent event) {    
      if (event instanceof RealizeCompleteEvent) {    
         Component comp;   
         if ((comp = player.getVisualComponent()) != null)    
            add ("Center", comp);     
         if ((comp = player.getControlPanelComponent()) != null)    
            add ("South", comp);             
         validate();   
      }   
   }   
}  

 

 

 Presenting RTP Media Streams

Creating a Player for RTP session.

 

String url= "rtp://224.144.251.104:49150/audio/1";   
  
        MediaLocator mrl= new MediaLocator(url);    
           
        if (mrl == null) {    
            System.err.println("Can't build MRL for RTP");    
            return false;    
        }   
           
        // Create a player for this rtp session    
        try {    
            player = Manager.createPlayer(mrl);   
        } catch (NoPlayerException e) {    
            System.err.println("Error:" + e);    
            return false;    
        } catch (MalformedURLException e) {    
            System.err.println("Error:" + e);    
            return false;    
        } catch (IOException e) {    
            System.err.println("Error:" + e);    
            return false;    
        }  

 

4、媒体数据处理

Converting Media Data from One Format to Another

你可以调整媒体的格式。

Specifying the Media Destination

你可以指定录像保存的方式,比如输出到文件, 输出到另一个player。

 

 Saving captured media data to a file

 

DataSink sink;   
 MediaLocator dest = new MediaLocator("file://newfile.wav");    
 try {    
     sink = Manager.createDataSink(p.getDataOutput(), dest);    
     sink.open();   
     sink.start();   
 } catch (Exception) {}  

 

 

5、扩展JMF

 

    Custom JMF plug-ins can be used seamlessly with Processors that support the plug-in API. After you implement your plug-in, you need to install it and register it with the PlugInManager to make it available to plug-in compatible Processors.

 

Implementing a Codec or Effect Plug-In

    Codec plug-ins are used to decode compressed media data, convert media data from one format to another, or encode raw media data into a compressed format.

    用户可以自行扩展压缩格式,以及格式转换。
Implementing a Renderer Plug-In

It is a single-input processing component with no output. Renderer plug-ins read data from a DataSource and typically present the media data to the user, but can also be used to provide access to the processed media data for use by another application or device.


To make a custom plug-in available to a Processor through the TrackControl interface, you need to register it with the PlugInManager

 

 // Name of the new plugin
 string GainPlugin = new String("COM.mybiz.media.GainEffect");
 
 // Supported input Formats
 Format[] supportedInputFormats = new Format[] {
 	     new AudioFormat(
 	         AudioFormat.LINEAR,
                 Format.NOT_SPECIFIED,
                 16,
                 Format.NOT_SPECIFIED,
                 AudioFormat.LITTLE_ENDIAN,
                 AudioFormat.SIGNED,
                 16,
                 Format.NOT_SPECIFIED,
                 Format.byteArray

 	     )
 };
 
 // Supported output Formats 
 Format[] supportedOutputFormats = new Format[] {
 	     new AudioFormat(
 	         AudioFormat.LINEAR,
                 Format.NOT_SPECIFIED,
                 16,
                 Format.NOT_SPECIFIED,
                 AudioFormat.LITTLE_ENDIAN,
                 AudioFormat.SIGNED,
                 16,
                 Format.NOT_SPECIFIED,
                 Format.byteArray
 	     )
 };
 
 // Add the new plug-in to the plug-in registry
 PlugInManager.addPlugIn(GainPlugin, supportedInputFormats, 
                          supportedOutputFormats, EFFECT);
 
 // Save the changes to the plug-in registry
 PlugInManager.commit();

 

 

Implementing a Protocol Data Source

A DataSource is an abstraction of a media protocol-handler. You can implement new types of DataSources to support additional protocols by extending PullDataSource,


Implementing a DataSink

JMF provides a default DataSink that can be used to write data to a file. Other types of DataSink classes can be implemented to facilitate writing data to the network or to other destinations.

 

6、事例代码

    this test program will capture the video and audio stream from your USB camera for 10 seconds and stores it on a file, named "testcam.avi".

 

 

 

  • 大小: 21.8 KB
  • camera.rar (4.1 KB)
  • 描述: camera
  • 下载次数: 401
分享到:
评论

相关推荐

    Java Media Framework API (JMF)

    个人整理的Java Media Framework API (JMF)的API,按Java6 API的风格整理

    Java Media Framework API Guide (PDF)

    Java Media Framework(JMF)API指南是一份详细阐述如何在Java平台上处理多媒体内容的重要文档。这份PDF指南旨在帮助开发者理解并有效地利用JMF来开发多媒体应用程序,如播放、捕获、处理音频和视频数据。以下是关于...

    Java Media Framework 基础教程

    Java Media Framework (JMF) 是一个Java平台上的多媒体处理API,它允许开发者处理各种媒体类型,包括音频和视频。本文档是一份基础教程,由Eric Olson撰写并由pawenwen业余时间翻译,旨在为初学者提供JMF的入门知识...

    java media framework简介

    ### Java Media Framework (JMF) 简介 Java Media Framework(简称JMF)是Sun Microsystems为Java平台提供的一套多媒体框架,它使得开发者能够轻松地处理音频、视频和其他形式的流媒体数据。JMF提供了丰富的API接口...

    Java Media Framework入门教程

    Java Media Framework(JMF)是Java平台上的一个多媒体处理框架,它允许开发者处理音频、视频以及复杂的媒体流。本教程将带你深入理解JMF的基本概念、工作原理以及如何在实际项目中应用它。 JMF的核心功能包括播放...

    Java Media Framework (JMF) 2.1.1e

    Java Media Framework(JMF)是Java平台上用于处理多媒体数据的核心框架。这个2.1.1e版本是一个较早但仍然广泛使用的发行版,主要用于开发能够播放、捕获、处理和控制音频、视频以及其它时间同步媒体的应用程序。...

    java swing 调用本地摄像头

    在Java Swing中,我们可以通过Java Media Framework (JMF) 或者更现代的JavaFX来实现调用本地摄像头的功能。下面将详细介绍如何在Java Swing应用中实现这一功能。 首先,让我们了解Java Media Framework (JMF)。JMF...

    Java_Media_Framework_基础教程

    Java 媒体框架(Java Media Framework,JMF)是Java平台上的一个核心API,用于处理多媒体数据,如音频、视频等。这个框架允许开发者创建能够播放、捕获、控制和处理多种媒体格式的应用程序。JMF提供了一个灵活的架构...

    Java Media Framework 2.0 API媒体框架官方编程接口文档chm版

    根据2013年7月最新文档制作自用,为备份共享。

    java 视频 摄像 调用电脑摄像头

    在Java中,通过Java Media Framework (JMF) 或者 JavaFX 可以实现对电脑摄像头的调用。下面将详细介绍这两个技术以及Spring框架在项目中的应用。 Java Media Framework (JMF) 是Java平台上的多媒体处理框架,它提供...

    JMF(java media framework)

    JMF(java media framework)帮助文档

    Java Media Framework basics

    ### Java Media Framework (JMF) 基础教程概述 #### 一、关于本教程 本教程主要介绍Java Media Framework(简称JMF),这是一款功能强大且多用途的应用程序编程接口(API),允许Java开发者以多种方式处理媒体数据...

    Java-Media-Framework-API-Guide.rar_JMF API_Java media framework_

    Java Media Framework (JMF) API 指南是Java开发者在处理多媒体内容时的重要参考资料。JMF 是一个可扩展的平台,用于播放、捕获、处理和操纵音频、视频和其他时间同步的数据流。这个API提供了丰富的功能,使得开发...

    USB摄像头java实现

    然而,JavaDCP并不是标准的Java库,因此在实际项目中,我们通常会依赖第三方库,如Java Media Framework (JMF) 或 OpenIMAJ。 1. **Java Media Framework (JMF)**:JMF是一个开源框架,它为音频、视频和流媒体的...

    Java Media Framework Guide

    ### Java Media Framework (JMF) 指南 #### 关于 JMF Java Media Framework (JMF) 是一个用于处理时间基媒体(如音频、视频)的开发平台。它为开发者提供了一套全面且强大的 API 来进行媒体的捕获、处理、存储以及...

    java 摄像头 拍照 程序 控制电脑摄像头拍照

    Java摄像头拍照程序是一种利用Java Media Framework (JMF) 或者其他相关库来访问并操作电脑摄像头,实现拍照功能的应用。JMF是Java平台上的多媒体框架,允许开发者处理音频、视频和流媒体数据。在这个程序中,我们将...

    java调用大华摄像头

    Java中可以使用开源库如Xuggler或者JMF(Java Media Framework)来处理这些视频流。 在给定的文件列表中,“DHTest.rar”可能是大华提供的SDK或者示例代码,而“java封装接口智能交通DEMO.zip”很可能是包含了Java...

Global site tag (gtag.js) - Google Analytics