Android Make Animated Video from List of Images

How to create a video from an array of images in Android?

You can use jcodec SequenceEncoder to convert sequence of images to MP4 file.

Sample code :

import org.jcodec.api.awt.SequenceEncoder;
...
SequenceEncoder enc = new SequenceEncoder(new File("filename"));
// GOP size will be supported in 0.2
// enc.getEncoder().setKeyInterval(25);
for(...) {
BufferedImage image = ... // Obtain an image to encode
enc.encodeImage(image);
}
enc.finish();

It's a java library so it's easy to import it into Android project, you don't have to use NDK unlike ffmpeg.

Refer http://jcodec.org/ for sample code & downloads.

Android: make animation from still images

OK. The biggest problem and the easiest solution I got to go with after so many days. I would never expect that it would be so easy to do... :D

I've used both handler and timer to achieve with just an image view, no flipper, no animator nothing else at all... Here is my solution:

----- main.xml file -----

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1">
</ImageView>

And here is the way I have done it:

public class MainActivity extends Activity {
private ImageView _imagView;
private Timer _timer;
private int _index;
private MyHandler handler;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler= new MyHandler();
_imagView=(ImageView) findViewById(R.id.imageView1);

_index=0;
_timer= new Timer();
_timer.schedule(new TickClass(), 500, 200);
}

private class TickClass extends TimerTask
{
@Override
public void run() {
// TODO Auto-generated method stub
handler.sendEmptyMessage(_index);
_index++;
}
}

private class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);

try {
Bitmap bmp= BitmapFactory.decodeStream(MainActivity.this.getAssets().open("drum_"+_index+".png"));
_imagView.setImageBitmap(bmp);

Log.v("Loaing Image: ",_index+"");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("Exception in Handler ",e.getMessage());
}
}
}

}

Note: I've placed all my images to asset directory.

Its so simple as it can, nothing big to do...

I hope it'll be helpful for someone looking to go like this :)

Create video from series of images using android

FFMpeg or X264 is your best bet to create videos from a series of images. Both of them have detailed instructions on how to integrate this library into an android application. You can just add the *.so files into your jni/libs folder and invoke them from your Java code. I recommend you read thru the instructions provided on the FFMPeg site. You can also find the precompiled FFMPeg library here

How can we add animated/simple image as a top layer to video and export it as a single video in Android?

The way to accomplish this is like you said - incorporate a video codec and use it to re-compose the video.

  1. Decode the original video
  2. Draw your overlay over the original video frames
  3. Encode the frames again.

Using FFMPEG with JNI is the obvious solution, but if you find any other codec library that can accomplish the same with pure Java, it will work too.

No knowledge of JNI? That's about time to learn it =)

References for learning:

  1. NDK docs on your file system: http://developer.android.com/tools/sdk/ndk/index.html#Docs

  2. JNI - http://192.9.162.55/docs/books/jni/html/jniTOC.html

  3. FFMPEG for Android - there are many tutorials out there and many source trees that contain a ready build environment.
    You can either follow them or just do it on your own - provided that you understand the NDK environment and can read makefiles.
    This is an example: http://vec.io/posts/how-to-build-ffmpeg-with-android-ndk

  4. Using FFMpeg to encode\decode frames - sadly, no good up-to-date tutorials here, there is the API documentation: http://ffmpeg.org/doxygen/trunk/index.html

  5. Blending bitmaps - use Android's Canvas infrastructure, or just manually copy pixels over each other and blend according to alpha values.

Warning - this is a complex library to build and you'd better experiment with easier NDK projects before attempting this one.



Related Topics



Leave a reply



Submit