在 Android 上获取 IP Cam 视频流 (MJEPG)
我目前正在 3 人小组中做一个 AndAR 项目。我是负责将视频流传输到 Android 手机的人。 我给自己买了一台 D-Link DCS-920 IP 摄像机,我发现它使用 MJPEG 编解码器进行实时视频流,而网络服务器使用 Jview 来查看实时流。据我所知,MJPG 不是 Android 操作系统支持的文件类型,所以我想出了一个想法,我不使用 ImageView,而是使用 WebView 来流式传输视频。 我已经实现了一个非常简单的概念并且它有效!但问题是,刷新率很糟糕。 我获取视频图像(例如:http://192.168.1.10/image.jpg
)在WebView上查看并实现一个计时器来控制刷新率(应该将其设置为30fps,这每 33 毫秒刷新一次)但它只能达到 500 毫秒的间隔,任何较低的间隔我注意到它都不会更平滑,有时图像不会加载并且连接不稳定(例如:掉线)。难道是我刷新的速度比它能接收到的速度快? 但是在网络服务器Jview上就没有问题了!试图找到 jview 的源代码,但我没有希望。 无论如何,这是我编写的代码
package org.example.test;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class Webview extends Activity {
public WebView webView;
public Timer autoUpdate;
public String url;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings();
final EditText urlText = (EditText) findViewById(R.id.urlText);
//Buttons//////////////////------------
final Button connectB = (Button)findViewById(R.id.connectButton);
connectB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Actions goes here
url = urlText.getText().toString();
webView.loadUrl(url);
timerSetup();
}
});
final Button exitB = (Button)findViewById(R.id.exitButton);
exitB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Actions goes here
finish();
}
});
}
//refresh timer//////////////-----------------
public void timerSetup(){
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
//Actions goes here
webView.loadUrl(url);
}
});
}
}, 0, 500);//refresh rate time interval (ms)
}
}
,我是否可以以至少 15fps 的速度获取视频流/具有更快的刷新率? 是否有诸如 MJPEG 查看器/源代码之类的东西可以用来显示这些图像?
这是应用程序的屏幕截图 http://s945.photobucket.com/albums/ ad295/kevinybh/?action=view¤t=video.jpg (没有足够的点来发布图片):(
我只需要使视频流大约 15-30fps。任何建议/帮助将非常感激:) 谢谢!
I am currently doing an AndAR project in group of 3. I'm the person who's in charge of video streaming into the Android phone.
I got ourselves a D-Link DCS-920 IP camera and I found out that it uses MJPEG codec for the live video stream and the webserver uses Jview to view the live stream. As far as I know MJPG is not a supported file type for Android OS so I've came out with an idea, instead of using ImageView, I use WebView to stream the video.
I've implemented a very simple concept and it works! But the problem is, refresh rate is terrible.
I get the video image (eg: http://192.168.1.10/image.jpg
) to view on the WebView and implement a Timer to control the refresh rate (supposed to set it to 30fps, which is refresh every 33ms) but it can only go up to 500ms interval, any lower interval I notice it will not be any smoother,sometimes the image wont load and connection is unstable (eg: dropped). Could this be I'm refreshing at a rate faster than it can receive?
But over on the webserver Jview it has no problem! was trying to find the source code for the jview but I have no hope.
Anyway here's the code I've written
package org.example.test;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class Webview extends Activity {
public WebView webView;
public Timer autoUpdate;
public String url;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings();
final EditText urlText = (EditText) findViewById(R.id.urlText);
//Buttons//////////////////------------
final Button connectB = (Button)findViewById(R.id.connectButton);
connectB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Actions goes here
url = urlText.getText().toString();
webView.loadUrl(url);
timerSetup();
}
});
final Button exitB = (Button)findViewById(R.id.exitButton);
exitB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Actions goes here
finish();
}
});
}
//refresh timer//////////////-----------------
public void timerSetup(){
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
//Actions goes here
webView.loadUrl(url);
}
});
}
}, 0, 500);//refresh rate time interval (ms)
}
}
Is there anyway I can get the video stream in by at least 15fps/have a faster refresh rate?
Are there any such thing as MJPEG viewer/source code that I can use to display these images?
here's the screenshot of the app
http://s945.photobucket.com/albums/ad295/kevinybh/?action=view¤t=video.jpg
(not enough points to post pictures) :(
I just need to make the video stream around 15-30fps. any suggestions/help would be very deeply appreciated :) Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在android上,如果我们通过CPU解码jpeg,将花费40-100ms。如果我们想以 15-30fps 播放 mjpeg,我们需要硬件 jpeg 解码器。
On android, if we decode a jpeg by CPU, it will cost 40-100ms. If we want to play mjpeg to 15-30fps, we need hardware jpeg decoder.
有一个有用的之前的SO讨论和这个很棒的代码。您可以尝试让我们知道这是否适合您。
There was a useful previous SO discussion and this great one with code. Would you try and let us know if that works for you.
您可以使用 MjpegView 类直接显示 mjpeg 流。
https://code.google.com/p/android-camera-axis/source/browse/trunk/serealizes/src/de/mjpegsample/MjpegView/MjpegView.java?r=33
你'必须在此类上实现一些 AsyncTasks 才能正常工作。
祝你好运
You can use MjpegView class to display mjpeg stream directly.
https://code.google.com/p/android-camera-axis/source/browse/trunk/serealisation/src/de/mjpegsample/MjpegView/MjpegView.java?r=33
You'll have to implement some AsyncTasks on this class to works fine.
Good luck
您可以使用 Raspberry PI 代替 Arduino,它应该有足够的 CPU 功率来控制车辆并同时转换视频流。当然,您需要将所有 Arduino 软件移植到 Raspberry...
Instead of an Arduino you could use a Raspberry PI, it should have enough CPU power to control the vehicle and to convert the video stream at the same time. Sure, you'll need to port all of your Arduino software to Raspberry...
MJPEG 是将动态视频传送到移动设备的一种极其低效的方式,因为每一帧都被压缩为它自己的独立图片。对于不需要视频的应用程序(上周有人询问是否有摄像机观看等待队列),每秒左右推送一个静态帧的解决方案听起来不错。
如果您需要动态视频,我建议您在网络服务器上将 MJPEG 转码为使用帧到帧压缩的受支持视频格式。这将导致通过用户的 3g 连接以及从服务器到所有客户端推送的数据大大减少。您应该只需要运行一种转码引擎来支持所有客户端 - 并且您将能够对 Android 和 Android 使用相同的转码引擎。 iPhone 设备,但如果您的相机输出足够好,您可能还希望平板电脑和个人电脑具有更高分辨率的输出。
MJPEG is a terribly inefficient way to deliver motion video to a mobile device, because each frame is compressed as it's own independent picture. For an application which doesn't need video (someone was asking about a camera watching waiting lines last week) your solution of pushing a static frame every second or so sounds good.
If you need motion video, I would recommend you do transcoding on your webserver from MJPEG to a supported video format which utilizes frame-to-frame compression. This will result in far less data to push, both over the user's 3g connection and from your server to all of its clients. You should only need to run one transcoding engine to support all clients - and you'll be able to use the same one for android & iphone devices, though you may want to also have a higher resolution output for tablets and pc's if your camera output is good enough to justify it.