Last active
March 11, 2017 11:39
-
-
Save jacobtabak/3d0562501df7c61f7b9a to your computer and use it in GitHub Desktop.
Picasso Video Frame RequestHandler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap; | |
import android.media.MediaMetadataRetriever; | |
import com.squareup.picasso.Picasso; | |
import com.squareup.picasso.Request; | |
import com.squareup.picasso.RequestHandler; | |
import java.io.IOException; | |
/** | |
* Loads frames from a video at the specified microsecond. Usage: | |
* Picasso picasso = new Picasso.Builder(this).addRequestHandler(new VideoFrameRequestHandler()).build(); | |
* picasso.load("videoframe://path/to/video#microseconds | |
* The offset is in microseconds, so #1000000 = 1 second in. | |
*/ | |
public class PicassoVideoFrameRequestHandler extends RequestHandler { | |
public static final String SCHEME = "videoframe"; | |
@Override public boolean canHandleRequest(Request data) { | |
return SCHEME.equals(data.uri.getScheme()); | |
} | |
@Override public Result load(Request data) throws IOException { | |
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); | |
mediaMetadataRetriever.setDataSource(data.uri.getPath()); | |
String offsetString = data.uri.getFragment(); | |
long offset = Long.parseLong(offsetString); | |
Bitmap bitmap = mediaMetadataRetriever.getFrameAtTime(offset); | |
return new Result(bitmap, Picasso.LoadedFrom.DISK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment