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
随机推荐
- 【python正则】工作中常用的python正则代码
工作中常用的一些正则代码: 01.用户名正则 import re # 4到16位(字母,数字,下划线,减号)if re.match(r'^[a-zA-Z0-9_-]{4,16}$', "ab ...
- SpringBoot-CommandLineRunner实现预操作
前提:在使用SpringBoot构建项目时,我们通常需要做一些预先操作(类似开机自启动).而SpringBoot正好提供了一个简单的方式来实现–CommandLineRunner. CommandLi ...
- 分享一些 Java 后端的个人干货
学习 Java 也有了不少时间,入 Java 后台的坑也有了一段时日.这段时间里,听过许多前辈的经验与分享,也看过许多大佬的文章和作品.找了个时间整理和总结了一下我个人到目前为止一路以来的听到看到或者 ...
- 03springMVC注解式控制器开发
注解式控制器开发简介 注解式控制器开发HelloWorld HelloWorld的运行流程 处理器定义 REST简介 URL路径映射 数据绑定 不同的Model有相同的属性的处理 静态资源的处理 1 ...
- 数据库-mongodb-高级查询表达式
不等于 $nq 1 2 SQL : select * where != 3 NOSQL : db.goods.find({cat_id:{$ne:3 }},{cat_id:1,id:0}); NOS ...
- 工具-VS CODE安装
在Linux下的安装 1.下载tar.gz文件包, 2.要注意加一条命令,这样在任何目录下就可以使用code .直接启动应用程序了 1 sudo ln -s /path/to/vscode/Code ...
- BA-siemens-insight在win7下如何配置opc接口
一.运行环境:win7(OPC接口在win_xp下配置需安装插件,不好意思没搞定,现在只有win7系统32位下的教程了) 由于OPC(OLE for Process Control)建立在Micros ...
- etymology-R
1)vor = to eat devour vt. 狼吞虎咽地吃光: 吞没,毁灭: 目不转睛地看[de-向下+vour-吃] voracity n.贪食,贪婪.拉丁词根vor-,vorac-表示吞食 ...
- shell文本过滤编程(十一):paste命令
[版权声明:转载请保留出处:blog.csdn.net/gentleliu. Mail:shallnew at 163 dot com] 从字面上能够看出.paste命令和cut命令功能相反,cut命 ...
- 0x21 剪枝
这一章真是心态崩,剪枝太玄学啦,特别是那个搜索顺序我靠真的... poj1011 枚举答案,搜索记录当前到第几根木棒. 剪枝:1.从大到小排序 2.排除等效,这个感觉还行,就是木棒按大小顺序进去,去除 ...