在java中从网络摄像头捕获图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/276292/
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
Capturing image from webcam in java?
提问by Divide By Zero
How can I continuously capture images from a webcam?
如何从网络摄像头连续捕捉图像?
I want to experiment with object recognition (by maybe using java media framework).
我想尝试对象识别(通过使用 java 媒体框架)。
I was thinking of creating two threads
我正在考虑创建两个线程
one thread:
一个线程:
- Node 1: capture live image
- Node 2: save image as "1.jpg"
- Node 3: wait 5 seconds
- Node 4: repeat...
- 节点 1:捕获实时图像
- 节点 2:将图像另存为“1.jpg”
- 节点 3:等待 5 秒
- 节点 4:重复...
other thread:
其他线程:
- Node 1: wait until image is captured
- Node 2: using the "1.jpg" get colors from every pixle
- Node 3: save data in arrays
- Node 4: repeat...
- 节点 1:等待图像被捕获
- 节点 2:使用“1.jpg”从每个像素中获取颜色
- 节点 3:将数据保存在数组中
- 节点 4:重复...
回答by goldenmean
I believe the web-cam application software which comes along with the web-cam, or you native windows webcam software can be run in a batch script(windows/dos script) after turning the web cam on(i.e. if it needs an external power supply). In the bacth script , u can add appropriate delay to capture after certain time period. And keep executing the capture command in loop.
我相信网络摄像头随附的网络摄像头应用程序软件或您的原生 windows 网络摄像头软件可以在打开网络摄像头后在批处理脚本(windows/dos 脚本)中运行(即,如果它需要外部电源)供应)。在 bacth 脚本中,您可以添加适当的延迟以在一定时间段后进行捕获。并继续循环执行捕获命令。
I guess this should be possible
我想这应该是可能的
-AD
-广告
回答by Karl
Java usually doesn't like accessing hardware, so you will need a driver program of some sort, as goldenmean said. I've done this on my laptop by finding a command line program that snaps a picture. Then it's the same as goldenmean explained; you run the command line program from your java program in the takepicture() routine, and the rest of your code runs the same.
Java通常不喜欢访问硬件,所以你需要某种驱动程序,正如goldenmean所说。我在我的笔记本电脑上找到了一个可以拍摄图片的命令行程序。那么就和goldenmean解释的一样;您从 takepicture() 例程中的 java 程序运行命令行程序,其余代码运行相同。
Except for the part about reading pixel values into an array, you might be better served by saving the file to BMP, which is nearly that format already, then using the standard java image libraries on it.
除了将像素值读入数组的部分外,将文件保存为 BMP(已经接近该格式),然后在其上使用标准 Java 图像库可能会更好地为您服务。
Using a command line program adds a dependency to your program and makes it less portable, but so was the webcam, right?
使用命令行程序会增加程序的依赖性并降低其可移植性,但网络摄像头也是如此,对吗?
回答by ArnauVP
I have used JMF on a videoconference application and it worked well on two laptops: one with integrated webcam and another with an old USB webcam. It requires JMF being installed and configured before-hand, but once you're done you can access the hardware via Java code fairly easily.
我在视频会议应用程序中使用了 JMF,它在两台笔记本电脑上运行良好:一台带有集成网络摄像头,另一台带有旧的 USB 网络摄像头。它需要事先安装和配置 JMF,但是一旦完成,您就可以很容易地通过 Java 代码访问硬件。
回答by Dan Monego
There's a pretty nice interface for this in processing, which is kind of a pidginjava designed for graphics. It gets used in some image recognition work, such as that link.
在处理中有一个非常好的界面,它是一种为图形设计的pidginjava。它被用于一些图像识别工作,例如那个链接。
Depending on what you need out of it, you might be able to load the video library that's used there in java, or if you're just playing around with it you might be able to get by using processing itself.
根据您的需要,您可能能够加载在 java 中使用的视频库,或者如果您只是在玩它,您可能可以通过使用处理本身来获得。
回答by rics
回答by rics
FMJ can do this, as can the supporting library it uses, LTI-CIVIL. Both are on sourceforge.
FMJ 可以做到这一点,它使用的支持库 LTI-CIVIL 也可以做到这一点。两者都在 sourceforge 上。
回答by rics
http://grack.com/downloads/school/enel619.10/report/java_media_framework.html
http://grack.com/downloads/school/enel619.10/report/java_media_framework.html
Using the Player with Swing
使用带有 Swing 的播放器
The Player can be easily used in a Swing application as well. The following code creates a Swing-based TV capture program with the video output displayed in the entire window:
Player 也可以在 Swing 应用程序中轻松使用。以下代码创建了一个基于 Swing 的电视捕获程序,视频输出显示在整个窗口中:
import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.event.*;
public class JMFTest extends JFrame {
Player _player;
JMFTest() {
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
_player.stop();
_player.deallocate();
_player.close();
System.exit( 0 );
}
});
setExtent( 0, 0, 320, 260 );
JPanel panel = (JPanel)getContentPane();
panel.setLayout( new BorderLayout() );
String mediaFile = "vfw://1";
try {
MediaLocator mlr = new MediaLocator( mediaFile );
_player = Manager.createRealizedPlayer( mlr );
if (_player.getVisualComponent() != null)
panel.add("Center", _player.getVisualComponent());
if (_player.getControlPanelComponent() != null)
panel.add("South", _player.getControlPanelComponent());
}
catch (Exception e) {
System.err.println( "Got exception " + e );
}
}
public static void main(String[] args) {
JMFTest jmfTest = new JMFTest();
jmfTest.show();
}
}
回答by Rose
Recommand using FMJ for multimedia relatived java app.
建议将 FMJ 用于多媒体相关的 Java 应用程序。
回答by Sam
JMyron is very simple for use. http://webcamxtra.sourceforge.net/
JMyron 使用起来非常简单。 http://webcamxtra.sourceforge.net/
myron = new JMyron();
myron.start(imgw, imgh);
myron.update();
int[] img = myron.image();
回答by Hugo
You can try Marvin Framework. It provides an interface to work with cameras. Moreover, it also provides a set of real-time video processing features, like object tracking and filtering.
你可以试试Marvin 框架。它提供了一个使用相机的界面。此外,它还提供了一组实时视频处理功能,如对象跟踪和过滤。
Take a look!
看一看!
Real-time Video Processing Demo:
http://www.youtube.com/watch?v=D5mBt0kRYvk
实时视频处理演示:http:
//www.youtube.com/watch?v=D5mBt0kRYvk
You can use the source below. Just save a frame using MarvinImageIO.saveImage()every 5 second.
您可以使用以下来源。只需每 5 秒使用MarvinImageIO.saveImage()保存一帧。
Webcam video demo:
网络摄像头视频演示:
public class SimpleVideoTest extends JFrame implements Runnable{
private MarvinVideoInterface videoAdapter;
private MarvinImage image;
private MarvinImagePanel videoPanel;
public SimpleVideoTest(){
super("Simple Video Test");
videoAdapter = new MarvinJavaCVAdapter();
videoAdapter.connect(0);
videoPanel = new MarvinImagePanel();
add(videoPanel);
new Thread(this).start();
setSize(800,600);
setVisible(true);
}
@Override
public void run() {
while(true){
// Request a video frame and set into the VideoPanel
image = videoAdapter.getFrame();
videoPanel.setImage(image);
}
}
public static void main(String[] args) {
SimpleVideoTest t = new SimpleVideoTest();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
For those who just want to take a single picture:
对于那些只想拍一张照片的人:
WebcamPicture.java
网络摄像头图片.java
public class WebcamPicture {
public static void main(String[] args) {
try{
MarvinVideoInterface videoAdapter = new MarvinJavaCVAdapter();
videoAdapter.connect(0);
MarvinImage image = videoAdapter.getFrame();
MarvinImageIO.saveImage(image, "./res/webcam_picture.jpg");
} catch(MarvinVideoInterfaceException e){
e.printStackTrace();
}
}
}