从实时视频设备捕获图像以供基于 Java 的应用程序使用的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/115835/
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
What is the best method to capture images from a live video device for use by a Java-based application?
提问by Bob Cross
I am looking into an image processing problem for semi-real time detection of certain scenarios. My goal is to have the live video arrive as Motion JPEG frames in my Java code somehow.
我正在研究用于某些场景的半实时检测的图像处理问题。我的目标是以某种方式让实时视频以 Motion JPEG 帧的形式出现在我的 Java 代码中。
I am familiar with the Java Media Frameworkand, sadly, I think we can consider that an effectively dead API. I am also familiar with Axis boxesand, while I really like their solution, I would appreciate any critical feedback on my specific points of interest.
我熟悉Java 媒体框架,遗憾的是,我认为我们可以认为这是一个有效的死 API。我也熟悉Axis 框,虽然我真的很喜欢他们的解决方案,但我会感谢任何关于我的特定兴趣点的重要反馈。
This is how I define "best" for the purpose of this discussion:
为了讨论的目的,我是这样定义“最佳”的:
- Latency - if I'm controlling the camera using this video stream, I would like to keep my round-trip latency at less than 100 milliseconds if possible. That's measured as the time between my control input to the time when I see the visible change. EDIT some time later: another thing to keep in mind is that camera control is likely to be a combination of manual and automatic (event triggers). We need to see those pictures right away, even if the high quality feed is archived separately.
- Cost - free / open source is better than not free.
- Adjustable codec parameters - I need to be able to tune the codec for certain situations. Sometimes a high-speed low-resolution stream is actually easier to process.
- "Integration" with Java - how much trouble is it to hook this solution to my code? Am I sending packets over a socket? Hitting URLs? Installing Direct3D / JNI combinations?
- Windows / Linux / both? - I would prefer an operating system agnostic solution because I have to deliver to several flavors of OS but there may be a solution that is optimal for one but not the other.
- 延迟 - 如果我使用此视频流控制相机,我希望尽可能将往返延迟保持在 100 毫秒以下。这是用我的控制输入到我看到可见变化的时间之间的时间来衡量的。一段时间后编辑:要记住的另一件事是相机控制可能是手动和自动(事件触发器)的组合。我们需要立即查看这些图片,即使高质量的提要是单独存档的。
- 成本 - 免费/开源总比不免费好。
- 可调整的编解码器参数 - 我需要能够在某些情况下调整编解码器。有时,高速低分辨率流实际上更容易处理。
- 与 Java 的“集成”——将这个解决方案挂接到我的代码上有多麻烦?我是否通过套接字发送数据包?命中网址?安装 Direct3D / JNI 组合?
- Windows / Linux / 两者?- 我更喜欢与操作系统无关的解决方案,因为我必须提供多种操作系统版本,但可能有一种解决方案最适合一种而不是另一种。
NOTE: I am aware of other image / video capture codecs and that is not the focus of this question. I am specifically notinterested in streaming APIs (e.g., MPEG4) due to the loss of frame accuracy. However, if there is a solution to my question that delivers another frame-accurate data stream, please chime in.
注意:我知道其他图像/视频捕获编解码器,这不是这个问题的重点。由于帧精度的损失,我对流式 API(例如 MPEG4)特别不感兴趣。但是,如果我的问题有解决方案可以提供另一个帧精确的数据流,请加入。
Follow-up to this question: at this point, I am strongly inclined to buy appliances such as the Axis video encodersrather than trying to capture the video in software or on the PC directly. However, if someone has alternatives, I'd love to hear them.
这个问题的后续:在这一点上,我强烈倾向于购买诸如Axis 视频编码器之类的设备,而不是尝试直接在软件中或在 PC 上捕获视频。但是,如果有人有其他选择,我很想听听他们的意见。
回答by CMPalmer
Have you ever looked at Processing.org? It's basically a simplified application framework for developing "artsy" applications and physical computing platforms, but it's based on Java and you can dig down to the "real" Java underneath.
你有没有看过Processing.org?它基本上是一个简化的应用程序框架,用于开发“艺术”应用程序和物理计算平台,但它基于 Java,您可以深入挖掘底层的“真正”Java。
The reason it came to mind is that there are several video librariesfor Processing which are basically Java components (at least I think they are - the site has all the technical information you might need). There is a tutorialon using the Processing libraries and tools in the Eclipse IDE. There are also numerous exampleson video capture and processing.
想到的原因是有几个用于处理的视频库,它们基本上是 Java 组件(至少我认为它们是 - 该站点包含您可能需要的所有技术信息)。有一个关于在 Eclipse IDE 中使用处理库和工具的教程。还有许多关于视频捕获和处理的示例。
Even if you can't use the libraries directly, Processing is a great language/environment for working out algorithms. There are several great examples of image and video capture and real-time processing there.
即使您不能直接使用这些库,Processing 也是一种用于计算算法的出色语言/环境。那里有几个很好的图像和视频捕获以及实时处理的例子。
回答by Greg Mattes
Regarding the dead-ness of JMF, are you aware of the FMJ implementation? I don't know whether it qualifies as the "best" solution, but it's probably worth adding to the discussion.
关于 JMF 的死机,你知道FMJ 的实现吗?我不知道它是否有资格成为“最佳”解决方案,但它可能值得加入讨论。
回答by Greg Mattes
FMJ can definitely capture video and turn it into MJPEG frames.
FMJ 绝对可以捕获视频并将其转换为 MJPEG 帧。
回答by gtiwari333
This JavaCV implementation works fine.
这个 JavaCV 实现工作正常。
CODE:
代码:
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
public class CaptureImage {
private static void captureFrame() {
// 0-default camera, 1 - next...so on
final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
try {
grabber.start();
IplImage img = grabber.grab();
if (img != null) {
cvSaveImage("capture.jpg", img);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
captureFrame();
}
}
There is also post on viewing live videofrom Camera .And configuration for JavaCV :
还有关于从 Camera观看实时视频的帖子。以及 JavaCV 的配置:
I think this will meet your requirements.
我认为这将满足您的要求。
回答by syb0rg
This is my JavaCV implementation with high resolution video output and no noticeable drop in the frame-rate than other solutions (only when my webcam refocuses do I notice a slight drop, only for a moment though).
这是我的 JavaCV 实现,具有高分辨率视频输出,与其他解决方案相比,帧速率没有明显下降(只有当我的网络摄像头重新聚焦时,我才会注意到轻微下降,虽然只是片刻)。
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.JFrame;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.OpenCVFrameRecorder;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
public class Webcam implements Runnable {
IplImage image;
static CanvasFrame frame = new CanvasFrame("Web Cam");
public static boolean running = false;
public Webcam()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void run()
{
try
{
grabber.setImageWidth(800);
grabber.setImageHeight(600);
grabber.start();
while (running)
{
IplImage cvimg = grabber.grab();
BufferedImage image;
if (cvimg != null)
{
// opencv_core.cvFlip(cvimg, cvimg, 1); // mirror
// show image on window
image = cvimg.getBufferedImage();
frame.showImage(image);
}
}
grabber.stop();
frame.dispose();
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String... args)
{
Webcam webcam = new Webcam();
webcam.start();
}
public void start()
{
new Thread(this).start();
running = true;
}
public void stop()
{
running = false;
}
}
回答by Gabriel Ambrósio Archanjo
Below is shown a very simple implementation using Marvin Framework. Using Marvin you can add real time video processing easily.
下面显示了一个使用Marvin Framework的非常简单的实现。使用 Marvin,您可以轻松添加实时视频处理。
import javax.swing.JFrame;
import marvin.gui.MarvinImagePanel;
import marvin.image.MarvinImage;
import marvin.video.MarvinJavaCVAdapter;
import marvin.video.MarvinVideoInterface;
public class SimpleVideoTest extends JFrame implements Runnable{
private MarvinVideoInterface videoAdapter;
private MarvinImage image;
private MarvinImagePanel videoPanel;
public SimpleVideoTest(){
super("Simple Video Test");
// Create the VideoAdapter and connect to the camera
videoAdapter = new MarvinJavaCVAdapter();
videoAdapter.connect(0);
// Create VideoPanel
videoPanel = new MarvinImagePanel();
add(videoPanel);
// Start the thread for requesting the video frames
new Thread(this).start();
setSize(800,600);
setVisible(true);
}
public static void main(String[] args) {
SimpleVideoTest t = new SimpleVideoTest();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void run() {
while(true){
// Request a video frame and set into the VideoPanel
image = videoAdapter.getFrame();
videoPanel.setImage(image);
}
}
}
Another exampleapplying multiple algorithms for real time video processing.
另一个应用多种算法进行实时视频处理的例子。