Exemple #1
0
        // Update is called once per frame
        void Update()
        {
            if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
            {
                Mat rgbaMat = webCamTextureToMatHelper.GetMat();

                if (applyComicFilter)
                {
                    comicFilter.Process(rgbaMat, rgbaMat);
                }

                if (NatCorder.IsRecording)
                {
                    Imgproc.putText(rgbaMat, "[NatCorder With OpenCVForUnity Example]", new Point(5, rgbaMat.rows() - 30), Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(255, 255, 255, 255), 1, Imgproc.LINE_AA, false);
                    Imgproc.putText(rgbaMat, "- Video Recording Example", new Point(5, rgbaMat.rows() - 10), Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(255, 255, 255, 255), 1, Imgproc.LINE_AA, false);
                }

                // Restore the coordinate system of the image by OpenCV's Flip function.
                Utils.fastMatToTexture2D(rgbaMat, texture);

                if (NatCorder.IsRecording)
                {
                    // Blit to recording frame
                    var encoderFrame = NatCorder.AcquireFrame();
                    Graphics.Blit(texture, encoderFrame);
                    NatCorder.CommitFrame(encoderFrame, recordingClock.CurrentTimestamp);
                }
            }

            if (isVideoPlaying && videoPlayer.isPlaying)
            {
                gameObject.GetComponent <Renderer> ().sharedMaterial.mainTexture = videoPlayer.texture;
            }
        }
Exemple #2
0
        // Update is called once per frame
        void Update()
        {
            if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
            {
                Mat rgbaMat = webCamTextureToMatHelper.GetMat();

                if (applyComicFilter)
                {
                    comicFilter.Process(rgbaMat, rgbaMat);
                }

                if (isVideoRecording && !isFinishWriting)
                {
                    textPos.x = 5;
                    textPos.y = rgbaMat.rows() - 70;
                    Imgproc.putText(rgbaMat, exampleTitle, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                    textPos.y = rgbaMat.rows() - 50;
                    Imgproc.putText(rgbaMat, exampleSceneTitle, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                    if (container == ContainerPreset.MP4 || container == ContainerPreset.HEVC)
                    {
                        textPos.y = rgbaMat.rows() - 30;
                        Imgproc.putText(rgbaMat, settingInfo1, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                        textPos.y = rgbaMat.rows() - 10;
                        Imgproc.putText(rgbaMat, settingInfo2, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                    }
                    else if (container == ContainerPreset.GIF)
                    {
                        textPos.y = rgbaMat.rows() - 30;
                        Imgproc.putText(rgbaMat, settingInfoGIF, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                    }
                    else if (container == ContainerPreset.JPG)
                    {
                        textPos.y = rgbaMat.rows() - 30;
                        Imgproc.putText(rgbaMat, settingInfoJPG, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                    }
                }

                // Restore the coordinate system of the image by OpenCV's Flip function.
                Utils.fastMatToTexture2D(rgbaMat, texture);

                // Record frames
                if (videoRecorder != null && (isVideoRecording && !isFinishWriting) && frameCount++ % recordEveryNthFrame == 0)
                {
                    videoRecorder.CommitFrame((IntPtr)rgbaMat.dataAddr(), recordingClock.timestamp);
                }
            }

            if (isVideoPlaying && videoPlayer.isPlaying)
            {
                gameObject.GetComponent <Renderer>().sharedMaterial.mainTexture = videoPlayer.texture;
            }
        }
        // Update is called once per frame
        void Update()
        {
            if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
            {
                Mat rgbaMat = webCamTextureToMatHelper.GetMat();

                if (applyComicFilter)
                {
                    comicFilter.Process(rgbaMat, rgbaMat);
                }

                if (isVideoRecording && !isFinishWriting)
                {
                    textPos.x = 5;
                    textPos.y = rgbaMat.rows() - 70;
                    Imgproc.putText(rgbaMat, exampleTitle, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                    textPos.y = rgbaMat.rows() - 50;
                    Imgproc.putText(rgbaMat, exampleSceneTitle, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                    if (container == ContainerPreset.MP4 || container == ContainerPreset.HEVC)
                    {
                        textPos.y = rgbaMat.rows() - 30;
                        Imgproc.putText(rgbaMat, settingInfo1, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                        textPos.y = rgbaMat.rows() - 10;
                        Imgproc.putText(rgbaMat, settingInfo2, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                    }
                    else if (container == ContainerPreset.GIF)
                    {
                        textPos.y = rgbaMat.rows() - 30;
                        Imgproc.putText(rgbaMat, settingInfoGIF, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                    }
                    else if (container == ContainerPreset.JPG)
                    {
                        textPos.y = rgbaMat.rows() - 30;
                        Imgproc.putText(rgbaMat, settingInfoJPG, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
                    }
                }

                // Upload the image Mat data to the Texture2D.
                // (The internal processing of the fastMatToTexture method restore the image Mat data to Unity coordinate system)
                Utils.fastMatToTexture2D(rgbaMat, texture);

                // Record frames
                if (videoRecorder != null && (isVideoRecording && !isFinishWriting) && frameCount++ % recordEveryNthFrame == 0)
                {
#if !OPENCV_USE_UNSAFE_CODE
                    MatUtils.copyFromMat(rgbaMat, pixelBuffer);
                    videoRecorder.CommitFrame(pixelBuffer, recordingClock.timestamp);
#else
                    unsafe
                    {
                        videoRecorder.CommitFrame((void *)rgbaMat.dataAddr(), recordingClock.timestamp);
                    }
#endif
                }
            }

            if (isVideoPlaying && videoPlayer.isPlaying)
            {
                gameObject.GetComponent <Renderer>().sharedMaterial.mainTexture = videoPlayer.texture;
            }
        }