英文:
OpenCV Android Studio Java IP Camera Not Connecting
问题
我目前正在尝试使用Android Studio开发一个应用程序,该应用程序连接两台IP摄像头并同时流传输它们。
我目前在使用OpenCV视频捕获
连接我的IP摄像头时遇到了问题,使用以下URL,该URL已由制造商确认为正确:
rtsp://(我的用户名):(我的密码)@192.168.0.34:554/stream1
这个URL在VLC播放器中也有效,但是当我运行应用程序时,我没有连接或错误消息,检查连接是否已打开时使用capture.isOpened()
。
我使用的摄像头是tp-link tapo 100
。
如果有任何建议,那将非常感谢,谢谢。
videoCapture = new VideoCapture();
videoCapture.open("rtsp://(我的用户名):(我的密码)@192.168.0.34:554/stream1");
if (!videoCapture.isOpened()) {
System.out.println("连接摄像头时出错");
} else {
System.out.println("视频已捕获!");
}
英文:
I am currently trying to develop an app using android studio that connects two IP cameras and streams them concurrently.
I am currently having issues connecting my IP cameras using OpenCV video capture
using the following url which was confirmed to be correct by the manufacturer
rtsp://(myUsername):(myPassword)@192.168.0.34:554/stream1
This url also works with VLC player but when I run the application I get no connection or error message checking that the connection is open with capture.isOpened().
The camera I'm using is a tp-link tapo 100
if anyone has any suggestions that would be great, thanks.
videoCapture = new VideoCapture();
videoCapture.open("rtsp://(myUsername):(myPassword)@192.168.0.34:554/stream1");
if (!videoCapture.isOpened()) {
System.out.println("ERROR CONNECTING TO CAMERA");
} else {
System.out.println("video is captured!");
}
答案1
得分: 1
请发布您从Logcat获取的错误日志。
OpenCV不像通常的HTTP链接那样处理rtsp
链接,您需要使用帧然后将这些帧转换为视频。
英文:
please post the error log you getting from logcat
open cv doesn't handle rtsp
links like the usual http
links you will need to use frames instead and then convert those frames into a video
答案2
得分: 0
尝试使用MediaPlayer,您只需要配置您的XML并添加额外的摄像头。
代码
public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
final static String USERNAME = "admin";
final static String PASSWORD = "camera";
final static String RTSP_URL = "Your URL Goes Hear";
private MediaPlayer _mediaPlayer;
private SurfaceHolder _surfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置一个全屏黑色窗口。
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setBackgroundDrawableResource(android.R.color.black);
setContentView(R.layout.activity_main);
// 配置渲染实时视频的视图。
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
_surfaceHolder = surfaceView.getHolder();
_surfaceHolder.addCallback(this);
_surfaceHolder.setFixedSize(1024, 600);
}
/*SurfaceHolder.Callback*/
@Override
public void surfaceChanged(SurfaceHolder sh, int f, int w, int h) {}
@Override
public void surfaceCreated(SurfaceHolder sh) {
_mediaPlayer = new MediaPlayer();
_mediaPlayer.setDisplay(_surfaceHolder);
Context context = getApplicationContext();
Map<String, String> headers = getRtspHeaders();
Uri source = Uri.parse(RTSP_URL);
try {
// 指定IP摄像头的URL和授权头。
_mediaPlayer.setDataSource(context, source, headers);
// 开始设置视频流的过程。
_mediaPlayer.setOnPreparedListener(this);
_mediaPlayer.prepare();
} catch (Exception e) {}
}
@Override
public void surfaceDestroyed(SurfaceHolder sh) {
_mediaPlayer.release();
}
private Map<String, String> getRtspHeaders() {
Map<String, String> headers = new HashMap<String, String>();
String basicAuthValue = getBasicAuthValue(USERNAME, PASSWORD);
headers.put("Authorization", basicAuthValue);
return headers;
}
private String getBasicAuthValue(String usr, String pwd) {
String credentials = usr + ":" + pwd;
int flags = Base64.URL_SAFE | Base64.NO_WRAP;
byte[] bytes = credentials.getBytes();
return "Basic " + Base64.encodeToString(bytes, flags);
}
/*MediaPlayer.OnPreparedListener*/
@Override
public void onPrepared(MediaPlayer mp) {
_mediaPlayer.start();
}
}
请注意,代码中的中文翻译是基于您提供的原始英文代码进行的。
英文:
Try using MediaPlayer you will just need to configure your XML and add for the additional camera.
Code
public class
MainActivity extends Activity implements MediaPlayer.OnPreparedListener, SurfaceHolder.Callback
{
final static String USERNAME = "admin";
final static String PASSWORD = "camera";
final static String RTSP_URL = "Your URL Goes Hear";
private MediaPlayer _mediaPlayer;
private SurfaceHolder _surfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set up a full-screen black window.
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setBackgroundDrawableResource(android.R.color.black);
setContentView(R.layout.activity_main);
// Configure the view that renders live video.
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
_surfaceHolder = surfaceView.getHolder();
_surfaceHolder.addCallback(this);
_surfaceHolder.setFixedSize(1024, 600);
}
/*SurfaceHolder.Callback*/
@Override
public void surfaceChanged(SurfaceHolder sh, int f, int w, int h) {}
@Override
public void surfaceCreated(SurfaceHolder sh)
{
_mediaPlayer = new MediaPlayer();
_mediaPlayer.setDisplay(_surfaceHolder);
Context context = getApplicationContext();
Map<String, String> headers = getRtspHeaders();
Uri source = Uri.parse(RTSP_URL);
try
{
// Specify the IP camera's URL and auth headers.
_mediaPlayer.setDataSource(context, source, headers);
// Begin the process of setting up a video stream.
_mediaPlayer.setOnPreparedListener(this);
_mediaPlayer.prepare();
}
catch (Exception e) {}
}
@Override
public void surfaceDestroyed(SurfaceHolder sh)
{
_mediaPlayer.release();
}
private Map<String, String> getRtspHeaders()
{
Map<String, String> headers = new HashMap<String, String>();
String basicAuthValue = getBasicAuthValue(USERNAME, PASSWORD);
headers.put("Authorization", basicAuthValue);
return headers;
}
private String getBasicAuthValue(String usr, String pwd)
{
String credentials = usr + ":" + pwd;
int flags = Base64.URL_SAFE | Base64.NO_WRAP;
byte[] bytes = credentials.getBytes();
return "Basic " + Base64.encodeToString(bytes, flags);
}
/*MediaPlayer.OnPreparedListener*/
@Override
public void onPrepared(MediaPlayer mp)
{
_mediaPlayer.start();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论