首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

Android-利用Java反照机制获取视频帧图像

2012-12-18 
Android--利用Java反射机制获取视频帧图像在Android多媒体开发中,我们必须了解系统MediaScanner的整体流程

Android--利用Java反射机制获取视频帧图像

在Android多媒体开发中,我们必须了解系统MediaScanner的整体流程,网上关于MediaScanner的文章非常多,感兴趣的朋友看以参看以下两篇博客:

http://blog.csdn.net/Innost/article/details/6083467(注重MediaScanner源码流程分析)

http://blog.csdn.net/zqiang_55/article/details/7060171(注重MediaScanner架构分析)

本文重点是怎样获取视频预览图像,并通过调用Gallery的MovieActivity播放视频,在此对MediaScanner就不做过多介绍。闲话少说,先上效果图:

         Android-利用Java反照机制获取视频帧图像                                       Android-利用Java反照机制获取视频帧图像

在了解了Mediascanner的基本功能后,在多媒体开发中我们常用的就是MediaStore这个类,MediaStore这个类包含了手机中的Audio、Video、Images多媒体信息。有关MediaStore的相关信息,请参考Android API。接下来直接讲解代码

Utils.java(该工具类的主要作用是获得SDcard中Video信息)

package com.jony.vedioframe;import java.util.ArrayList;import android.content.Context;import android.database.Cursor;import android.net.Uri;import android.provider.MediaStore;public class Utils {    public static ArrayList<String> getVideoData(Context context) {        Cursor cursor = null;        int count = 0;        ArrayList<String> data = new ArrayList<String>();        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;        String[] projection = new String[] { MediaStore.Video.Media.DATA };        try {            cursor = context.getContentResolver().query(uri, projection, null,                    null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);            if (cursor != null) {                count = cursor.getCount();                cursor.moveToFirst();                for (int i = 0; i < count; i++) {                    String string = cursor                            .getString(cursor                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));                    data.add(string);                    cursor.moveToNext();                }            }        } finally {            if (cursor != null) {                cursor.close();            }        }        return data;    }}

MainActivity.java(主要通过Java反射机制获得视频Frame图像)

package com.jony.vedioframe;import java.lang.reflect.Method;import java.util.ArrayList;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.FrameLayout;import android.widget.ImageView;public class MainActivity extends Activity {    private Button next;    private ImageView image;    private ImageView play;    private FrameLayout vedio;    private ArrayList<String> data;    private int count;    private int index = 0;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        next = (Button) findViewById(R.id.next);        image = (ImageView) findViewById(R.id.imageview);        play = (ImageView) findViewById(R.id.play);                vedio = (FrameLayout) findViewById(R.id.vedio);        data = Utils.getVideoData(getApplicationContext());        count = data.size();        next.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                    Bitmap bitmap = createVideoThumbnail(getApplicationContext(), Uri.parse(data.get(index)));                    if (bitmap != null) {                        vedio.setVisibility(View.VISIBLE);                        image.setImageBitmap(bitmap);                        image.setTag( Uri.parse(data.get(index)));                    index++;                    if (index == count) {                        index = 0;                    }                }            }        });        play.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(Intent.ACTION_VIEW);                intent.setDataAndType(Uri.parse(image.getTag().toString()), "video/3gp");                startActivity(intent);            }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }    public static Bitmap createVideoThumbnail(Context context, Uri uri) {        Bitmap bitmap = null;        String className = "android.media.MediaMetadataRetriever";        Object objectMediaMetadataRetriever = null;        Method release = null;        try {            objectMediaMetadataRetriever = Class.forName(className)                    .newInstance();            Method setDataSourceMethod = Class.forName(className).getMethod(                    "setDataSource", Context.class, Uri.class);            setDataSourceMethod.invoke(objectMediaMetadataRetriever, context,                    uri);            Method getFrameAtTimeMethod = Class.forName(className).getMethod("getFrameAtTime");            bitmap = (Bitmap) getFrameAtTimeMethod.invoke(objectMediaMetadataRetriever);        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                if (release != null) {                    release.invoke(objectMediaMetadataRetriever);                }            } catch (Exception e) {                e.printStackTrace();            }        }        return bitmap;    }}
布局文件

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <Button         android:id="@+id/next"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/next"        />    <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="@string/prompt"        android:textSize="10pt"        />    <FrameLayout        android:id="@+id/vedio"        android:layout_width="fill_parent"        android:layout_height="300dip"        android:visibility="gone"        >    <ImageView         android:id="@+id/imageview"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_marginLeft="20dip"        android:layout_marginRight="20dip"        android:contentDescription="@string/imageview"        />    <ImageView         android:id="@+id/play"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/play"        android:layout_gravity="center"        android:contentDescription="@string/imageview"        android:focusable="true"        />    </FrameLayout></LinearLayout>


源码下载:

http://download.csdn.net/detail/weihan1314/4875129







热点排行