unity使用Texture在内存中切割图片

在unity2018中使用摄像头的WebCamTexture tex获取到图像后,需要对获得的图像进行切割处理,参考网上的代码进行了简单的切割处理,具体过程如下:

一、打开摄像头

 public RawImage m_orinalImage;
    public RawImage m_orinalImage2;
    WebCamTexture tex;
    byte[] imageDataBytes;//图像的buffer

void autoOpenVideo()
    {

        if (!gameObject.activeSelf)
            return;
        bool isOpenFlag = false;

        if (tex == null)
            isOpenFlag = true;
        else if (!tex.isPlaying)
        {
            isOpenFlag = true;
        }
        if (isOpenFlag)
        {
            m_orinalImage.texture = null;
            StartCoroutine(OpenVideo());
        }
    }
    IEnumerator OpenVideo()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);

        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            tex = new WebCamTexture("Integrated Camera",640,480, 30);
            tex.Play();

            m_orinalImage.texture = tex;
        }
    }

二、截图

IEnumerator CapturePhoto()
    {
        yield return new WaitForEndOfFrame();
        if (tex != null)
        {
            Texture2D t2d = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, true);

            //将WebCamTexture 的像素保存到texture2D中
            t2d.SetPixels(tex.GetPixels());
            t2d.Apply();
            StopCamera();
            SetBuffer(t2d.EncodeToJPG());
        }

        Resources.UnloadUnusedAssets();//卸载未占用的asset资源
        System.GC.Collect();//回收内存
    }
public void SetBuffer(byte[] buffer)
    {
        if (imageDataBytes == null)
            imageDataBytes = new byte[buffer.Length];

        imageDataBytes = buffer;

        Texture2D texture = new Texture2D(1, 1);
        texture.LoadImage(imageDataBytes);
        Texture2D textureLeft = ScaleTextureCutOut(texture, 0, 0, texture.width/2, texture.height);
        m_orinalImage2.texture = textureLeft;

    }
 Texture2D ScaleTextureCutOut(Texture2D originalTexture, float startX, float startY, float originalWidth, float originalHeight)
    {
        originalWidth = Mathf.Clamp(originalWidth, 0, Mathf.Max(originalTexture.width - startX, 0));
        originalHeight = Mathf.Clamp(originalHeight, 0, Mathf.Max(originalTexture.height - startY, 0));
        Texture2D newTexture = new Texture2D(Mathf.CeilToInt(originalWidth), Mathf.CeilToInt(originalHeight));
        int maxX = originalTexture.width - 1;
        int maxY = originalTexture.height - 1;
        for (int y = 0; y < newTexture.height; y++)
        {
            for (int x = 0; x < newTexture.width; x++)
            {
                float targetX = x + startX;
                float targetY = y + startY;
                int x1 = Mathf.Min(maxX, Mathf.FloorToInt(targetX));
                int y1 = Mathf.Min(maxY, Mathf.FloorToInt(targetY));
                int x2 = Mathf.Min(maxX, x1 + 1);
                int y2 = Mathf.Min(maxY, y1 + 1);

                float u = targetX - x1;
                float v = targetY - y1;
                float w1 = (1 - u) * (1 - v);
                float w2 = u * (1 - v);
                float w3 = (1 - u) * v;
                float w4 = u * v;
                Color color1 = originalTexture.GetPixel(x1, y1);
                Color color2 = originalTexture.GetPixel(x2, y1);
                Color color3 = originalTexture.GetPixel(x1, y2);
                Color color4 = originalTexture.GetPixel(x2, y2);
                Color color = new Color(Mathf.Clamp01(color1.r * w1 + color2.r * w2 + color3.r * w3 + color4.r * w4),
                                        Mathf.Clamp01(color1.g * w1 + color2.g * w2 + color3.g * w3 + color4.g * w4),
                                        Mathf.Clamp01(color1.b * w1 + color2.b * w2 + color3.b * w3 + color4.b * w4),
                                        Mathf.Clamp01(color1.a * w1 + color2.a * w2 + color3.a * w3 + color4.a * w4)
                                        );
                newTexture.SetPixel(x, y, color);
            }
        }
        newTexture.anisoLevel = 2;
        newTexture.Apply();
        return newTexture;
    }
    

备注:参考unity 内存中切割图片使用了GetPixels的方式进行

使用UV贴图的方式大家可以参考 unity3d任意图片截取和裁剪(我没有成功)

你可能感兴趣的:(C#,unity)