private int[] GifToIntAA(byte[] bytes, int length) { AndroidJavaClass bmf = new AndroidJavaClass("android.graphics.BitmapFactory"); AndroidJavaClass bm = new AndroidJavaClass("android.graphics.Bitmap"); // this bitmapfactory class method returns a Bitmap object AndroidJavaObject bmo = bmf.CallStatic <AndroidJavaObject>("decodeByteArray", new object[] { bytes, 0, length }); // we can figure out the width and height of the gif data int h = bmo.Call <int>("getHeight", new object[] { }); int w = bmo.Call <int>("getWidth", new object[] { }); wh = new Vector2Int(w, h); // set the global wh for offsetment // the trick is getting the pixels without calling the JNI to often i.e. _getPixel()_ // setup java inputs for BitMap.getPixels System.IntPtr pixs = AndroidJNI.NewIntArray(h * w); jvalue[] gpargs; gpargs = new jvalue[7]; gpargs[0].l = pixs; gpargs[1].i = 0; gpargs[2].i = w; gpargs[3].i = 0; gpargs[4].i = 0; gpargs[5].i = w; gpargs[6].i = h; // this is the same as `bmo.getPixels(pixs,0,w,0,0,w,h)` but in raw AndroidJNI calls because pixs is a pointer to an int[] buffer AndroidJNI.CallVoidMethod(bmo.GetRawObject(), AndroidJNI.GetMethodID(bm.GetRawClass(), "getPixels", "([IIIIIII)V"), gpargs); return(AndroidJNI.FromIntArray(pixs)); }
// Use this for initialization void Start() { if (Application.platform != RuntimePlatform.Android) { return; } // player1 = new player; liveRawImage = GetComponent <RawImage>(); Debug.Log("Start"); //videoPlaying = new AndroidJavaObject("com.fuho.fromfleetcloud.video.FleetLive", "WW00001ASED5", 1, "fleet.vacron.com", "admin", "admin", "L"); videoPlaying = new AndroidJavaObject("com.fuho.fromfleetcloud.video.FleetLive", "WW00001ASED5", 1, "fleet.vacron.com", "admin", "admin", "L"); videoPlaying = new AndroidJavaObject("com.fuho.fromfleetcloud.video.FleetLive", "VG500122", 1, "54.199.103.26", "admin", "admin", "L"); //videoPlaying = new AndroidJavaObject("com.fuho.fromfleetcloud.video.FleetLive", "TT001802", 1, "125.227.91.156", "guest", "123456", "L"); IntPtr methodID = AndroidJNI.GetMethodID(videoPlaying.GetRawClass(), "start", "()[I"); AndroidJavaObject nullObject = null; UnityEngine.jvalue[] args = AndroidJNIHelper.CreateJNIArgArray(new[] { nullObject }); IntPtr intArray = AndroidJNI.CallObjectMethod(videoPlaying.GetRawObject(), methodID, args); if (intArray != IntPtr.Zero) { wh = AndroidJNI.FromIntArray(intArray); } Debug.LogFormat("============================= width : {0}, height : {0} =============================", wh[0], wh[1]); }
// Update is called once per frame void Update() { if (Application.platform != RuntimePlatform.Android) { return; } Debug.Log("Update"); try { IntPtr methodID2 = AndroidJNI.GetMethodID(videoPlaying.GetRawClass(), "getVideoImage", "()[I"); AndroidJavaObject nullObject2 = null; UnityEngine.jvalue[] args2 = AndroidJNIHelper.CreateJNIArgArray(new[] { nullObject2 }); IntPtr intArray2 = AndroidJNI.CallObjectMethod(videoPlaying.GetRawObject(), methodID2, args2); if (intArray2 == IntPtr.Zero) { return; } int[] videoData = AndroidJNI.FromIntArray(intArray2); int length = videoData.Length; if (length <= 0 || wh == null || wh[0] <= 0 || wh[1] <= 0) { return; } byte[] bytes = new byte[length * sizeof(int)]; Buffer.BlockCopy(videoData, 0, bytes, 0, bytes.Length); Texture2D bmp = new Texture2D(wh[0], wh[1], TextureFormat.ARGB32, false); bmp.LoadRawTextureData(bytes); bmp.Apply(); // Sprite sprite = Sprite.Create(bmp, new Rect(0, 0, bmp.width, bmp.height), new Vector2 (0.5f, 0.5f), 100F, 0, SpriteMeshType.FullRect); // Texture2D nativeTexture = Texture2D.CreateExternalTexture(wh[0], wh[1], TextureFormat.ARGB32, false, false, (IntPtr)intArray2); // bmp.UpdateExternalTexture(nativeTexture.GetNativeTexturePtr()); // bmp.Apply(); liveRawImage.texture = bmp; Resources.UnloadUnusedAssets(); Debug.LogFormat("============================= Update : videoData int len : {0} =============================", length); } catch (Exception e) { Debug.LogFormat("============================= Update : Exception {0} ====================================", e.Message); } }