Community

Topic: Android SDK

displaying animated gif on the tracked image (SOLVED)

Francis Anyeji
Friday, June 23, 2017

Hi,
Im using your cloud recognition service on trial plan, I have an animated gif on your cloud custom data as base64 string.
I want to know, when the image on the cloud is recognised, is it possible to retrieve it's custom data, decode to gif, and display this gif on the tracked image. I'm more concerned on how I'll display the animation as gif on the tracked image.
Thanks.

user profile image
Support Team  (expert)
Monday, June 26, 2017

Hi,
retrieving the gif data is quite easy, the callback function public void markerFound(Marker marker) gives you the Marker data on marker recognition. The Marker object has a function (public String getCustomData()) that allows you to retrieve the custom data.
To display the gif image on the marker you have to write your own rendering functions, unfortunately the Android MediaPlayer does not support playback of .gif file.
A simple solution is to use the Movie class to render the Gif on a View that is attached to an OpenGL texture and than draw that texture on a plane (similarly to our PikkartVideoPlayer class).

This is an example of a GifView:

import java.io.InputStream;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

public class GifView extends View {

private InputStream gifInputStream;
private Movie gifMovie;
private int movieWidth, movieHeight;
private long movieDuration;
private long mMovieStart;

public GifView(Context context) {
super(context);
init(context);
}

public GifView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

public GifView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}

private void init(Context context){
setFocusable(true);
gifInputStream = context.getResources()
.openRawResource(R.drawable.android_er);

gifMovie = Movie.decodeStream(gifInputStream);
movieWidth = gifMovie.width();
movieHeight = gifMovie.height();
movieDuration = gifMovie.duration();
}

@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
setMeasuredDimension(movieWidth, movieHeight);
}

public int getMovieWidth(){
return movieWidth;
}

public int getMovieHeight(){
return movieHeight;
}

public long getMovieDuration(){
return movieDuration;
}

@Override
protected void onDraw(Canvas canvas) {

long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}

if (gifMovie != null) {

int dur = gifMovie.duration();
if (dur == 0) {
dur = 1000;
}

int relTime = (int)((now - mMovieStart) % dur);

gifMovie.setTime(relTime);

gifMovie.draw(canvas, 0, 0);
invalidate();

}

}

and here is a link to StackOverflow with a few tidbits on how to render android Views as opengl texture:
https://stackoverflow.com/questions/12499396/is-it-possible-to-render-an-android-view-to-an-opengl-fbo-or-texture

Best,

Francis Anyeji
Monday, June 26, 2017

Thank you for this information, I'll try the movie class, to see if it works for me.
I appreciate,
best regards.

Sign in to add a comment