Unity 代码集锦之图片处理
1、将Texture2D保存为jpg
void TestSaveImageToJPG(Texture2D buffer)
{
File.WriteAllBytes("F:/output.jpg", buffer.EncodeToJPG());
}
2、将图片的原始数据保存至txt
void TestSaveImageToTXT(Texture2D buffer)
{
byte[] rawData= buffer.GetRawTextureData();
//byte[] bt = ChangeRGB(rawData).GetRawTextureData();//转换RGB
//char[] ch = Encoding.ASCII.GetChars(bt);//byte[]转为char[]
StreamWriter sw = new StreamWriter(@"F:\\output.txt"); //保存到指定路径
for (int i = 0; i < rawData.Length; i++)
{
sw.Write(rawData[i]);
sw.Write(' ');
}
sw.Flush();
sw.Close();
}
3、转换图片的RGB
Texture2D ChangeRGB(Texture2D source)
{
Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false);
for (int i = 0; i < result.height; ++i)
{
for (int j = 0; j < result.width; ++j)
{
Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
float temp = newColor.r;
newColor.r = newColor.b;
newColor.b = temp;
result.SetPixel(j, i, newColor);
}
}
result.Apply();
return result;
}
对于大多数纹理,更快的是使用GetPixels32,它返回低精度颜色数据,而无需进行昂贵的整数到浮点转换。其中Color数组是Texture2D从左到右,从下到上的像素
Texture2D ChangeRGB(Texture2D source)
{
Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false); for (int i = 0; i < result.height; ++i)
{
for (int j = 0; j < result.width; ++j)
{
result.SetPixel(j, result.height - i, source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height));
}
}
result.Apply();
return result;
}
下面代码和上面代码功能是一样的,都是将图片上下并镜像翻转,但是速度却快了近一倍
Texture2D ChangeRGB(Texture2D source)
{
Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false); Color32[] sourceColor = source.GetPixels32();
Color32[] newColor = new Color32[sourceColor.Length];
for (int i = 0; i < result.height; ++i)
{
for (int j = 0; j < result.width; ++j)
{
newColor[(result.width * (result.height - i - 1)) + j] = sourceColor[(result.width * i) + j];
}
}
result.SetPixels32(newColor);
result.Apply();
return result;
}
或
Texture2D ChangeRGB(Texture2D source)
{
Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false); Color32[] sourceColor = source.GetPixels32();
Color32[] newColor = new Color32[sourceColor.Length];
int currentR = 0;//当前的行
int currentC = 0;//当前的列
for (int i = 0; i < sourceColor.Length; i++)
{
if (i % result.width == 0)
{
currentR = i / result.width;
currentC = 0;
}
else
{
currentC++;
}
newColor[(result.width * (result.height - currentR - 1)) + currentC] = sourceColor[(result.width * currentR) + currentC];
}
result.SetPixels32(newColor);
result.Apply();
return result;
}
附:
//顺时针旋转90度
Texture2D ChangeRGB(Texture2D source)
{
Texture2D result = new Texture2D( source.height, source.width, TextureFormat.RGB24, false); Color32[] sourceColor = source.GetPixels32();
Color32[] newColor = new Color32[sourceColor.Length];
for (int i = 0; i < source.height; ++i)
{
for (int j = 0; j < source.width; ++j)
{
newColor[(source.height * (source.width - 1-j)) + i] = sourceColor[(source.width * i) + j];
}
}
result.SetPixels32(newColor);
result.Apply();
return result;
}
//逆时针旋转90度
Texture2D ChangeRGB(Texture2D source)
{
Texture2D result = new Texture2D( source.height, source.width, TextureFormat.RGB24, false); Color32[] sourceColor = source.GetPixels32();
Color32[] newColor = new Color32[sourceColor.Length];
for (int i = 0; i < source.height; ++i)
{
for (int j = 0; j < source.width; ++j)
{
newColor[(source.height * j) + (source.height-1-i)] = sourceColor[(source.width * i) + j];
}
}
result.SetPixels32(newColor);
result.Apply();
return result;
}
4、将Texture转为Texture2D,参数可传入Texture也可传入WebCamTexture类型的参数,和上述的“1”配合使用,可将摄像头的数据保存为图片
Texture2D TextureToTexture2D(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture); RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply(); RenderTexture.active = currentRT;
RenderTexture.ReleaseTemporary(renderTexture);
return texture2D;
}
5、屏幕截图
Texture2D frameBuffer;
Rect camRect;
frameBuffer = new Texture2D(width, height, TextureFormat.RGB24, false, false);
camRect = new Rect(0, 0, width, height);
frameBuffer.ReadPixels(camRect, 0, 0);
frameBuffer.Apply();
Unity 代码集锦之图片处理的更多相关文章
- Unity 代码编译成dll 更新dll实现热更代码
Unity 代码编译成dll 更新dll实现热更代码 实现流程 代码编译成DLL DLL打包成AssetBundle 加载AssetBundle 加载代码程序集 获取指定类 使用反射赋值 C#代码编译 ...
- Unity搭建简单的图片服务器
具体要实现的目标是:将图片手动拷贝到服务器,然后在Unity中点击按钮将服务器中的图片加载到Unity中. 首先简答解释下 WAMP(Windows + Apache + Mysql + PHP),一 ...
- 用qt代码怎样编写图片保存格式[qt4.6]
用qt代码怎样编写图片保存格式 qt提供了多个保存图片的接口,比较常用的接口如下 bool QPixmap::save ( const QString & fileName, const ch ...
- jquery常用代码集锦
1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({ ajaxSettings : { contentT ...
- 代码: 两列图片瀑布流(一次后台取数据,图片懒加载。下拉后分批显示图片。图片高度未知,当图片onload后才显示容器)
代码: 两列图片瀑布流(一次后台取数据,无ajax,图片懒加载.下拉后分批显示图片.图片高度未知,当图片onload后才显示容器) [思路]: 图片瀑布流,网上代码有多种实现方式,也有各类插件.没找到 ...
- java如何从一段html代码中获取图片的src路径
java如何从一段html代码中获取图片的src路径 package com.cellstrain.icell.Test; import java.util.ArrayList;import java ...
- unity 代码有调整,重新导出 iOS 最烦的就是 覆盖导出后项目不能打开
unity 代码有调整,重新导出 iOS 最烦的就是 覆盖导出后项目不能打开,原因是 editor 里面的脚本,破坏了 Unity-iPhone.xcodeproj 里面的结构,具体是什么原因,也不 ...
- css代码添加背景图片常用代码
css代码添加背景图片常用代码 1 背景颜色 { font-size: 16px; content: ""; display: block; width: 700px; heigh ...
- visual studio tools for unity代码提示快捷键
visual studio tools for unity代码提示快捷键 ctrl+ shift +q
随机推荐
- SpringBoot-CommandLineRunner实现预操作
前提:在使用SpringBoot构建项目时,我们通常需要做一些预先操作(类似开机自启动).而SpringBoot正好提供了一个简单的方式来实现–CommandLineRunner. CommandLi ...
- BA-siemens-PXC模块调试
PXC24模块写地址步骤: 1.制作一根HMI线: 2.通过HMI+232转USB转接卡连接模块: 3.根据已经做好的系统架构表来配置模块: 写模块之前不要忘了格式化模块,主要命令如下: 视频教程:h ...
- BA-冷源系统--一次流量控制方案(转载)
空调水变一次流量控制方案 浙大网新快威科技 黄逸林 一.引言 建筑物中央空调系统的冷冻水一次泵,传统上都采用固定转速水泵.空调水的变一次流量控制系统(VPF:Variable-Primary-Flow ...
- [using_microsoft_infopath_2010]Chapter12 管理监视InfoPath表单服务
本章概要: 1.在SharePoint中心控制台管理InfoPath设置 2.分析监视浏览器表单开考虑潜在性能问题 3.最小化回发数据
- NHibernate之旅(18):初探代码生成工具使用
本节内容 引入 代码生成工具 结语 引入 我们花了大量的篇幅介绍了相关NHibernate的知识.一直都是带着大家手动编写代码,首先创建数据库架构.然后编写持久化类和映射文件,最后编写数据操作方法.測 ...
- 【C++ Primer每日刷】之三 标准库 string 类型
标准库 string 类型 string 类型支持长度可变的字符串.C++ 标准库将负责管理与存储字符相关的内存,以及提供各种实用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与 ...
- Nginx 做系统的前端反向proxy
Nginx是一款很优秀的基于event的webserver.吞吐量大.占用资源少,只是文档就很让人郁闷了,免费的Nginx和收费的Nginx+的文档共用一份,配置完之后才发现免费的Nginx启动某些命 ...
- 【Android 系统开发】使用 Source InSight 阅读 Android 源代码
1. 安装 Source Insight (1) Source Insight 相关资源 安装相关资源 : -- 下载地址 : http://www.sourceinsight.com/down35. ...
- h5-注册成功
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdUAAAGnCAIAAABuMVpqAAAgAElEQVR4nOy9eXQTd57om2R6uvtO3z ...
- 浅谈for循环
for循环 <script> /* ** (1) 是执行代码块之前 ** (2) 运行代码块的条件 ** (3) 需要执行的代码块 ** (4) 代码块执行后执行 ** 执行顺序是(1)( ...