IEnumerator ShareDelay()
    {
        yield return(new WaitForEndOfFrame());

        Texture2D captureScreenshotAsTexture = ScreenCapture.CaptureScreenshotAsTexture();

        if (GetSocial.IsInitialized)
        {
            var inviteContent = InviteContent.CreateBuilder()
                                .WithText("Try to beat me in this game [APP_INVITE_URL]") // NOTE: if you customize the text [APP_INVITE_URL] placeholder have to be used
                                .WithMediaAttachment(MediaAttachment.Image(captureScreenshotAsTexture))
                                .Build();
            bool wasShown = GetSocialUi.CreateInvitesView()
                            .SetCustomInviteContent(inviteContent)
                            .SetInviteCallbacks(
                onComplete: (channelId) => Debug.Log("Invitation was sent via " + channelId),
                onCancel: (channelId) => Debug.Log("Invitation via " + channelId + " was cancelled"),
                onFailure: (channelId, error) => Debug.LogError("Invitation via" + channelId + "failed, error: " + error.Message)
                )
                            .Show();
        }
    }
Esempio n. 2
0
    /// <summary>
    /// Takes screenshot of the screen area specified in the Inspector window.
    /// Clears the color you want to delete. That's why please pick a color which is not in your prefab.
    /// For 1600x900 screen resolution: x: 546, y: 196, w: 512, h: 512
    /// </summary>
    /// <param name="sceneObject"> the prefab that is currently on the scene</param>
    /// <returns></returns>
    IEnumerator TakeTransparentScreenshot(GameObject sceneObject)
    {
        yield return(new WaitForEndOfFrame());

        if (objectBackground.GetComponent <Image>().sprite != null)
        {
            Debug.Log("Please pick a solid color.");
            StartCoroutine(ShowMessage("Please pick a solid color."));
        }
        else
        {
            Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();

            Color[]   pixels = texture.GetPixels(startX, startY, width, height);
            Texture2D tex    = new Texture2D(width, height);

            for (int i = 0; i < pixels.Length; i++)
            {
                if (pixels[i] == objectBackground.GetComponent <Image>().color)
                {
                    pixels[i] = new Color(0, 0, 0, 0);
                }
            }

            tex.SetPixels(pixels);
            tex.Apply();

            byte[] bytes = tex.EncodeToPNG();
            Destroy(tex);

            File.WriteAllBytes(Application.dataPath + "/IconStudio/CreatedTransparentIcons/" + sceneObject.name + ".png", bytes);

            Destroy(objectOnScene);
            prefabIndex++;

            anyObjectOnScene = false;
            isPrefabCalled   = true;
        }
    }
Esempio n. 3
0
    void TirarFoto()
    {
        //Coloca o screensht na textura
        textura = ScreenCapture.CaptureScreenshotAsTexture(1);
        //Instancia um novo objecto do tipo raw image
        RawImage NovaImagem = Instantiate(Imagem, new Vector2(0, 0), Quaternion.identity) as RawImage;

        //Aplica textura no novo obj
        NovaImagem.texture = textura;
        //Parentea a UI
        NovaImagem.transform.SetParent(UltimaFototirada.transform);
        //Manda a foto para o local de amostra
        NovaImagem.transform.position = UltimaFototirada.transform.position;
        //Escala da imagem
        NovaImagem.transform.localScale = new Vector3(1, 1, 1);
        //Adiciona foto dentro da lista de fotos
        FotosGuardadas.Add(NovaImagem);
        //Depois da foto reestabeleça todos os icones ao normal
        MemoriaAtual += 1;
        LigarIcones();
        Obturador();
    }
Esempio n. 4
0
    public IEnumerator TakeScreenShot()
    {
        yield return(SkipFrame(2));

        try
        {
#if PLATFORM_IOS || PLATFORM_ANDROID
            var cam    = Camera;
            var width  = cam.GetComponent <Camera>().scaledPixelWidth;
            var height = cam.GetComponent <Camera>().scaledPixelHeight;

            mobileTexture = new Texture2D(width, height, TextureFormat.RGBA32, false);
            mobileTexture = ScreenCapture.CaptureScreenshotAsTexture(ScreenCapture.StereoScreenCaptureMode.BothEyes);
#else
            fileName = Application.temporaryCachePath + "/ScreenShotTest.jpg";
            ScreenCapture.CaptureScreenshot(fileName, ScreenCapture.StereoScreenCaptureMode.BothEyes);
#endif
        }
        catch (Exception e)
        {
            Debug.Log("Failed to get capture! : " + e.Message);
            Assert.Fail("Failed to get capture! : " + e.Message);
        }

        yield return(SkipFrame(5));

#if PLATFORM_IOS || PLATFORM_ANDROID
        Assert.IsNotNull(mobileTexture, "Texture data is empty for mobile");
#else
        var tex = new Texture2D(2, 2);

        var texData = File.ReadAllBytes(fileName);
        Debug.Log("Screen Shot Success!" + Environment.NewLine + "File Name = " + fileName);

        tex.LoadImage(texData);

        Assert.IsNotNull(tex, "Texture Data is empty");
#endif
    }
Esempio n. 5
0
    void CaptureWorld()
    {
        // capture screen to texture
        //yield return new WaitForEndOfFrame();
        planeTex = ScreenCapture.CaptureScreenshotAsTexture();
        //yield return new WaitForEndOfFrame();

        // get camera pose
        Pose cameraPose = new Pose(Camera.main.transform.position, Camera.main.transform.rotation);

        // create anchor a few points in front of camera
        Pose planePosition = new Pose(cameraPose.position + cameraPose.forward * 1.5f, cameraPose.rotation);

        anchor = Session.CreateAnchor(planePosition);

        // instantiate game object, ancrhor the game object and put texture to quad
        GameObject newPlane = Instantiate(plane, cameraPose.position + cameraPose.forward * 1.5f, cameraPose.rotation);

        newPlane.transform.parent = anchor.transform;
        //newPlane.transform.localScale = new Vector3(Screen.width, Screen.height, 1.0f);
        newPlane.GetComponent <MeshRenderer>().material.mainTexture = planeTex;
        planeTex.Apply();
    }
    void Update()
    {
        // get texture from camera assigned to player 1
        Texture2D screenTexture = ScreenCapture.CaptureScreenshotAsTexture();
        //Texture2D screenTexture = GetRTPixels (cameraP1.targetTexture);

        // get pixel color to drive leds pannel
        int offsetX_ = (int)(screenTexture.width / 2f - screenTexture.height / 2f);

        for (int i = 0; i < COLS; i++)
        {
            for (int j = 0; j < ROWS; j++)
            {
//				int gx_ = (int)(screenTexture.width * (0.8f * i / ((float)COLS) + 0.1f));
//				int gy_ = (int)(screenTexture.height * (0.8f * j / ((float)ROWS) + 0.1f));
                int gx_ = (int)(screenTexture.height * (i / ((float)COLS))) + offsetX_;
                int gy_ = (int)(screenTexture.height * ((j / ((float)ROWS)) * 0.95f + 0.025f));
                newLedColor[getLedIndex(i, j)] = screenTexture.GetPixel(gx_, gy_);
            }
        }

        DestroyObject(screenTexture);
    }
Esempio n. 7
0
    IEnumerator RecordFrame()
    {
        yield return(new WaitForEndOfFrame());

        var texture = ScreenCapture.CaptureScreenshotAsTexture();
        // do something with texture
        var matRes = TextureToMat(texture);

        // Assign the Vec3b array to Mat
        _image.SetArray(0, 0, matRes);

        var blob = CvDnn.BlobFromImage(_image, 1 / 255.0, new Size(1147, 575), new Scalar(), true, false);

        net.SetInput(blob, "data");
        var prob = net.Forward();

        predict(prob);

        //MatToTexture();

        // cleanup
        Object.Destroy(texture);
    }
Esempio n. 8
0
    public IEnumerator Screen()
    {
        yield return(new WaitForEndOfFrame());

        mainGameObject.SetActive(false);
        var tmp = GameObject.Instantiate(area.gameObject);

        tmp.transform.SetParent(imgTest.transform);
        tmp.transform.localScale    = new Vector3(1, 1, 1);
        tmp.transform.localPosition = new Vector3(0, 0, 0);
        imgTest.gameObject.SetActive(true);

        mainTex      = ScreenCapture.CaptureScreenshotAsTexture(2);
        mainTex.name = "ScreenShot";

        Sprite sp = Sprite.Create(mainTex, new Rect(0, 0, mainTex.width, mainTex.height), new Vector2(0, 0));

        imgTest.sprite = sp;

        var childs = imgTest.gameObject.GetComponentsInChildren <Transform>();

        if (childs.Length != 0)
        {
            foreach (Transform item in childs)
            {
                //print(item.gameObject.name);
                if (item.gameObject.name != "TestImage")
                {
                    Destroy(item.gameObject);
                }
            }
        }

        mainImage.sprite = sp;
        imgTest.gameObject.SetActive(false);
        mainGameObject.SetActive(true);
    }
