当前位置:   article > 正文

Unity中生成相机照片并保存到本地的详细教程_unity 如何将相机图像保存到贴图

unity 如何将相机图像保存到贴图


前言

在游戏和应用程序开发中,有时候需要通过代码在Unity中使用相机捕捉当前场景或特定视角的照片,并将其保存到本地。本教程将演示如何通过C#代码实现这一功能。


一、代码基本结构

 /// <summary>
    /// 生成相机照片并保存
    /// </summary>
    /// <param name="photographyCamera">相机</param>
    /// <param name="width">图像宽度</param>
    /// <param name="height">图像高度</param>
    /// <param name="path">保存路径</param>
    /// <param name="imageName">保存图片名字</param>
    public void CreateCameraCaptureAndSaveLocal(Camera photographyCamera,int width,int height, string path, string imageName){
        // 销毁之前的 RenderTexture 和 Texture2D
        if (photographyCamera.targetTexture != null){
            RenderTexture.ReleaseTemporary(photographyCamera.targetTexture);
            photographyCamera.targetTexture = null;
            RenderTexture.active = null;
        }

        // 创建 RenderTexture
        RenderTexture rt = new RenderTexture(width, height, 16, RenderTextureFormat.ARGB32);
        photographyCamera.targetTexture = rt;
        GL.Clear(true, true, Color.clear); // 清除颜色和深度缓冲区
        photographyCamera.Render();
        RenderTexture.active = rt;

        // 创建 Texture2D 并读取图像数据
        Texture2D image = new Texture2D(width, height, TextureFormat.ARGB32, false);
        image.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        image.Apply();

        // 重要:将 targetTexture 设置为 null,以便相机继续渲染到主屏幕
        photographyCamera.targetTexture = null;
        RenderTexture.active = null;

        // 检查保存路径是否为空或无效
        if (string.IsNullOrEmpty(path)){
            Debug.LogError("Invalid save path.");
            return;
        }

        // 如果文件夹不存在,则创建文件夹
        if (!Directory.Exists(path)){
            Directory.CreateDirectory(path);
        }

        // 保存图像到本地文件夹

        byte[] bytes = image.EncodeToJPG();
        if (bytes != null){
            string savePath = Path.Combine(path, imageName + ".jpg");

            try{
                File.WriteAllBytes(savePath, bytes);
                Debug.Log("Image saved successfully: " + savePath);
            }
            catch (Exception e){
                Debug.LogError("Error saving image: " + e.Message);
            }
        }
        else{
            Debug.LogError("Failed to encode image to JPG.");
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

二、使用步骤

1. 函数定义

/// <summary>
/// 生成相机照片并保存
/// </summary>
/// <param name="photographyCamera">相机</param>
/// <param name="width">图像宽度</param>
/// <param name="height">图像高度</param>
/// <param name="path">保存路径</param>
/// <param name="imageName">保存图片名字</param>
public void CreateCameraCaptureAndSaveLocal(Camera photographyCamera, int width, int height, string path, string imageName){

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

此函数的目标是使用给定的相机(photographyCamera)生成图像,并将图像保存到指定路径。函数有五个参数,分别是相机、图像宽度、图像高度、保存路径和保存图片的名字

2. 销毁之前的 RenderTexture 和 Texture2D

// 销毁之前的 RenderTexture 和 Texture2D
if (photographyCamera.targetTexture != null){
    RenderTexture.ReleaseTemporary(photographyCamera.targetTexture);
    photographyCamera.targetTexture = null;
    RenderTexture.active = null;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

此段代码确保在生成新图像前,释放之前的渲染纹理和2D纹理,以防止内存泄漏。

3. 创建 RenderTexture

// 创建 RenderTexture
RenderTexture rt = new RenderTexture(width, height, 16, RenderTextureFormat.ARGB32);
photographyCamera.targetTexture = rt;
GL.Clear(true, true, Color.clear); // 清除颜色和深度缓冲区,防止图像叠加
photographyCamera.Render();
RenderTexture.active = rt;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里,我们创建了一个新的RenderTexture(rt),用于保存相机渲染的图像。相机的目标渲染纹理被设置为这个新创建的纹理,然后相机被渲染,将图像绘制到rt上。

4. 创建 Texture2D 并读取图像数据

// 创建 Texture2D 并读取图像数据
Texture2D image = new Texture2D(width, height, TextureFormat.ARGB32, false);
image.ReadPixels(new Rect(0, 0, width, height), 0, 0);
image.Apply();


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这一步,我们创建了一个新的Texture2D(image),并使用ReadPixels方法从RenderTexture中读取像素数据。Apply方法确保纹理被正确应用。

5. 重要步骤:设置 targetTexture 为 null

// 重要:将 targetTexture 设置为 null,以便相机继续渲染到主屏幕
photographyCamera.targetTexture = null;
RenderTexture.active = null;


  • 1
  • 2
  • 3
  • 4
  • 5

这一步非常重要,因为它将相机的目标渲染纹理设置为null,以确保相机继续在主屏幕上渲染。这是在生成图像后必须执行的步骤。

6. 检查保存路径是否有效

// 检查保存路径是否为空或无效
if (string.IsNullOrEmpty(path)){
    Debug.LogError("Invalid save path.");
    return;
}

// 如果文件夹不存在,则创建文件夹
if (!Directory.Exists(path)){
    Directory.CreateDirectory(path);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在这里,我们检查用户提供的保存路径是否为空或无效。如果路径无效,将打印错误消息并提前返回,避免后续操作导致错误。
如果保存路径对应的文件夹不存在,我们会创建这个文件夹。这样可以确保保存图像的目标路径是有效的。

7. 保存图像到本地文件夹

// 保存图像到本地文件夹
byte[] bytes = image.EncodeToJPG();
if (bytes != null){
    string savePath = Path.Combine(path, imageName + ".jpg");

    try{
        File.WriteAllBytes(savePath, bytes);
        Debug.Log("Image saved successfully: " + savePath);
    }
    catch (Exception e){
        Debug.LogError("Error saving image: " + e.Message);
    }

    // 保存文件路径到特定键值
    _rc.WriteKey("takingPhotosPath", savePath);
}
else{
    Debug.LogError("Failed to encode image to JPG.");
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

在这里,我们将Texture2D中的图像数据编码为JPG格式的字节数组,并将其写入指定路径的文件中。如果保存成功,将打印成功消息并将文件路径保存到特定的键值。如果保存失败,将打印错误消息。


总结

通过这个函数,你可以在游戏开发中轻松实现相机捕捉和图像保存的功能,方便用于创建截图、快照等场景。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号