简体   繁体   中英

OpenCV Android Studio Java IP Camera Not Connecting

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!");
}

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

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM