Android intercepts video frames and converts them to bitmap

Source: Internet
Author: User
Tags xmlns


Mainactivity is as follows

Package cn.testmediametadataretriever;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Android.media.MediaMetadataRetriever;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.Bitmap.CompressFormat;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button; /** * Demo Description: * Use Mediametadataretriever to intercept video at time * and convert to bitmap store in SDcard * Special NOTE: * Getframeattime () method The unit of the first parameter is microsecond (u
	s) * * */public class Mainactivity extends activity {private Button Mbutton;
		@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
		Setcontentview (R.layout.main);
	Init ();
		private void Init () {mbutton= (Button) Findviewbyid (R.id.button);
	Mbutton.setonclicklistener (New Clicklistenerimpl ()); Private class Clicklistenerimpl implements onclicklistener{@Override public void OnClick (View v) {switch (v. GetId ()) {case R.id.button:getbitmapsfromvideo ();
			Default:break; }} public void Getbitmapsfromvideo () {String datapath = environment.getexternalstoragedirectory () +/te
		Stvideo.mp4 ";
		Mediametadataretriever retriever = new Mediametadataretriever ();
		Retriever.setdatasource (DataPath);
		Gets the length of the video (in milliseconds) String time = Retriever.extractmetadata (mediametadataretriever.metadata_key_duration);
		Gets the length of the video (in seconds) int seconds = integer.valueof (time)/1000; Get Bitmap of every second moment like first second, for (int i = 1; I <= seconds i++) {Bitmap Bitmap = Retriever.getframeattime (i*1000*10
			00,mediametadataretriever.option_closest_sync);
			String path = environment.getexternalstoragedirectory () + file.separator + i + ". jpg";
			FileOutputStream fos = null;
				try {fos = new FileOutputStream (path);
				Bitmap.compress (compressformat.jpeg, FOS);
			Fos.close ();
			catch (Exception e) {e.printstacktrace (); }
		}

	}
}

Main.xml is as follows:

<relativelayout
    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 "
    >

    <button
        android:id= "@+id/button"
        android:layout_width= "Wrap_content"
        android: layout_height= "Wrap_content"
        android:text= "get frame picture for video" 
        android:layout_centerinparent= "true"
    />

</RelativeLayout>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.