如何从android中的视频视图中显示的ip camera的流视频中捕获图像

时间:2014-12-24 04:38:21

标签: android video-streaming ip-camera android-videoview

我想问一下如何通过我的应用程序中的视频视图布局从我的应用程序中显示的ip camera的流式视频中捕获图像,当我点击一个按钮时,它将捕获视频视图中显示的图像。我使用rtsp协议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);
    }
}

这是我在这个组中找到的编码,它的工作非常棒...现在我想添加一个按钮来捕获图像,

我发现我们可以使用mediadataretriever方法并将其保存到SD卡? 那是真的吗?

1 个答案:

答案 0 :(得分:1)

您还可以尝试使用cgi服务以另一种方式捕获图像,例如大华IPC-HDW2231T-AS-S2附带的cgi服务。

http://CAM-IP/cgi-bin/snapshot.cgi

在我的情况下,我使用它来调用快照创建并将其自动存储在ftp服务器上,但是如果要从请求中获取图像,则规则将是相同的。

要发出该请求而未获得401-未授权的结果代码,您必须实现摘要式身份验证。您可以随便使用它,也可以像我一样使用bare-bones-digest依赖项。

然后您可以使AsyncTask类使其异步(在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
        }
    }

并执行使用:

RequestTask().execute("http://$CAM-IP/cgi-bin/snapshot.cgi")

如果遇到Cleartext HTTP traffic not permitted错误,请向that寻求帮助。