I want to ask how to capture image from a streaming video of an ip camera that shown in my app through video view layout in my app, when i click a button it will capture the image that shown in video view. i use a rtsp protocol ip camera..
package com.javacodegeeks.androidvideoviewexample;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
public class AndroidVideoViewExample extends Activity {
private VideoView myVideoView;
private int position = 0;
private ProgressDialog progressDialog;
private MediaController mediaControls;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.activity_main);
if (mediaControls == null) {
mediaControls = new MediaController(AndroidVideoViewExample.this);
}
// Find your VideoView in your video_main.xml layout
myVideoView = (VideoView) findViewById(R.id.video_view);
// Create a progressbar
progressDialog = new ProgressDialog(AndroidVideoViewExample.this);
// Set progressbar title
progressDialog.setTitle("JavaCodeGeeks Android Video View Example");
// Set progressbar message
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
// Show progressbar
progressDialog.show();
try {
myVideoView.setMediaController(mediaControls);
myVideoView.setVideoURI(Uri.parse("rtsp://192.168.2.12/user=admin&password=&channel=1&stream=0.sdp?"));
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
myVideoView.pause();
}
}
});
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
myVideoView.pause();
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
position = savedInstanceState.getInt("Position");
myVideoView.seekTo(position);
}
}
this is the coding that I found in this group, it work awesomely...now I want to add a button to capture the image,
what I have found that we can use mediadataretriever method and save it to sd card? is that true?
You can try to capture image also in a bit another way using cgi service for example available with dahua IPC-HDW2231T-AS-S2.
http://CAM-IP/cgi-bin/snapshot.cgi
In my case I use it to invoke snapshot creation and automatically store it on ftp server, but if you want get image from request the rule will be the same.
To make that request without getting a 401 - unathorized result code you have to implement digest authentication . You can make it on your way or use like me the bare-bones-digest dependency.
Then you can make AsyncTask class to make it asynchronous (below example in kotlin)
class RequestTask :
AsyncTask<String?, String?, Boolean?>() {
override fun doInBackground(vararg params: String?): Boolean? {
// Step 1. Create the connection
val url = URL(params[0])
var connection =
url.openConnection() as HttpURLConnection
// Step 2. Make the request and check to see if the response contains an authorization challenge
if (connection.responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
// Step 3. Create a authentication object from the challenge...
val auth: DigestAuthentication = DigestAuthentication.fromResponse(connection)
// ...with correct credentials
auth.username(username).password(password)
// Step 4 (Optional). Check if the challenge was a digest challenge of a supported type
if (!auth.canRespond()) {
// No digest challenge or a challenge of an unsupported type - do something else or fail
return false
}
// Step 5. Create a new connection, identical to the original one...
connection = url.openConnection() as HttpURLConnection
// ...and set the Authorization header on the request, with the challenge response
connection.setRequestProperty(
DigestChallengeResponse.HTTP_HEADER_AUTHORIZATION,
auth.getAuthorizationForRequest("GET", connection.url.path)
)
}
connection.connect() //there you can also get inputStream from connection for local store of snapshot
val responseCode = connection.responseCode
val cLength = connection.contentLength
return responseCode == 200
}
}
and to execute use:
RequestTask().execute("http://$CAM-IP/cgi-bin/snapshot.cgi")
If you encounter the Cleartext HTTP traffic not permitted
error then see that for help.
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.