1 package com.visagetechnologies.showcasedemo;
3 import android.content.ContentResolver;
4 import android.content.Context;
5 import android.graphics.Bitmap;
6 import android.graphics.BitmapFactory;
7 import android.graphics.ImageDecoder;
8 import android.media.Image;
9 import android.media.MediaCodec;
10 import android.media.MediaExtractor;
11 import android.media.MediaFormat;
12 import android.net.Uri;
13 import android.os.Build;
14 import android.provider.MediaStore;
15 import android.util.Log;
18 import java.io.FileInputStream;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.nio.ByteBuffer;
24 import static android.graphics.Bitmap.Config.ARGB_8888;
28 static class ImageLoader {
29 private static Bitmap bitmap = null;
31 private static void ResetVariables() {
46 public static byte[] ConvertToByte(Bitmap bitmap_,
boolean opacity) {
47 int w = bitmap_.getWidth();
48 int h = bitmap_.getHeight();
50 int[] rgbStream =
new int[w * h];
51 bitmap_.getPixels(rgbStream, 0, w, 0, 0, w, h);
53 byte[] buffer =
new byte[w * h * 4];
55 for (
int i = 0; i < w * h; i++) {
56 int pixel = rgbStream[i];
58 int R = (pixel >> 16) & 0xFF;
59 int G = (pixel >> 8) & 0xFF;
62 buffer[offset + 0] = (byte) R;
63 buffer[offset + 1] = (byte) G;
64 buffer[offset + 2] = (byte) B;
67 int A = (pixel >> 24) & 0xFF;
68 buffer[offset + 3] = (byte) A;
84 public static void LoadPictureSendToVW(ContentResolver resolver, Uri imageUri)
throws IOException {
85 if (Build.VERSION.SDK_INT >= 28)
87 Bitmap tempBitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(resolver, imageUri));
88 bitmap = tempBitmap.copy(Bitmap.Config.ARGB_8888,
true);
92 BitmapFactory.Options bitmapOptions =
new BitmapFactory.Options();
93 bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;
94 InputStream input = resolver.openInputStream(imageUri);
95 bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
101 VisageWrapper.SetParameters(bitmap.getWidth(), bitmap.getHeight());
102 VisageWrapper.WriteFrameImage(ConvertToByte(bitmap,
false), bitmap.getWidth(), bitmap.getHeight());
110 static class VideoLoaderThread
implements Runnable {
111 static final String TAG =
"VideoLoaderThread";
112 volatile boolean exitThread =
false;
113 Context appContext = null;
114 Thread videoThread = null;
116 MediaExtractor extractor = null;
117 MediaCodec decoder = null;
118 MediaCodec.BufferInfo info;
122 Integer rotationAngle = 0;
123 Integer frameRate = 0;
128 VideoLoaderThread(Uri URI, Context context) {
131 appContext = context;
135 videoThread.interrupt();
139 videoThread =
new Thread(
this);
140 videoThread.setName(
"videoThread");
144 void stop() throws InterruptedException {
145 videoThread.interrupt();
152 MediaCodec.BufferInfo info =
new MediaCodec.BufferInfo();
153 boolean isEOS =
false;
156 long currentTime = 0;
157 long startTime =
System.currentTimeMillis();
161 int inIndex = decoder.dequeueInputBuffer(1000);
163 ByteBuffer buffer = decoder.getInputBuffer(inIndex);
164 int sampleSize = extractor.readSampleData(buffer, 0);
165 long sampleTime = extractor.getSampleTime();
166 if (sampleSize < 0) {
167 decoder.queueInputBuffer(inIndex, 0, 0, sampleTime, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
170 decoder.queueInputBuffer(inIndex, 0, sampleSize, sampleTime, 0);
176 Integer outIndex = decoder.dequeueOutputBuffer(info, 10000);
179 Image.Plane[] imagePlanes = decoder.getOutputImage(outIndex).getPlanes();
180 currentTime =
System.currentTimeMillis() - startTime;
191 while(currentTime < frameTime * (frameCount -5)){
193 currentTime =
System.currentTimeMillis() - startTime;
198 ts =(long)((1000.0/frameRate) * frameCount);
201 VisageWrapper.WriteFrameStream(imagePlanes[0].getBuffer(), imagePlanes[1].getBuffer(), imagePlanes[2].getBuffer(), ts, 0, imagePlanes[1].getPixelStride());
203 decoder.releaseOutputBuffer(outIndex, 1000);
209 }
catch (InterruptedException e) {
210 System.out.println(
"InterruptedException occur");
211 Log.w(
"VideoThread",
"exception occur");
214 long elapsedTime =
System.currentTimeMillis() - startTime;
215 Log.d(
"DURATION",
"elapsed time video decoding " + elapsedTime);
225 private void initialize() {
226 info =
new MediaCodec.BufferInfo();
227 extractor =
new MediaExtractor();
230 extractor.setDataSource(appContext, videoURI, null);
232 int index = extractor.getTrackCount();
234 for (
int i = 0; i < index; i++) {
235 MediaFormat format = extractor.getTrackFormat(i);
237 String mime = format.getString(MediaFormat.KEY_MIME);
238 if (mime.startsWith(
"video/")) {
239 extractor.selectTrack(i);
240 decoder = MediaCodec.createDecoderByType(mime);
241 decoder.configure(format, null, null, 0);
243 width = format.getInteger(MediaFormat.KEY_WIDTH);
244 height = format.getInteger(MediaFormat.KEY_HEIGHT);
246 if(format.containsKey(MediaFormat.KEY_ROTATION)){
247 rotationAngle = format.getInteger(MediaFormat.KEY_ROTATION);
251 if (format.containsKey(MediaFormat.KEY_FRAME_RATE)) {
252 frameRate = format.getInteger(MediaFormat.KEY_FRAME_RATE);
253 Log.d(
"DURATION",
"framerate " + frameRate);
254 frameTime = 1000 / (long)frameRate;
256 if(format.containsKey(MediaFormat.KEY_DURATION)){
257 long duration = format.getLong(MediaFormat.KEY_DURATION);
258 Log.d(
"DURATION",
"duration " + duration);
261 VisageWrapper.SetParameters(width, height, rotationAngle, 0);
268 }
catch (Exception exception) {
269 exception.printStackTrace();