如何在android中对视频流进行图像处理?

时间:2012-03-07 06:22:28

标签: android

我想(实时)拍摄视频流并对图像进行简单的图像处理。 例如 - 我想在制作视频流的所有图像上制作直方图(这意味着我需要为制作视频的所有图像制作直方图)

问题: 我如何拍摄实时视频? 如何实时访问设备视频并分离制作视频的图像?

1 个答案:

答案 0 :(得分:0)

public class WatchVideo extends Activity {
    VideoView mVideoView; 
    String TAG = "------------------";
    String current = "";
    String mPath="";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

         mVideoView=new VideoView(this);

       //specify video path
        String path = ?;

        playVideo(path);
    }

     private void playVideo(String path) 
     {
                    try {   

                        Log.v(TAG, "path: " + path);
                        if (path == null || path.length() == 0) {
                            Toast.makeText(WatchVideo.this, "File URL/path is empty",
                                    Toast.LENGTH_LONG).show();

                        } else {
                            // If the path has not changed, just start the media player
                            if (path.equals(current) && mVideoView != null) {
                                mVideoView.start();
                                mVideoView.requestFocus();
                                return;
                            }
                            current = path;

                            mVideoView.setVideoPath(getDataSource(path));
                            mVideoView.start();
                            mVideoView.requestFocus();


                        }
                    } catch (Exception e) {
                     Log.e(TAG, "error: " + e.getMessage(), e);
                        if (mVideoView != null) {
                            mVideoView.stopPlayback();
                        }
                    }
                }

                private String getDataSource(String path) throws IOException {
                    if (!URLUtil.isNetworkUrl(path)) {
                        return path;
                    } else {
                        URL url = new URL(path);
                        URLConnection cn = url.openConnection();
                        cn.connect();
                        InputStream stream = cn.getInputStream();
                        if (stream == null)
                            throw new RuntimeException("stream is null");
                        File temp = File.createTempFile("mediaplayertmp", "dat");
                        temp.deleteOnExit();   
                        String tempPath = temp.getAbsolutePath();
                        FileOutputStream out = new FileOutputStream(temp);
                        byte buf[] = new byte[128];
                        do {
                            int numread = stream.read(buf);
                            if (numread <= 0)
                                break;
                            out.write(buf, 0, numread);
                        } while (true);
                        try {
                            stream.close();
                        } catch (IOException ex) {
                         Log.e(TAG, "error: " + ex.getMessage(), ex);
                     }
                    return tempPath;
                 }



                }

}