Java基于JMF、FMJ实现打开摄像头实时录制

纪念平时一天的研究成果,本人学了那么多技术从未写过文章,今儿想贡献一下,望大家笑纳,多多指教

1、基于FMJ实现

     FMJ是一个Java开源项目它是JMF(Java Media Framework)的一个"替代品"并能够使用现存的第三方插件如jffmpeg和IBM的MPEG-4。

     它不需要给JRE安装任东西,还提供一些JMF没有的特性如:SWT支持等。

     项目主页:http://www.open-open.com/lib/view/home/1325062025468

     测试成功,兼容Window32、Linux32/64  由于官方未提供Window64的DLL支持,不能够在64下运行,期待官方早日更新

    所需Lib:fmj.jar、ffmpeg-java.jar、gstreamer-java.jar、jdom.jar、jheora-patch.jar、jl1.0.jar、jna.jar、jogg-0.0.7.jar、jorbis-0.0.15.jar、jspeex.jar、lti-civil-no_s_w_t.jar、

                    mp3spi1.9.4.jar、theora-java.jar、tritonus_share.jar、vorbisspi1.0.2.jar

    所需Dll:Window32【civil.dll、jdshow.dll】  Linux32/64 【libcivil.so】这些置于项目根目录

     实现代码:

package xs.camera.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.media.MediaLocator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.sf.fmj.ui.application.CaptureDeviceBrowser;
import net.sf.fmj.ui.application.ContainerPlayer;
import net.sf.fmj.ui.application.PlayerPanelPrefs;

public class Camera extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel cameraPanel=null;//摄像头面板
private static int num=0;

public Camera() throws Exception{
this.setTitle("摄像头截图工具");
this.setSize(480, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.cameraPanel=new JPanel();
this.getContentPane().setLayout(new BorderLayout());
this.getCont

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 Java 中使用摄像头录制视频,你可以使用 Java 的 OpenCV 库或者 Java Media Framework(JMF)。 使用 OpenCV 库: 1. 安装 OpenCV 库并配置环境变量。 2. 在 Java 代码中导入 OpenCV 库。 3. 初始化摄像头打开视频流。 4. 读取视频流中的每一帧,并将其显示在窗口中或者保存为视频文件。 以下是示例代码: ```java import org.opencv.core.*; import org.opencv.videoio.VideoCapture; public class VideoRecorder { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); VideoCapture camera = new VideoCapture(0); if (!camera.isOpened()) { System.out.println("Failed to open camera!"); return; } Mat frame = new Mat(); Size frameSize = new Size((int) camera.get(Videoio.CAP_PROP_FRAME_WIDTH), (int) camera.get(Videoio.CAP_PROP_FRAME_HEIGHT)); VideoWriter videoWriter = new VideoWriter("output.avi", VideoWriter.fourcc('M','J','P','G'), 30, frameSize, true); while (camera.read(frame)) { // Display the frame HighGui.imshow("Camera", frame); // Save the frame to video file videoWriter.write(frame); if (HighGui.waitKey(1) == 27) { break; } } camera.release(); videoWriter.release(); HighGui.destroyAllWindows(); } } ``` 使用 JMF: 1. 下载并安装 JMF。 2. 在 Java 代码中导入 JMF 库。 3. 创建一个 Player 对象并指定视频源。 4. 创建一个 DataSource 对象并将其连接到 Player 对象。 5. 创建一个 Processor 对象并连接到 DataSource 对象。 6. 将 Processor 对象的输出连接到一个 FileWriter 对象,用于保存视频文件。 7. 开始录制并停止录制时关闭所有对象。 以下是示例代码: ```java import java.io.File; import java.io.IOException; import javax.media.*; import javax.media.format.VideoFormat; import javax.media.protocol.DataSource; import javax.media.protocol.FileTypeDescriptor; public class VideoRecorder { public static void main(String[] args) { String outputFile = "output.avi"; int width = 640; int height = 480; int frameRate = 30; File file = new File(outputFile); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } // Create a player for the video source Player player = null; try { MediaLocator locator = new MediaLocator("vfw://0"); player = Manager.createRealizedPlayer(locator); } catch (Exception e) { e.printStackTrace(); } // Create a data source for the player DataSource dataSource = player.getDataSource(); // Create a processor for the data source Processor processor = null; try { processor = Manager.createProcessor(dataSource); } catch (Exception e) { e.printStackTrace(); } // Configure the processor processor.configure(); while (processor.getState() < Processor.Configured) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } // Set the output format TrackControl trackControl = (TrackControl) processor.getTrackControls()[0]; VideoFormat videoFormat = new VideoFormat(VideoFormat.MJPG, new Dimension(width, height), Format.NOT_SPECIFIED, Format.byteArray, (float) frameRate); trackControl.setFormat(videoFormat); // Create a file writer for the processor FileTypeDescriptor outputFileType = new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME); outputFileType.appendSuffix("avi"); DataSource outputDataSource = null; try { outputDataSource = Manager.createDataSource(new MediaLocator("file:" + outputFile)); } catch (Exception e) { e.printStackTrace(); } DataSink dataSink = null; try { dataSink = Manager.createDataSink(processor.getDataOutput(), outputDataSource); dataSink.open(); } catch (Exception e) { e.printStackTrace(); } // Connect the processor to the file writer processor.setContentDescriptor(new ContentDescriptor(outputFileType)); processor.realize(); while (processor.getState() < Processor.Realized) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } processor.start(); dataSink.start(); System.out.println("Recording..."); // Wait for the user to stop recording try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } // Stop recording and close all objects System.out.println("Stopping recording..."); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } dataSink.stop(); dataSink.close(); processor.stop(); processor.close(); player.stop(); player.close(); System.out.println("Done."); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值