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-基础-day1
一.python的介绍 1.python的出现与应用场景 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯 ...
- 最快理解 - IO多路复用:select / poll / epoll 的区别.
目录 第一个解决方案(多线程) 第二个解决方案(select) 第三个解决方案(poll) 最终解决方案(epoll) 客栈遇到的问题 从开始学习编程后,我就想开一个 Hello World 餐厅,由 ...
- Tensorflow 之物体检测
1)安装Protobuf TensorFlow内部使用Protocol Buffers,物体检测需要特别安装一下. # yum info protobuf protobuf-compiler 2.5. ...
- python中的各个包的安装,用pip安装whl文件
在安装之前,一直比较迷惑究竟如何用,安装完后,发现竟然如此简单 首先我一般用的是python27,官网下载python27msi安装window7 64位后,已经自带了Pip 和 easy_insta ...
- eclipse迅速新建main函数
创建类的时候勾选 或者在类的下面敲main,然后alt+/ 就可以了
- 02022_System类的方法练习
1.验证for循环打印数字1-9999所需要使用的时间(毫秒) public class Test { public static void main(String[] args) { long st ...
- 使用动态代理实现dao接口
使用动态代理实现dao接口的实现类 MyBatis允许只声明一个dao接口,而无需写dao实现类的方式实现数据库操作.前提是必须保证Mapper文件中的<mapper>标签的namespa ...
- 《SAS编程与数据挖掘商业案例》学习笔记之十五
继续<SAS编程与数据挖掘商业案例>读书笔记,本次重点:输出控制 主要内容包含:log窗体输出控制.output窗体输出控制.ods输出控制 1.log窗体输出控制 将日志输出到外部文件 ...
- 怎样使windows上的javaWEB项目公布到Centos上
首先在windows上把项目导入到myeclipse或者eclipse(JEE)版本号上. 然后经过调试,没有错误后. 点击项目,然后右键导出(Export...) 然后选择JEE的war格式,这个是 ...
- SharePoint 2013 开启訪问请求
1.通常,我们进入SharePoint 2013网站,假设没权限会提示该网站未被共享,而没有切换账号或者申请訪问,实在是非常流氓:事实上,SharePoint为我们提供了訪问请求页面.可是可能须要手动 ...