unity 处理图片:截图,下载,保存

1.场景上加入图片处理代码类

using System;

using System.Collections;

using System.IO;

using UnityEngine;

using UnityEngine.Networking;

namespace MyPictrue

{

/// <summary>

/// 处理图片:截图,下载,保存

/// </summary>

public class ProcessImages : MonoBehaviour

{

private static ProcessImages _instance;

private static ProcessImages Instance

{

get

{

if (_instance == null)

{

GameObject obj = new GameObject("ProcessImages");

_instance = obj.AddComponent<ProcessImages>();

DontDestroyOnLoad(obj);

}

return _instance;

}

}

/// <summary>

/// 截图

/// </summary>

/// <returns></returns>

public static void ScreenshotPictrue(GetPicture callBack)

{

Instance.StartCoroutine(Instance.waitTimeGetPicture(callBack));

}

protected IEnumerator waitTimeGetPicture(GetPicture callBack)

{

yield return new WaitForEndOfFrame();

//截图

Texture2D pictrue;

var rect = new Rect(0, 0, Screen.width, Screen.height);

pictrue = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);

pictrue.ReadPixels(rect, 0, 0);

pictrue.Apply();

callBack?.Invoke(pictrue);

}

/// <summary>

/// 下载图片

/// </summary>

/// <param name="url"></param>

/// <param name="onload"></param>

/// <param name="onError"></param>

public static void LoadImage(string url, LoadImageTexture onload, LoadImageError onError)

{

Instance.StartCoroutine(Instance.Load(url, onload, onError));

}

protected IEnumerator Load(string url, LoadImageTexture onload, LoadImageError onError)

{

//加载图片

using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(url))

{

yield return www.SendWebRequest();

if (www.result != UnityWebRequest.Result.ConnectionError)

{

Texture2D tx = DownloadHandlerTexture.GetContent(www);

onload?.Invoke(tx);

}

else

{

onError?.Invoke(www.result);

}

}

}

/// <summary>

/// texture2D压缩到指定宽高

/// </summary>

/// <param name="source">原图</param>

/// <param name="targetWidth"></param>

/// <param name="targetHeight"></param>

/// <returns></returns>

public static Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)

{

RenderTexture rt = RenderTexture.GetTemporary(targetWidth, targetHeight);

RenderTexture.active = rt;

Graphics.Blit(source, rt);

Texture2D resizedTexture = new Texture2D(targetWidth, targetHeight);

resizedTexture.ReadPixels(new Rect(0, 0, targetWidth, targetHeight), 0, 0);

resizedTexture.Apply();

RenderTexture.active = null;

RenderTexture.ReleaseTemporary(rt);

return resizedTexture;

}

/// <summary>

/// texture2D转字符Base64

/// </summary>

/// <param name="texture2D"></param>

/// <returns></returns>

public static string Texture2DToBase64(Texture2D texture2D)

{

byte[] imageData = texture2D.EncodeToJPG();

string baser64 = Convert.ToBase64String(imageData);

return baser64;

}

/// <summary>

/// texture2D保存成JPG

/// </summary>

/// <param name="texture2D"></param>

/// <param name="url">保存地址</param>

public static void SaveJPG(Texture2D texture2D, string url)

{

string names = DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";

byte[] imageData = texture2D.EncodeToJPG();

File.WriteAllBytes(url, imageData);

Debug.Log("保存照片:" + names);

}

/// <summary>

/// texture2D保存成JPG

/// </summary>

/// <param name="texture2D"></param>

/// <param name="url"></param>

public static void SavePNG(Texture2D texture2D, string url)

{

string names = DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";

byte[] imageData = texture2D.EncodeToPNG();

File.WriteAllBytes(url, imageData);

Debug.Log("保存照片:" + names);

}

}

/// <summary>

/// 下载图片委托

/// </summary>

/// <param name="texture2D"></param>

public delegate void LoadImageTexture(Texture2D texture2D);

/// <summary>

/// 截图委托

/// </summary>

/// <param name="texture2D"></param>

public delegate void GetPicture(Texture2D texture2D);

/// <summary>

/// 下载图片错误委托

/// </summary>

/// <param name="error"></param>

public delegate void LoadImageError(UnityWebRequest.Result error);

}

2.调用方法

using MyPictrue;

using UnityEngine;

using UnityEngine.UI;

public class TestPictrue : MonoBehaviour

{

public RawImage raw;

public string URL = "";

// Start is called before the first frame update

void Start()

{

//截图

ProcessImages.ScreenshotPictrue((texture2D) =>

{

raw.texture = texture2D;

});

//下载

ProcessImages.LoadImage(URL, load =>

{

raw.texture = load;

}, error =>

{

Debug.Log(error);

});

}

}

相关推荐
怒放吧德德22 分钟前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆2 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
心之语歌4 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
华仔啊6 小时前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端
ray_liang6 小时前
用六边形架构与整洁架构对比是伪命题?
java·架构
Ray Liang7 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Java水解7 小时前
Java 中间件:Dubbo 服务降级(Mock 机制)
java·后端
SimonKing11 小时前
OpenCode AI辅助编程,不一样的编程思路,不写一行代码
java·后端·程序员
FastBean11 小时前
Jackson View Extension Spring Boot Starter
java·后端
Seven9713 小时前
剑指offer-79、最⻓不含重复字符的⼦字符串
java