Esempio n. 9
0
    /// <summary>
    /// Send message to server using socket connection.
    /// </summary>
    public void SendTCPMessage()
    {
        // ConnectToTcpServer();
        //System.Threading.Thread.Sleep(5000);

        if (socketConnection == null)
        {
            return;
        }
        try
        {
            // Get a stream object for writing.
            NetworkStream stream = socketConnection.GetStream();
            if (stream.CanWrite)
            {
                //string clientMessage = "This is a message from one of your clients.";
                // Convert string message to byte array.
                var    texture = ScreenCapture.CaptureScreenshotAsTexture();
                byte[] clientMessageAsByteArray = texture.EncodeToPNG();
                //byte[] clientMessageAsByteArray = Encoding.ASCII.GetBytes(clientMessage);
                // Write byte array to socketConnection stream.
                stream.Write(clientMessageAsByteArray, 0, clientMessageAsByteArray.Length);
                byte[] emptyByte = new byte[0];
                stream.Write(emptyByte, 0, emptyByte.Length);
                Debug.Log("Client sent his message - should be received by server");
            }
        }
        catch (SocketException socketException)
        {
            Debug.Log("Socket exception: " + socketException);
        }
        finally
        {
            socketConnection.GetStream().Close();
            //socketConnection.Close();
        }
    }
Esempio n. 10
0
        private IEnumerator WaitForNoVisualChangeCoroutine(Config config)
        {
#if ENABLE_IMAGE_MAGICK
            var consecutiveNoDiffNeeded = config.minReqMsWithNoVisualChange / config.msBetweenVisualChangeDetectCaptures;
            var noDiffCounter           = 0;
            var timer = Stopwatch.StartNew();
            do
            {
                yield return(new WaitForEndOfFrame());

                Texture2D screenShot = ScreenCapture.CaptureScreenshotAsTexture(config.screenshotUpscaleFactor);
                yield return(new WaitForSeconds(config.msBetweenVisualChangeDetectCaptures / 1000f));

                yield return(new WaitForEndOfFrame());

                Texture2D screenShot2        = ScreenCapture.CaptureScreenshotAsTexture(config.screenshotUpscaleFactor);
                var       visualDiffDetected = AreVisuallyDifferent(screenShot, screenShot2, config);
                if (!visualDiffDetected)
                {
                    noDiffCounter++;
                }
                else
                {
                    noDiffCounter = 0;
                }
                screenShot.Destroy();
                screenShot2.Destroy();
                if (timer.ElapsedMilliseconds > config.maxMsToWaitForNoVisualChange)
                {
                    throw new TimeoutException($"WaitForNoVisualChange not done after {config.maxMsToWaitForNoVisualChange}ms");
                }
            } while (noDiffCounter < consecutiveNoDiffNeeded);
#else
            Log.w("WaitForNoVisualChangeInScene() skipped: ENABLE_IMAGE_MAGICK define not active, see instructions 'readme Image Magick Unity Installation Instructions.txt'");
            yield return(null);
#endif
        }
Esempio n. 11
0
    //Capture Image (Screenshot) ***Need around 20 images
    IEnumerator captureImage()
    {
        if (capture)             //Image capturing available
        {
            recordCaptureInfo(); //Record device orientation at time of image capture

            //Capture image
            canvas.SetActive(false);               //Hiding UI
            yield return(new WaitForEndOfFrame()); //Waiting for everything to render

            //Save image to Texture2D
            Texture2D img = ScreenCapture.CaptureScreenshotAsTexture(); //Save screenshot to a Texture2D
            img.Apply();                                                //Apply changes to make texture visible
            capturedImages.Add(img);                                    //Save to List<Texture2D>

            //Save image to Application.persistentDataPath
            string imgName  = "IMG" + capturedImagePaths.Count + ".jpg";
            string filePath = Application.persistentDataPath + "/" + imgName;
            ScreenCapture.CaptureScreenshot(imgName);
            capturedImagePaths.Add(filePath);

            //imagePreview.texture = img; //Set imagePreview to current image
            canvas.SetActive(true); //Restoring UI
        }
        else
        {
            //Display camera feed
            cam.SetActive(true);                                         //Activate camera
            TrackerManager.Instance.GetTracker <ObjectTracker>().Stop(); //Disabling Vuforia object tracker (I think it is reenabled when the camera is set active)
            imgViewer.gameObject.SetActive(false);                       //Disable image gallery
            cameraBtn.gameObject.SetActive(false);
            //cameraBtn.GetComponentInChildren<Text>().text = "Capture Image"; //Change button text
            galleryBtn.GetComponentInChildren <Text>().text = "View Images"; //Change button text
            capture = true;                                                  //Allow image capturing
        }
    }
Esempio n. 12
0
        private IEnumerator TweetWithScreenshotCo()
        {
            yield return(new WaitForEndOfFrame());

            Texture2D tex = ScreenCapture.CaptureScreenshotAsTexture();

            var wwwForm = new WWWForm();

            wwwForm.AddField("image", Convert.ToBase64String(tex.EncodeToJPG()));
            wwwForm.AddField("type", "base64");

            // Upload to Imgur
            UnityWebRequest www = UnityWebRequest.Post("https://api.imgur.com/3/image.xml", wwwForm);

            www.SetRequestHeader("AUTHORIZATION", "Client-ID " + _imgurClientId);

            yield return(www.SendWebRequest());

            var uri = "";

            if (!www.isNetworkError)
            {
                XDocument xDoc = XDocument.Parse(www.downloadHandler.text);
                uri = xDoc.Element("data")?.Element("link")?.Value;

                // Remove Ext
                uri = uri?.Remove(uri.Length - 4, 4);
            }

#if !UNITY_EDITOR && UNITY_WEBGL
            TweetFromUnity($"{_tweetMessage}%0a{uri}");
            yield break;
#endif

            Application.OpenURL($"https://twitter.com/intent/tweet?text={_tweetMessage}%0a{uri}");
        }
Esempio n. 13
0
    public int[] getWH(string path = "")
    {
        Texture2D img = new Texture2D(2, 2);

        if (path != "")
        {
            byte[] fileData = File.ReadAllBytes(path);
            img.LoadImage(fileData);
        }
        else
        {
            img = ScreenCapture.CaptureScreenshotAsTexture();
        }
        int[] wh        = new int[4];
        int   minxIndex = img.width;
        int   maxxIndex = 0;
        int   minyIndex = img.height;
        int   maxyIndex = 0;

        for (int i = 0; i < img.width; i++)
        {
            for (int j = 0; j < img.height; j++)
            {
                Color pixel = img.GetPixel(i, j);
                if ((pixel.r != 1) | (pixel.g != 1) | (pixel.b != 1))
                {
                    if (j < minyIndex)
                    {
                        minyIndex = j;
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < img.width; i++)
        {
            for (int j = img.height; j > 0; j--)
            {
                Color pixel = img.GetPixel(i, j);
                if ((pixel.r != 1) | (pixel.g != 1) | (pixel.b != 1))
                {
                    if (j > maxyIndex)
                    {
                        maxyIndex = j;
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < img.height; i++)
        {
            for (int j = 0; j < img.width; j++)
            {
                Color pixel = img.GetPixel(j, i);
                if ((pixel.r != 1) | (pixel.g != 1) | (pixel.b != 1))
                {
                    if (j < minxIndex)
                    {
                        minxIndex = j;
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < img.height; i++)
        {
            for (int j = img.width; j > 0; j--)
            {
                Color pixel = img.GetPixel(j, i);
                if ((pixel.r != 1) | (pixel.g != 1) | (pixel.b != 1))
                {
                    if (j > maxxIndex)
                    {
                        maxxIndex = j;
                        break;
                    }
                }
            }
        }
        wh[0] = minxIndex;
        wh[1] = maxxIndex;
        wh[2] = minyIndex;
        wh[3] = maxyIndex;
        GameObject.Destroy(img);
        return(wh);
    }
Esempio n. 14
0
        protected void RenderWorldMap()
        {
#if UNITY_EDITOR
            if (UnityEditor.EditorApplication.isPaused)
            {
                return;
            }
#endif

            bool shouldCreateOnscreenTexture = false;
            if (_screenRectDirty)
            {
                shouldCreateOnscreenTexture = true;
                CacheScreenRect();
            }

            var gameManager      = OpenTibiaUnity.GameManager;
            var worldMapStorage  = OpenTibiaUnity.WorldMapStorage;
            var worldMapRenderer = OpenTibiaUnity.WorldMapRenderer;
            var protocolGame     = OpenTibiaUnity.ProtocolGame;

            if (gameManager != null && worldMapStorage != null && gameManager.IsGameRunning && worldMapStorage.Valid)
            {
                if (_mouseCursorOverRenderer && gameManager.GameCanvas.gameObject.activeSelf && !gameManager.GamePanelBlocker.gameObject.activeSelf)
                {
                    InternalStartMouseAction(Input.mousePosition, MouseButton.None, false, true, true);
                }
                else
                {
                    worldMapRenderer.HighlightTile   = null;
                    worldMapRenderer.HighlightObject = null;
                }

                if (ContextMenuBase.CurrentContextMenu != null || ObjectDragHandler.AnyDraggingObject)
                {
                    worldMapRenderer.HighlightTile = null;
                }

                var error = worldMapRenderer.RenderWorldMap(rectTransform.rect, gameManager.WorldMapRenderTexture);

                // no point in rendering text if rendering worldmap failed
                if (error == RenderError.None)
                {
                    if (_onscreenTextRenderTexture == null || shouldCreateOnscreenTexture)
                    {
                        _onscreenTextRenderTexture?.Release();
                        _onscreenTextRenderTexture            = new RenderTexture(_lastScreenWidth, _lastScreenHeight, 0, RenderTextureFormat.ARGB32);
                        _onscreenTextRenderTexture.filterMode = FilterMode.Bilinear;
                        _onscreenTextRenderTexture.Create();
                        _onscreenTextImage.texture = _onscreenTextRenderTexture;
                    }

                    worldMapRenderer.RenderOnscreenText(_cachedScreenRect, _onscreenTextRenderTexture);

                    if (!_onscreenTextImage.enabled)
                    {
                        _onscreenTextImage.enabled = true;
                    }
                }

                // setting the clip area
                var clipRect = worldMapRenderer.CalculateClipRect();
                if (clipRect != rawImage.uvRect)
                {
                    rawImage.uvRect = clipRect;
                }

                bool supportsPing = gameManager.GetFeature(GameFeature.GameClientPing);
                if (worldMapRenderer.Framerate != _lastFramerate || (supportsPing && _lastPing != protocolGame.Latency))
                {
                    _lastFramerate = worldMapRenderer.Framerate;
                    var fpsColor = GetFramerateColor(_lastFramerate);

                    string text = string.Format("FPS: <color=#{0:X6}>{1}</color>", fpsColor, _lastFramerate);
                    if (supportsPing)
                    {
                        _lastPing = protocolGame.Latency;
                        var pingColor = GetPingColor(_lastPing);
                        text += string.Format("\nPing: <color=#{0:X6}>{1}</color>", pingColor, _lastPing);
                    }

                    _framecounterText.text = text;
                }

                // tho, this is a very effecient way of taking screenshots
                // it doesn't consider windows ontop of the gamewindow that
                // may be blocking it the view
                if (_shouldTakeScreenshot && error == RenderError.None)
                {
                    _shouldTakeScreenshot = false;

                    var tmpTex2D = ScreenCapture.CaptureScreenshotAsTexture();
                    tmpTex2D.Apply();

                    int startX = (int)_cachedScreenRect.x;
                    int startY = (int)(_lastScreenHeight - _cachedScreenRect.height - _cachedScreenRect.y);
                    int width  = (int)_cachedScreenRect.width;
                    int height = (int)_cachedScreenRect.height;

                    var tex2D = new Texture2D((int)_cachedScreenRect.width, (int)_cachedScreenRect.height, TextureFormat.ARGB32, false);
                    tex2D.SetPixels(0, 0, tex2D.width, tex2D.height, tmpTex2D.GetPixels(startX, startY, width, height));
                    tex2D.Apply();
                    //byte[] texBytes = tex2D.EncodeToPNG();

                    if (!Directory.Exists(Path.Combine(Application.persistentDataPath, "Screenshots")))
                    {
                        Directory.CreateDirectory(Path.Combine(Application.persistentDataPath, "Screenshots"));
                    }

                    var texPath = Path.Combine(Application.persistentDataPath, "Screenshots/Screenshot_" + protocolGame.CharacterName + "_" +
                                               System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png");

                    // File.WriteAllBytes(texPath, texBytes);
                    Debug.Log($"Saved Screenshot to : {texPath}");
                }
                else if (_shouldTakeScreenshot)
                {
                    _shouldTakeScreenshot = false;
                }
            }
            else
            {
                _framecounterText.text = "";
            }
        }
Esempio n. 15
0
 public static Texture2D TakeScreenshotTexture2D()
 {
     return(ScreenCapture.CaptureScreenshotAsTexture());
 }
    IEnumerator GetPicture()
    {
        yield return(new WaitForEndOfFrame()); //攝影機讀取到的Frame繪製完畢後才進行拍照

        //calculate mat of selected region and whole rawimage
        Mat srcMat = new Mat(4, 1, CvType.CV_32FC2);
        Mat dstMat = new Mat(4, 1, CvType.CV_32FC2);

        Vector3 circle1screenPos = mainCam.WorldToScreenPoint(circle1.transform.position);
        Vector3 circle2screenPos = mainCam.WorldToScreenPoint(circle2.transform.position);
        Vector3 circle3screenPos = mainCam.WorldToScreenPoint(circle3.transform.position);
        Vector3 circle4screenPos = mainCam.WorldToScreenPoint(circle4.transform.position);

        rectTopLeft  = mainCam.WorldToScreenPoint(new Vector3(rawImageRT.rect.xMin, rawImageRT.rect.yMax, 0));
        rectTopRight = mainCam.WorldToScreenPoint(new Vector3(rawImageRT.rect.xMax, rawImageRT.rect.yMax, 0));
        rectBotLeft  = mainCam.WorldToScreenPoint(new Vector3(rawImageRT.rect.xMin, rawImageRT.rect.yMin, 0));
        rectBotRight = mainCam.WorldToScreenPoint(new Vector3(rawImageRT.rect.xMax, rawImageRT.rect.yMin, 0));

        //getPerspectiveTransform要以目標rect的左下角為(0,0)
        srcMat.put(0, 0, circle1screenPos.x - rectBotLeft.x, circle1screenPos.y - rectBotLeft.y,
                   circle2screenPos.x - rectBotLeft.x, circle2screenPos.y - rectBotLeft.y,
                   circle3screenPos.x - rectBotLeft.x, circle3screenPos.y - rectBotLeft.y,
                   circle4screenPos.x - rectBotLeft.x, circle4screenPos.y - rectBotLeft.y);

        dstMat.put(0, 0, rectTopLeft.x - rectBotLeft.x, rectTopLeft.y - rectBotLeft.y,
                   rectTopRight.x - rectBotLeft.x, rectTopRight.y - rectBotLeft.y,
                   rectBotLeft.x - rectBotLeft.x, rectBotLeft.y - rectBotLeft.y,
                   rectBotRight.x - rectBotLeft.x, rectBotRight.y - rectBotLeft.y);

        //calculate transform matrix
        transformMat = new Mat(3, 3, CvType.CV_32FC1);
        transformMat = Imgproc.getPerspectiveTransform(srcMat, dstMat);

        width  = (int)(rectTopRight.x - rectBotLeft.x);
        height = (int)(rectTopRight.y - rectBotLeft.y);

        Texture2D sourceTex = ScreenCapture.CaptureScreenshotAsTexture();

        Color[]   pix     = sourceTex.GetPixels((int)rectBotLeft.x, (int)rectBotLeft.y, width, height);
        Texture2D destTex = new Texture2D(width, height);

        destTex.SetPixels(pix);
        destTex.Apply();

        Size textureSize = new Size(width, height);

        Mat rawImageSrcMat     = new Mat(textureSize, CvType.CV_8UC4);
        Mat rawImageSrcMatFlip = new Mat(textureSize, CvType.CV_8UC4);

        Utils.texture2DToMat(destTex, rawImageSrcMat);

        Core.flip(rawImageSrcMat, rawImageSrcMatFlip, 0);

        Mat rawImageDstMat = new Mat(textureSize, CvType.CV_8UC4);

        //Mat rawImageDstMatFlip = new Mat(textureSize, CvType.CV_8UC4);

        Imgproc.warpPerspective(rawImageSrcMatFlip, rawImageDstMat, transformMat, textureSize);

        texture = new Texture2D(width, height, TextureFormat.RGB24, false);
        Utils.matToTexture2D(rawImageDstMat, texture);
        rawImageRI.texture = texture;
    }
Esempio n. 17
0
        private IEnumerator VideoRecoder()
        {
            while (isFinish == false)
            {
                yield return(new WaitForEndOfFrame());

                if (isPlay == false)
                {
                    continue;
                }
                if (isPause == false)
                {
                    frameCount--;
                }
                if (frameCount >= 0 || isAsyncGPUReadBackFinish == false)
                {
                    continue;
                }
                if (isFinish == true)
                {
                    break;
                }
                switch (captureMode)
                {
                    #if UNITY_2019_1_OR_NEWER
                case CaptureMode.AsyncGPUReadBack:
                {
                    if (
                        (textureHeader == null) ||
                        (renderTexture == null) ||
                        (renderTexture.width != Screen.currentResolution.width) ||
                        (renderTexture.height != Screen.currentResolution.height)
                        )
                    {
                        if (renderTexture != null)
                        {
                            renderTexture.Release();
                            Destroy(renderTexture);
                        }
                        renderTexture = new RenderTexture(
                            Screen.currentResolution.width,
                            Screen.currentResolution.height,
                            0
                            );
                        var header = new TextureHeader(renderTexture.width, renderTexture.height, TextureFormat.RGBA32, false, true);
                        var bf     = new BinaryFormatter();
                        var ms     = new MemoryStream();
                        try
                        {
                            bf.Serialize(ms, header);
                            PlayerConnection.instance.Send(PlayerView.kMsgSendPlayerToEditorHeader, ms.ToArray());
                        }
                        finally
                        {
                            ms.Close();
                        }
                        textureHeader = header;
                    }
                    frameCount = frameCountMax;
                    isAsyncGPUReadBackFinish = false;

                    ScreenCapture.CaptureScreenshotIntoRenderTexture(renderTexture);
                    AsyncGPUReadback.Request(renderTexture, 0, TextureFormat.RGBA32, ReadbackCompleted);
                }
                break;

                case CaptureMode.Normal:
                    #else
                default:
                    #endif
                {
                    var texture2D = ScreenCapture.CaptureScreenshotAsTexture();
                    var header    = new TextureHeader(texture2D.width, texture2D.height, TextureFormat.RGBA32, false, false);
                    if (textureHeader == null || textureHeader != header)
                    {
                        var bf = new BinaryFormatter();
                        var ms = new MemoryStream();
                        try
                        {
                            bf.Serialize(ms, header);
                            PlayerConnection.instance.Send(PlayerView.kMsgSendPlayerToEditorHeader, ms.ToArray());
                        }
                        finally
                        {
                            ms.Close();
                        }
                        textureHeader = header;
                    }
                    PlayerConnection.instance.Send(
                        PlayerView.kMsgSendPlayerToEditor,
                        texture2D.GetRawTextureData()
                        );
                    Destroy(texture2D);
                    frameCount = frameCountMax;
                }
                break;
                }
            }
        }
Esempio n. 18
0
    IEnumerator RecordFrame(DetectionStatus detectionStatus)
    {
        isRecordingImage = true;
        string path = Application.dataPath;

        path = path.Substring(0, path.LastIndexOf('/'));
        path = path + "/Screenshots/";
        System.IO.Directory.CreateDirectory(path);
        path = path + "Screenshot_" + System.DateTime.Now.ToString("dd-MM-yy") + "_" + System.DateTime.Now.ToString("HH-mm-ss") + ".png";

        if (hasToFeedbackFlash)
        {
            CameraController.Instance.FlashlightController.ActiveForFlash(true);
        }

        changeActivebeforePhoto(false);
        yield return(new WaitForEndOfFrame()); //Primordial

        changeActivebeforePhoto(true);
        Texture2D renderResult = ScreenCapture.CaptureScreenshotAsTexture();

        byte[] byteArray;
        //TODO ajouter un nouveau controlle pour prendre des screenshot entier
        squareScreenshot = true;
        if (squareScreenshot)
        {
            squareScreenshot = false;
            //Texture2D resultCentered = new Texture2D(Screen.height, Screen.height);
            //resultCentered.SetPixels(renderResult.GetPixels(Mathf.FloorToInt(Screen.width/2) - Mathf.FloorToInt(Screen.height/2), 0, Screen.height, Screen.height));
            Texture2D resultCentered = new Texture2D(Mathf.RoundToInt(Screen.width * squareFactor.x), Mathf.RoundToInt(Screen.height * squareFactor.y));
            resultCentered.SetPixels(renderResult.GetPixels(resultCentered.width / 2, resultCentered.height / 2, resultCentered.width, resultCentered.height));
            resultCentered.Apply();

            byteArray    = resultCentered.EncodeToPNG();
            renderResult = resultCentered;
        }
        else
        {
            byteArray = renderResult.EncodeToPNG();
        }
        Sprite toShowInEcodex = Sprite.Create(renderResult, new Rect(0.0f, 0.0f, renderResult.width, renderResult.height), Vector2.zero);
        bool   newSpecie      = false;
        bool   hasCreature    = creatureDetected != null;

        if (hasCreature)
        {
            newSpecie = EcodexManager.Instance.TakePictureOfCreature(toShowInEcodex, creatureDetected);
        }
        else
        {
            EcodexManager.Instance.TakePicture(toShowInEcodex);
        }

        System.IO.File.WriteAllBytes(path, byteArray);

        yield return(null);

        if (hasToFeedbackFlash)
        {
            CameraController.Instance.FlashlightController.ActiveForFlash(false);
        }

        //Feedback take photo
        whitePicture.color          = Color.white;
        feedbackCreatureSeen.sprite = toShowInEcodex;
        //feedbackCreatureSeen.preserveAspect = true;


        if (!hasCreature)
        {
            switch (detectionStatus)
            {
            case DetectionStatus.NotVisible:
                textFeedback.text = "";
                break;

            case DetectionStatus.TooFar:
                textFeedback.text = "La créature est trop éloignée";
                break;

            case DetectionStatus.FromTheBack:
                textFeedback.text = "La créature n'est pas de face";
                break;

            case DetectionStatus.IsSleep:
                textFeedback.text = "La créature est en train de dormir";
                break;

            case DetectionStatus.Multiple:
                textFeedback.text = "Plusieurs espèces sont sur l'image";
                break;

            case DetectionStatus.Weird:
                textFeedback.text = weridPhrases[UnityEngine.Random.Range(0, weridPhrases.Length)];
                break;

            default:
                textFeedback.text = "";
                break;
            }
        }
        else if (newSpecie)
        {
            textFeedback.text = "<b><color=#" + ColorUtility.ToHtmlStringRGB(creatureDetected.ColorSwap.GetColor()) + ">Nouvelle espèce</color> découverte !</b>\n<size=80%>Ouvrez l'Ecodex : nouvelles infos disponibles</size>";
        }
        else
        {
            textFeedback.text = "<b>Nouvelle photo de <color=#" + ColorUtility.ToHtmlStringRGB(creatureDetected.ColorSwap.GetColor()) + ">" + EcodexManager.Instance.GetUserName(creatureDetected.SpecieID) + "</color>.</b>";
        }

        textFeedback.text += "\n<size=40%>Disponible sur : <u>" + path + "</u></size>";

        if (flashIsActive)
        {
            dayColor.postExposure.value   = 2.5f;
            nightColor.postExposure.value = 2.5f;
        }

        float       totalTime    = 0f;
        const float feedbackTime = 1f;
        float       percentDone  = 0f;

        while (totalTime < feedbackTime)
        {
            totalTime  += Time.deltaTime;
            percentDone = totalTime / feedbackTime;
            Color newCol = whitePicture.color;
            newCol.a           = Mathf.Lerp(1f, 0f, percentDone);
            whitePicture.color = newCol;
            if (hasToFeedbackFlash)
            {
                dayColor.postExposure.value   = Mathf.Lerp(2.5f, postExposureDay, percentDone);
                nightColor.postExposure.value = Mathf.Lerp(2.5f, postExposureNight, percentDone);
            }
            yield return(null);
        }
        whitePicture.color = Color.clear;
        if (hasToFeedbackFlash)
        {
            dayColor.postExposure.value   = postExposureDay;
            nightColor.postExposure.value = postExposureNight;
        }
        yield return(new WaitForSeconds(2f));

        stopRecordingImage();
    }
    IEnumerator CalculateHomography()
    //void CalculateHomography()
    {
        //Debug.Log("CalculateHomography1");
        //myCam.Pause();
        yield return(new WaitForEndOfFrame());

        //yield return new WaitForSeconds((float)0.5);
        //程式開始後至少要等0.3秒才會出現影像畫面,不然算sift一開始就會記憶體爆掉

        //input camera image

        /*Texture2D sourceTex = ScreenCapture.CaptureScreenshotAsTexture();
         * Color[] pix = sourceTex.GetPixels((int)rectBotLeft.x, (int)rectBotLeft.y, width, height);
         * Texture2D tex = new Texture2D(width, height);
         * tex.SetPixels(pix);
         * tex.Apply();*/

        //Debug.Log("CalculateHomography2");

        //rawimage position at (0,0),start from bottom left
        int xStart = (int)(Screen.width - rawImageRT.rect.width) / 2;
        int yStart = (int)(Screen.height - rawImageRT.rect.height) / 2;

        /*Debug.Log("xStart: "+xStart);
         * Debug.Log("yStart: "+yStart);
         * Debug.Log("Screen.width: "+Screen.width);
         * Debug.Log("Screen.height: "+Screen.height);
         * Debug.Log("rawImageRT.rect.width: "+rawImageRT.rect.width);
         * Debug.Log("rawImageRT.rect.height: "+rawImageRT.rect.height);*/

        //get sign image with text
        Texture2D sourceTex = ScreenCapture.CaptureScreenshotAsTexture();

        //rawImageRI.texture = sourceTex;
        //Color[] pix = sourceTex.GetPixels((int)rectBotLeft.x, (int)rectBotLeft.y, width, height);
        Color[] pix = sourceTex.GetPixels(xStart, yStart, (int)rawImageRT.rect.width, (int)rawImageRT.rect.height);
        tex = new Texture2D((int)rawImageRT.rect.width, (int)rawImageRT.rect.height);
        tex.SetPixels(pix);
        tex.Apply();

        //Debug.Log("tex.width: "+tex.width);
        //Debug.Log("tex.height: "+tex.height);

        //input fixed image

        /*Texture2D tex = new Texture2D(2,2);
         * string imgPath = "../signboard-rectangle/test-199-fast-628.jpg";
         * byte [] binaryImageData = File.ReadAllBytes(imgPath);
         * tex.LoadImage(binaryImageData);*/

        //scale texture to make it smaller
        TextureScale.Bilinear(tex, tex.width / 2, tex.height / 2);

        //必要 防止記憶體爆炸
        tex = TextureGray.ToGray(tex);

        //rawImageRI.texture = tex;

        mat = Unity.TextureToMat(tex);

        Destroy(sourceTex);
        Destroy(tex);

        //Cv2.ImShow("img", mat); ok
        //OpenCvSharp.Mat mat = Cv2.ImRead(imgPath, ImreadModes.Unchanged);

        //Debug.Log("mat: "+mat.ToString());
        //string imgPath = "../signboard-rectangle/test-199-fast-628.jpg";
        //OpenCvSharp.Mat mat = Cv2.ImRead(imgPath);
        InputArray imgCam = InputArray.Create(mat);

        desCam = OutputArray.Create(mat);

        //Cv2.ImShow("img", mat); ok
        //OpenCvSharp.Mat mat2 = mat;

        //sift = SIFT.Create();

        //System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch();
        //time.Start ();

        //卡卡
        OpenCvSharp.KeyPoint[] kpCam = sift.Detect(mat);
        //OpenCvSharp.KeyPoint[] kpCam = surf.Detect(mat);
        //OpenCvSharp.KeyPoint[] kpCam = orb.Detect(mat);
        //OpenCvSharp.KeyPoint[] kpCam = brief.Detect(mat);

        //time.Stop();
        //Debug.Log("執行 " + time.Elapsed.TotalSeconds + " 秒");

        //myCam.Pause();
        //rawImageRI.texture = tex;

        //Cv2.ImShow("img", mat); ok
        //Cv2.ImShow("img", mat2); ok

        sift.Compute(imgCam, ref kpCam, desCam);
        //surf.Compute(img2, ref kpCam, desCam);
        //orb.Compute(img2, ref kpCam, desCam);
        //brief.Compute(img2, ref kpCam, desCam);

        //Cv2.ImShow("img", mat);
        //Cv2.ImShow("img", mat2); 爆炸

        OpenCvSharp.Mat desCammat = desCam.GetMat();
        //Debug.Log("desCammat: "+desCammat);

        //if (!M) 如果還沒計算出homography M {
        //desFirstCatch = desCam;
        //OutputArray descriptors_object = des1;

        OpenCvSharp.Mat des1mat = des1.GetMat();
        OpenCvSharp.Mat des2mat = des2.GetMat();
        //OpenCvSharp.Mat des3mat = des3.GetMat();
        //Debug.Log("des1mat: "+des1mat);

        OpenCvSharp.DMatch[] dmatch1 = descriptorMatcher.Match(des1mat, desCammat);
        OpenCvSharp.DMatch[] dmatch2 = descriptorMatcher.Match(des2mat, desCammat);
        //OpenCvSharp.DMatch[] dmatch3 = descriptorMatcher.Match(des3mat, desCammat);

        //Debug.Log("damtch1[0]: "+dmatch1[0].ToString());
        //}
        //else {
        //OpenCvSharp.Mat desFirstCatchmat = desFirstCatch.GetMat();

        // OpenCvSharp.DMatch[] dmatch = descriptorMatcher.Match(desFirstCatchmat, desCammat);
        // OutputArray descriptors_object = desFirstCatch;
        //}

        double max_dist1 = 0;
        double min_dist1 = 100;
        double max_dist2 = 0;
        double min_dist2 = 100;

        //double max_dist3 = 0;
        //double min_dist3 = 100;

        //Cv2.ImShow("img", mat); 爆炸

        //Quick calculation of max and min distances between keypoints
        foreach (OpenCvSharp.DMatch d in dmatch1)
        {
            double dist = d.Distance;
            if (dist < min_dist1)
            {
                min_dist1 = dist;
            }
            if (dist > max_dist1)
            {
                max_dist1 = dist;
            }
        }

        foreach (OpenCvSharp.DMatch d in dmatch2)
        {
            double dist = d.Distance;
            if (dist < min_dist2)
            {
                min_dist2 = dist;
            }
            if (dist > max_dist2)
            {
                max_dist2 = dist;
            }
        }

        /*foreach (OpenCvSharp.DMatch d in dmatch3){
         *  double dist = d.Distance;
         *  if( dist < min_dist3 ) min_dist3 = dist;
         *  if( dist > max_dist3 ) max_dist3 = dist;
         * }*/

        //Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
        List <OpenCvSharp.DMatch> goodMatch1 = new List <OpenCvSharp.DMatch>();

        foreach (OpenCvSharp.DMatch d in dmatch1)
        {
            if (d.Distance < 3 * min_dist1)
            {
                goodMatch1.Add(d);
            }
        }

        List <OpenCvSharp.DMatch> goodMatch2 = new List <OpenCvSharp.DMatch>();

        foreach (OpenCvSharp.DMatch d in dmatch2)
        {
            if (d.Distance < 3 * min_dist2)
            {
                goodMatch2.Add(d);
            }
        }

        /*List<OpenCvSharp.DMatch> goodMatch3 = new List<OpenCvSharp.DMatch>();
         * foreach (OpenCvSharp.DMatch d in dmatch3){
         *  if( d.Distance < 3*min_dist3 )
         *      goodMatch3.Add(d);
         * }*/

        List <OpenCvSharp.Point2f> srcPts1 = new List <OpenCvSharp.Point2f>();
        List <OpenCvSharp.Point2f> dstPts1 = new List <OpenCvSharp.Point2f>();

        foreach (OpenCvSharp.DMatch d in goodMatch1)
        {
            //-- Get the keypoints from the good matches
            srcPts1.Add(kp1[d.QueryIdx].Pt);
            dstPts1.Add(kpCam[d.TrainIdx].Pt);
            //Debug.Log("kp1[d.QueryIdx].Pt: "+kp1[d.QueryIdx].Pt);
        }

        List <OpenCvSharp.Point2f> srcPts2 = new List <OpenCvSharp.Point2f>();
        List <OpenCvSharp.Point2f> dstPts2 = new List <OpenCvSharp.Point2f>();

        foreach (OpenCvSharp.DMatch d in goodMatch2)
        {
            //-- Get the keypoints from the good matches
            srcPts2.Add(kp2[d.QueryIdx].Pt);
            dstPts2.Add(kpCam[d.TrainIdx].Pt);
            //Debug.Log("kp1[d.QueryIdx].Pt: "+kp1[d.QueryIdx].Pt);
        }

        /*List<OpenCvSharp.Point2f> srcPts3 = new List<OpenCvSharp.Point2f>();
         * List<OpenCvSharp.Point2f> dstPts3 = new List<OpenCvSharp.Point2f>();
         * foreach (OpenCvSharp.DMatch d in goodMatch3){
         *  //-- Get the keypoints from the good matches
         *  srcPts3.Add(kp3[d.QueryIdx].Pt);
         *  dstPts3.Add(kpCam[d.TrainIdx].Pt);
         *  //Debug.Log("kp1[d.QueryIdx].Pt: "+kp1[d.QueryIdx].Pt);
         * }*/

        //jump to next iteration if less than certain number of keypoints matched
        if (srcPts1.Count < 200 && srcPts2.Count < 200)
        {
            yield break;
        }

        if (srcPts1.Count >= srcPts2.Count)
        {
            srcPts        = new List <OpenCvSharp.Point2f>(srcPts1);
            dstPts        = new List <OpenCvSharp.Point2f>(dstPts1);
            text1.enabled = true;
            text2.enabled = false;
            num1++;
            //text3.enabled = false;
        }

        /*else if(srcPts2.Count >= srcPts1.Count && srcPts2.Count >= srcPts3.Count){
         *  srcPts = new List<OpenCvSharp.Point2f>(srcPts2);
         *  dstPts = new List<OpenCvSharp.Point2f>(dstPts2);
         *  text2.enabled = true;
         *  text1.enabled = false;
         *  text3.enabled = false;
         * }*/
        else
        {
            srcPts        = new List <OpenCvSharp.Point2f>(srcPts2);
            dstPts        = new List <OpenCvSharp.Point2f>(dstPts2);
            text2.enabled = true;
            text1.enabled = false;
            num2++;
            //text2.enabled = false;
        }

        if (num1 > num2 + 10)
        {
            text1.enabled = true;
            text2.enabled = false;
        }

        if (num2 > num1 + 10)
        {
            text2.enabled = true;
            text1.enabled = false;
        }

        if (num1 > 60 || num2 > 60)
        {
            num1 = 0;
            num2 = 0;
        }
        //OpenCvSharp.Mat mat2 = mat;

        //Cv2.DrawKeypoints(mat, kpCam, mat2);

        //Cv2.ImShow("img", mat); 亂碼圖

        //Texture2D tex2 = new Texture2D(8, 8);
        //tex2 = Unity.MatToTexture(mat);
        //rawImageRI.texture = tex2;
        //myCam.Pause();

        //Cv2.ImShow("img", mat2); 亂碼圖

        Texture2D emptyTex = new Texture2D(8, 8);

        OpenCvSharp.Mat outputImg = Unity.TextureToMat(emptyTex);
        //Debug.Log("outputImg: "+outputImg.ToString());

        InputArray  srcArr = InputArray.Create <OpenCvSharp.Point2f>(srcPts);
        InputArray  dstArr = InputArray.Create <OpenCvSharp.Point2f>(dstPts);
        OutputArray mask   = OutputArray.Create(outputImg);

        OpenCvSharp.Mat M = Cv2.FindHomography(srcArr, dstArr, HomographyMethods.Ransac, 5, mask);

        OpenCVForUnity.Mat transMat = new OpenCVForUnity.Mat(3, 3, CvType.CV_32FC1);
        transMat.put(0, 0, M.Get <double>(0, 0), M.Get <double>(0, 1), M.Get <double>(0, 2),
                     M.Get <double>(1, 0), M.Get <double>(1, 1), M.Get <double>(1, 2),
                     M.Get <double>(2, 0), M.Get <double>(2, 1), M.Get <double>(2, 2));
        //Debug.Log("transMat: "+transMat.dump());

        //Debug.Log("mask: "+mask);
        //OpenCvSharp.Mat maskMat = mask.GetMat();
        //Debug.Log("maskMat: "+maskMat.ToString());
        //maskMoB = new OpenCvSharp.MatOfByte(maskMat);

        //-- Get the corners from the image_1 ( the object to be "detected" )

        /*OpenCvSharp.Point2f[] obj_corners = new OpenCvSharp.Point2f[4];
         * obj_corners[0] = new OpenCvSharp.Point2f(0, 0);
         * obj_corners[1] = new OpenCvSharp.Point2f(inputTex.width, 0);
         * obj_corners[2] = new OpenCvSharp.Point2f(inputTex.width, inputTex.height);
         * obj_corners[3] = new OpenCvSharp.Point2f(0, inputTex.height);
         *
         * //OpenCvSharp.Point2f[] scene_corners = new OpenCvSharp.Point2f[4];
         * //scene_corners = Cv2.PerspectiveTransform(obj_corners, M);
         *
         * //if (!M) 如果還沒計算出homography M {
         * //Cv2.DrawMatches(inputImg, kp1, mat, kpCam, goodMatch, outputImg, OpenCvSharp.Scalar.All(-1),
         * //OpenCvSharp.Scalar.All(-1), maskMoB.ToArray(), DrawMatchesFlags.NotDrawSinglePoints);
         * //else {
         *
         * //Texture2D outputTex = Unity.MatToTexture(outputImg);
         * //rawImageRI.texture = outputTex;
         *
         * //-- Draw lines between the corners (the mapped object in the scene - image_2 )
         * //Cv2.Line(outputImg, scene_corners[0] + obj_corners[1], scene_corners[1] + obj_corners[1], OpenCvSharp.Scalar.LightBlue, 4);
         * //Cv2.Line(outputImg, scene_corners[1] + obj_corners[1], scene_corners[2] + obj_corners[1], OpenCvSharp.Scalar.LightBlue, 4);
         * //Cv2.Line(outputImg, scene_corners[2] + obj_corners[1], scene_corners[3] + obj_corners[1], OpenCvSharp.Scalar.LightBlue, 4);
         * //Cv2.Line(outputImg, scene_corners[3] + obj_corners[1], scene_corners[0] + obj_corners[1], OpenCvSharp.Scalar.LightBlue, 4);
         *
         * //OpenCvSharp.Mat outimg = Unity.TextureToMat(emptyTex);
         * //inputImg = Unity.TextureToMat(emptyTex);
         * //Cv2.DrawKeypoints(mat, kpCam, outimg, OpenCvSharp.Scalar.LightBlue);
         *
         * //show image with text after homography
         * /*string imgPath2 = "../signboard-rectangle/test-IMG_0204-text.PNG";
         * textTex = new Texture2D(2,2);
         * byte [] binaryImageData2 = File.ReadAllBytes(imgPath2);
         * textTex.LoadImage(binaryImageData2);
         * rawImageRI.texture = textTex;*/

        /*OpenCVForUnity.Mat inputTextImg = new OpenCVForUnity.Mat(new OpenCVForUnity.Size(textTex.width, textTex.height), CvType.CV_8UC4);
         * Utils.texture2DToMat(textTex, inputTextImg);
         * OpenCVForUnity.Mat outputTextImg = new OpenCVForUnity.Mat(new OpenCVForUnity.Size(textTex.width, textTex.height), CvType.CV_8UC4);
         *
         * Imgproc.warpPerspective(inputTextImg, outputTextImg, transMat, new OpenCVForUnity.Size(textTex.width, textTex.height));
         *
         * Texture2D outputTex = new Texture2D((int)textTex.width, (int)textTex.height, TextureFormat.RGB24, false);
         * Utils.matToTexture2D(outputTextImg, outputTex);*/

        //TextureScale.Bilinear(outputTex, outputTex.width/5, outputTex.height/5);
        //rawImageRI.texture = outputTex;

        //text.enabled = true;

        /*Vector3 scale;
         * scale.x = new Vector4((float)M.Get<double>(0,0), (float)M.Get<double>(1,0), (float)M.Get<double>(2,0), 0).magnitude;
         * scale.y = new Vector4((float)M.Get<double>(0,1), (float)M.Get<double>(1,1), (float)M.Get<double>(2,1), 0).magnitude;
         * scale.z = new Vector4((float)M.Get<double>(0,2), (float)M.Get<double>(1,2), (float)M.Get<double>(2,2), 0).magnitude;
         *
         * Vector3 forward;
         * forward.x = (float)M.Get<double>(0,2);
         * forward.y = (float)M.Get<double>(1,2);
         * forward.z = (float)M.Get<double>(2,2);
         *
         * Vector3 upwards;
         * upwards.x = (float)M.Get<double>(0,1);
         * upwards.y = (float)M.Get<double>(1,1);
         * upwards.z = (float)M.Get<double>(2,1);
         *
         * //textRT.localScale = scale;
         * //textRT.rotation = Quaternion.LookRotation(forward, upwards);*/

        Matrix4x4 matrix = new Matrix4x4();

        /*matrix.SetRow(0, new Vector4((float)M.Get<double>(0,0), (float)M.Get<double>(0,1), (float)M.Get<double>(0,2),0));
         * matrix.SetRow(1, new Vector4((float)M.Get<double>(1,0), (float)M.Get<double>(1,1), (float)M.Get<double>(1,2),0));
         * matrix.SetRow(2, new Vector4(0,0,1,0));
         * matrix.SetRow(3, new Vector4(0,0,0,1));*/

        //inverse效果還行
        matrix.SetRow(0, new Vector4((float)M.Get <double>(0, 0), (float)M.Get <double>(0, 1), 0, (float)M.Get <double>(0, 2)));
        matrix.SetRow(1, new Vector4((float)M.Get <double>(1, 0), (float)M.Get <double>(1, 1), 0, (float)M.Get <double>(1, 2)));
        matrix.SetRow(2, new Vector4(0, 0, 1, 0));
        matrix.SetRow(3, new Vector4(0, 0, 0, 1));

        Matrix4x4 inverse = matrix.inverse;

        //textRT.localScale = matrix.lossyScale;
        //textRT.rotation = matrix.rotation; //rotation跟eulerangles效果一樣
        textRT1.rotation = inverse.rotation;
        textRT2.rotation = inverse.rotation;
        //textRT3.rotation = inverse.rotation;

        Destroy(emptyTex);

        //calculate euler angle

        /*double angleX = Math.Asin(-M.Get<double>(2,1));
         * double angleY = Math.Atan2(M.Get<double>(2,0), M.Get<double>(2,2));
         * double angleZ = Math.Atan2(M.Get<double>(0,1), M.Get<double>(1,1));
         * //textRT.eulerAngles = new Vector3((float)angleX, (float)angleY, (float)angleZ);
         * //Debug.Log("textRT.eulerAngles: "+textRT.eulerAngles.ToString());
         *
         * //calculate quaternion
         * double w = Math.Sqrt(1 + M.Get<double>(0,0) + M.Get<double>(1,1) + M.Get<double>(2,2))/2;
         * double w4 = w*4;
         * double qx = (M.Get<double>(2,1) - M.Get<double>(1,2))/w4 ;
         * double qy = (M.Get<double>(0,2) - M.Get<double>(2,0))/w4 ;
         * double qz = (M.Get<double>(1,0) - M.Get<double>(0,1))/w4 ;
         * //textRT.rotation = new Quaternion((float)qx, (float)qy, (float)qz, 1);
         *
         * double tr = M.Get<double>(0,0) + M.Get<double>(1,1) + M.Get<double>(2,2);
         * Debug.Log("tr: "+tr);*/

        //Cv2.ImShow("img", mat);
        //myCam.Pause();
    }
Esempio n. 20
0
 public override Texture2D GetFrame()
 {
     image = ScreenCapture.CaptureScreenshotAsTexture();
     return(image);
 }
Esempio n. 21
0
        public void Screenshot(int maximumWidth, int maximumHeight, ScreenshotType type, Action <byte[], object> callback, object state)
        {
            Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();

            this.Screenshot(texture, maximumWidth, maximumHeight, type, callback, state);
        }
Esempio n. 22
0
    public static IEnumerator TweetWithScreenShot(string str)
    {
        yield return(new WaitForEndOfFrame());

        var tex = ScreenCapture.CaptureScreenshotAsTexture();

        // imgurへアップロード
        string uploadedURL = string.Empty;

        UnityWebRequest www;

        WWWForm wwwForm = new WWWForm();

        wwwForm.AddField("image", Convert.ToBase64String(tex.EncodeToJPG()));
        wwwForm.AddField("type", "base64");

        www = UnityWebRequest.Post("https://api.imgur.com/3/image.xml", wwwForm);

        www.SetRequestHeader("AUTHORIZATION", "Client-ID " + Instance.ClientID);

        yield return(www.SendWebRequest());

        if (www.isNetworkError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Data: " + www.downloadHandler.text);
            XDocument xDoc = XDocument.Parse(www.downloadHandler.text);
            // TwitterCard用に拡張子を外す
            string url = xDoc.Element("data").Element("link").Value;
            url         = url.Remove(url.Length - 4, 4);
            uploadedURL = url;
        }

        string hashtags = "#Unity #indiedev"; // ツイートに挿入するハッシュタグ
        string gameUrl  = string.Empty;       // ゲームのURL

#if UNITY_EDITOR
        gameUrl = "https://sgtkraft.github.io/oju-10seconds/";
        str    += string.Format("{0}\n{1}\n{2}", hashtags, gameUrl, uploadedURL);
#elif OJU_ATSUMARU
        hashtags += " #RPGアツマール";
        gameUrl   = "https://game.nicovideo.jp/atsumaru/games/gm17898";
        str      += string.Format("{0}\n{1}", hashtags, gameUrl);
#elif UNITY_WEBGL
        string referrer = GetWebUrl();
        if (referrer.Contains("unityroom"))
        {
            hashtags += " #unityroom";
            gameUrl   = "https://unityroom.com/games/oju-10seconds";
        }
        else
        {
            gameUrl = "https://sgtkraft.github.io/oju-10seconds/";
        }
        str += string.Format("{0}\n{1}\n{2}", hashtags, gameUrl, uploadedURL);
#endif

        // Twitter投稿用URL
        string tweetUrl = "https://twitter.com/intent/tweet?text=" + UnityWebRequest.EscapeURL(str);
        if (isDebugActive)
        {
            Instance.debugText.text += ("\n" + gameUrl);
        }
        if (isDebugActive)
        {
            Instance.debugText.text += ("\n" + tweetUrl);
        }

#if UNITY_EDITOR
        System.Diagnostics.Process.Start(tweetUrl);
        if (isDebugActive)
        {
            Instance.debugText.text += "\nEditor";
        }
#elif OJU_ATSUMARU
        am.OpenLink(tweetUrl);
        if (isDebugActive)
        {
            Instance.debugText.text += "\nAtsumaru";
        }
#elif UNITY_WEBGL
        OpenNewWindow(tweetUrl);
        if (isDebugActive)
        {
            Instance.debugText.text += "\nWebGL";
        }
#else
        Application.OpenURL(tweetUrl);
        if (isDebugActive)
        {
            Instance.debugText.text += "\nOther";
        }
#endif
    }
 public override void NewFrameReady(RecordingSession session)
 {
     image = ScreenCapture.CaptureScreenshotAsTexture();
 }
Esempio n. 24
0
 public void TakeScreenshot()
 {
     screenshot = ScreenCapture.CaptureScreenshotAsTexture();
 }
        /// <summary>
        /// 截屏并截取分享部分的图片
        /// </summary>
        /// <returns>The screen shot and share.</returns>
        private IEnumerator TrimScreenShotAndShare()
        {
            yield return(new WaitForEndOfFrame());

            // 截屏
            Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();

            Debug.LogFormat("截屏图片大小[{0},{1}]", texture.width, texture.height);

            float transferScaler = 1f;

            // 按照分辨率来确认转换系数
            if (Camera.main.pixelWidth < 1080f)
            {
                if (CommonData.HWScalerOfCurrentScreen < 1.7f)
                {
                    transferScaler = CommonData.scalerToPresetH;
                }
                else
                {
                    transferScaler = CommonData.scalerToPresetW;
                }
            }

            // 获取分享区域的大小
            int shareHUDWidth  = (int)((shareShotcutRect.transform as RectTransform).rect.width * transferScaler);
            int shareHUDHeight = (int)((shareShotcutRect.transform as RectTransform).rect.height * transferScaler);


            Debug.LogFormat("实际图片大小:[{0},{1}]", shareHUDWidth, shareHUDHeight);

            // 分享区域在y方向的偏移
            int sharePlaneFixY = (int)(sharePlane.localPosition.y * transferScaler);

            Debug.LogFormat("3y:{0}", sharePlane.localPosition.y);

            // 分享区域的X方向offset和Y方向offset
            int offsetYFix = (int)(shareShotcutRect.localPosition.y * transferScaler);
            int offsetX    = (texture.width - shareHUDWidth) / 2;

            // 实际分享区域的最小y值
            int offsetYMin = (texture.height - shareHUDHeight) / 2 + offsetYFix + sharePlaneFixY;
            // 实际分享区域的最大y值
            int offsetYMax = (texture.height + shareHUDHeight) / 2 + offsetYFix + sharePlaneFixY;


            //int offsetYMin = offsetYFix;
            //int offsetYMax = offsetYFix + shareHUDHeight;

            Debug.LogFormat("实际最小y{0},最大y{1}", offsetYMin, offsetYMax);

            // 按照分享图片的大小创建新的空纹理
            Texture2D newT2d = new Texture2D(shareHUDWidth, shareHUDHeight);

            // 像素处理
            for (int i = offsetX; i < texture.width - offsetX; i++)
            {
                for (int j = offsetYMin; j < offsetYMax; j++)
                {
                    Color c = texture.GetPixel(i, j);

                    newT2d.SetPixel(i - offsetX, j - offsetYMin, c);
                }
            }


            // 纹理应用
            newT2d.Apply();

            // 纹理转化为jpg格式二进制数据
            byte[] trimImgData = newT2d.EncodeToJPG();

            // 检查临时分享文件夹是否存在,不存在创建文件夹
            if (!DataHandler.DirectoryExist(Application.persistentDataPath + "/tempPics"))
            {
                DataHandler.CreateDirectory(Application.persistentDataPath + "/tempPics");
            }

            // 临时图片保存位置
            string trimImgPath = Application.persistentDataPath + "/tempPics/shareImage.jpg";

            // 保存图片
            File.WriteAllBytes(trimImgPath, trimImgData);

            // 清理工作
            Destroy(texture);
            Destroy(newT2d);

            texture     = null;
            newT2d      = null;
            trimImgData = null;

            Resources.UnloadUnusedAssets();
            GC.Collect();

#if UNITY_EDITOR
            //DataHandler.DeleteFile(Application.persistentDataPath + "/tempPics/shareImage.jpg");
            QuitShareView();
#elif UNITY_IOS || UNITY_ANDROID
            Share();
#endif
        }
        /// <summary>
        /// Get path to game screenshot when exception occured
        /// </summary>
        /// <returns>Path to game screenshot</returns>
        private string GetScreenshotPath(string dataPrefix)
        {
            if (!_settings.GenerateScreenshotOnException)
            {
                return(string.Empty);
            }
            var screenshotPath = Path.Combine(_settings.DatabasePath, string.Format("{0}-screen.jpg", dataPrefix));

            lock (_lock)
            {
                if (BacktraceDatabase.LastFrameTime == _lastScreenTime)
                {
                    if (File.Exists(_lastScreenPath))
                    {
                        File.Copy(_lastScreenPath, screenshotPath);
                        return(screenshotPath);
                    }
                    return(_lastScreenPath);
                }
                else
                {
                    Texture2D result;
                    if (ScreenshotMaxHeight < Screen.height)
                    {
                        float ratio        = Screen.width / Screen.height;
                        int   targetHeight = Mathf.Min(Screen.height, ScreenshotMaxHeight);
                        int   targetWidth  = Mathf.RoundToInt(targetHeight * ratio);

#if UNITY_2019_1_OR_NEWER
                        RenderTexture screenTexture = RenderTexture.GetTemporary(Screen.width, Screen.height);
                        ScreenCapture.CaptureScreenshotIntoRenderTexture(screenTexture);
#else
                        Texture2D screenTexture = ScreenCapture.CaptureScreenshotAsTexture();
#endif

                        // Create a render texture to render into
                        RenderTexture rt = RenderTexture.GetTemporary(targetWidth, targetHeight);

                        if (SystemInfo.graphicsUVStartsAtTop)
                        {
                            Graphics.Blit(screenTexture, rt, new Vector2(1.0f, -1.0f), new Vector2(0.0f, 1.0f));
                        }
                        else
                        {
                            Graphics.Blit(screenTexture, rt);
                        }

                        RenderTexture previousActiveRT = RenderTexture.active;
                        RenderTexture.active = rt;

                        // Create a texture & read data from the active RenderTexture
                        result = new Texture2D(targetWidth, targetHeight, TextureFormat.RGB24, false);
                        result.ReadPixels(new Rect(0, 0, targetWidth, targetHeight), 0, 0);
                        result.Apply();

                        // Reset to initial state
                        RenderTexture.active = previousActiveRT;

                        RenderTexture.ReleaseTemporary(rt);

#if UNITY_2019_1_OR_NEWER
                        RenderTexture.ReleaseTemporary(screenTexture);
#else
                        GameObject.Destroy(screenTexture);
#endif
                    }
                    else
                    {
                        result = ScreenCapture.CaptureScreenshotAsTexture();
                    }

                    File.WriteAllBytes(screenshotPath, result.EncodeToJPG(ScreenshotQuality));
                    GameObject.Destroy(result);

                    _lastScreenTime = BacktraceDatabase.LastFrameTime;
                    _lastScreenPath = screenshotPath;
                }
            }
            return(screenshotPath);
        }
Esempio n. 27
0
 // Token: 0x06005249 RID: 21065 RVA: 0x0018650C File Offset: 0x0018470C
 private void Test(string str)
 {
     string[] array = str.Split(new char[]
     {
         ' '
     });
     if (array.Length == 0)
     {
         return;
     }
     if (array[0].ToLower() == "logout")
     {
         if (PDSDK.Instance != null)
         {
             PDSDK.Instance.onLogoutSuccess("{\"customparams\":\"\"}");
         }
     }
     else if (array[0].ToLower() == "lua")
     {
         if (PDSDK.m_eventLoginSuccess != null)
         {
             PDSDK.m_eventLoginSuccess(new LoginSuccessMsg("t1", "t2", "t3", "t4"));
         }
         if (PDSDK.m_eventLoginFailed != null)
         {
             PDSDK.m_eventLoginFailed();
         }
         if (PDSDK.m_eventLogoutSuccess != null)
         {
             PDSDK.m_eventLogoutSuccess();
         }
         if (PDSDK.m_eventInitSuccess != null)
         {
             PDSDK.m_eventInitSuccess();
         }
         if (PDSDK.m_eventInitFailed != null)
         {
             PDSDK.m_eventInitFailed();
         }
     }
     else if (array[0].ToLower() == "ei")
     {
         CommonUIController.Instance.PDSDKEnableInput(true);
     }
     else if (array[0].ToLower() == "di")
     {
         CommonUIController.Instance.PDSDKEnableInput(false);
     }
     else if (array[0].ToLower() == "get")
     {
         List <PDSDKGood> list = StoreUITask.PDSDKGetGoods();
         foreach (PDSDKGood pdsdkgood in list)
         {
             global::Debug.Log(string.Format("id={0} regID={1} name={2} type={3} price={4} desc={5}", new object[]
             {
                 pdsdkgood.m_ID,
                 pdsdkgood.m_registerID,
                 pdsdkgood.m_name,
                 pdsdkgood.m_type,
                 pdsdkgood.m_price,
                 pdsdkgood.m_desc
             }));
         }
     }
     else if (array[0].ToLower() == "req")
     {
         StoreUITask.PDSDKRequestGoods();
     }
     else if (array[0].ToLower() == "buy")
     {
         if (array.Length < 4)
         {
             return;
         }
         string        goodID   = array[1];
         PDSDKGoodType goodType = PDSDKGoodType.Normal;
         int           number   = int.Parse(array[3]);
         StoreUITask.PDSDKRequestBuyGoods(goodID, goodType, number);
     }
     else if (array[0].ToLower() == "capture")
     {
         Texture2D texture2D = ScreenCapture.CaptureScreenshotAsTexture(1);
         File.WriteAllBytes(Application.persistentDataPath + "/ShareImage.jpg", texture2D.EncodeToJPG(100));
         Texture2D tex = Utility.ResizeTexture(texture2D, ImageFilterMode.Average, 300f / (float)texture2D.width);
         File.WriteAllBytes(Application.persistentDataPath + "/thumbShareImage.jpg", tex.EncodeToJPG(100));
         JsonData jsonData = new JsonData();
         jsonData["Title"]          = "这是分享的标题";
         jsonData["TitleUrl"]       = "http://q.zilongame.com/";
         jsonData["Text"]           = "这是分享的正文";
         jsonData["ImagePath"]      = Application.persistentDataPath + "/ShareImage.jpg";
         jsonData["thumbImagePath"] = Application.persistentDataPath + "/thumbShareImage.jpg";
         jsonData["SiteUrl"]        = "http://q.zilongame.com/";
         jsonData["Url"]            = "http://q.zilongame.com/";
         jsonData["shareType"]      = 2;
         jsonData["sharePlatform"]  = 2;
         if (array.Length > 1)
         {
             jsonData["sharePlatform"] = int.Parse(array[1]);
         }
         PDSDK.Instance.doshare(jsonData.ToJson());
     }
 }
Esempio n. 28
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            objetoArrasta = GetTransformObj();

            if (objetoArrasta != null)
            {
                isArrasta = true;
                objetoArrasta.SetAsLastSibling();

                posicaoOriginal         = objetoArrasta.position;
                objetoArrastaImg        = objetoArrasta.GetComponent <Image>();
                objLixeira.enabled      = true;
                objBotaoAvancar.enabled = false;

                if (objetoArrasta.parent.name == "imagens")
                {
                    Transform copia = Instantiate(objetoArrasta);
                    copia.SetParent(objetoArrasta.parent);
                    copia.position   = posicaoOriginal;
                    copia.localScale = objetoArrasta.localScale;

                    widthObj  = copia.GetComponent <RectTransform>().rect.width;
                    HeightObj = copia.GetComponent <RectTransform>().rect.height;
                }
                objetoArrasta.SetParent(containerDesenho);
                objetoArrastaImg.raycastTarget = false;
            }
        }


        if (isArrasta)
        {
            objComponente           = objetoArrasta.GetComponent <RectTransform>();
            objComponente.sizeDelta = GetSizeObjeto();
            objetoArrasta.position  = GetPositionObjeto();
            isSalvo = false;
        }
        else
        {
            if (!isSalvo)
            {
                scroll.SetActive(false);
                quad.SetActive(false);
                compareBtn.SetActive(false);

                //File.Delete(Application.dataPath + "/OpenCVForUnity/Examples/Resources/ScreenCapture.jpg");
                //ScreenCapture.CaptureScreenshot(Application.dataPath + "/OpenCVForUnity/Examples/Resources/ScreenCapture.jpg", 2);
                UIArrastar.teste = ScreenCapture.CaptureScreenshotAsTexture(2);

                isSalvo = true;
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            if (objetoArrasta != null)
            {
                objetoArrastaImg.raycastTarget = true;

                if (scrollView != null)
                {
                    if (ObjetoSobreposLixeira())
                    {
                        Destroy(objetoArrasta.gameObject);
                    }
                }

                objetoArrasta = null;
            }

            isArrasta               = false;
            objLixeira.enabled      = false;
            objBotaoAvancar.enabled = true;
        }

        if (isSalvo && File.Exists(Application.dataPath + "/OpenCVForUnity/Examples/Resources/ScreenCapture.jpg"))
        {
            scroll.SetActive(true);
            quad.SetActive(true);
            compareBtn.SetActive(true);
        }
    }
Esempio n. 29
0
        private IEnumerator AssertNoVisualRegressionCoroutine(string id, StackTrace stacktrace)
        {
            id = EnvironmentV2.SanatizeToFileName(id);
            if (id.IsNullOrEmpty())
            {
                throw new ArgumentNullException("Invalid ID passed");
            }

            var idFolder = GetFolderFor(id);
            var oldImg   = idFolder.GetChild(id + ".regression.jpg");
            var newImg   = idFolder.GetChild(id + ".jpg");
            var backup   = idFolder.GetChild(id + ".jpg.backup");

            Config config = LoadConfigFor(id);

            yield return(new WaitForEndOfFrame());

            Texture2D screenShot = ScreenCapture.CaptureScreenshotAsTexture(config.screenshotUpscaleFactor);

            // Texture2D screenShot = Camera.allCameras.CaptureScreenshot(); // Does not capture UI

            if (newImg.Exists)
            {
                newImg.CopyToV2(backup, replaceExisting: false);
            }
            screenShot.SaveToJpgFile(newImg, config.screenshotQuality);
            screenShot.Destroy();

            var diffImg = CalculateDiffImage(oldImg, newImg, config.maxAllowedDiff, config.errorMetric);

            if (diffImg != null)
            {
                var e = $"Visual diff to previous '{id}' detected! To approve an allowed visual change, delete '{oldImg.Name}'";
                if (!config.customErrorMessage.IsNullOrEmpty())
                {
                    e = config.customErrorMessage + "/n" + e;
                }
                var exeption = new Error(e, stacktrace);
                if (config.throwAssertException)
                {
                    throw exeption;
                }
                else if (config.logAsError)
                {
                    Log.e(exeption);
                }
                else if (config.logAsWarning)
                {
                    Log.w(e);
                }
                if (config.openExternallyOnAssertFail)
                {
                    diffImg.Parent.OpenInExternalApp();
                    diffImg.OpenInExternalApp();
                }
            }
            else     // No difference between oldImg and newImg
            // To prevent git from detecting invalid file changes:
            {
                if (backup.Exists)                               // If there is a backup of newImg..
                {
                    newImg.DeleteV2();                           // Remove the newly generated version ..
                    backup.Rename(newImg.Name, out FileEntry _); // and replace it with the backup
                }
            }
            backup.DeleteV2(); // If a backup file was created during the process delete it
            AssertV2.IsTrue(newImg.Exists, "newImg did not exist after AssertNoVisualChange done");
        }
Esempio n. 30
0
 public void update()
 {
     tex    = ScreenCapture.CaptureScreenshotAsTexture();
     width  = tex.width;
     colors = tex.GetPixels32();
 }