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
随机推荐
- indy10中idtcpclient的使用问题[和大华电子称数据交换]
在实际事务应用中,多次打开server进行大写.其中遇到一些问题,由于时间关系,没有好好整理,虽然问题解决了, 但原因和其他方法没有去进一步测试. 1.每个单元用本地TidTCPClient变量连接s ...
- LaTeX 加粗
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50997822 LaTeX中文本加粗的方 ...
- python 在爬虫中timeout设置超时有什么作用
是为了防止url不可访问,或者响应速度太慢而造成的时间浪费. 比如,你要爬取1000个网站,如果有100个需要30s才能返回数据,你等待他们返回的话就需要3000s了,如果你设置10s超时,那么就能知 ...
- 使用angularjs的$http.post异步提交数据时,服务器接收不了的问题
一,在正常情况下,使用表单的post方法提交数据,默认请求头的Content-Type:application/x-www-form-urlencoded类型, 提交数据格式如下: 二,使用angul ...
- 数据操作的封装--sqlhelper
为了提高软件的灵活性和可维护性,软件的代码须要科学的管理.我们引入了架构这个词.设计模式提醒我们,软件中反复性的代码须要封装起来. 近期在做收费系统时.须要和数据库进行频繁的联系.既然是反复的使用,就 ...
- Hibernate单向关联N-N
单向N-N关联必须使用连接表. Company实体: package com.ydoing.hibernate5; import java.util.HashSet; import java.util ...
- openssl之EVP系列之6---EVP_Encrypt系列函数编程架构及样例
openssl之EVP系列之6---EVP_Encrypt系列函数编程架构及样例 ---依据openssl doc/crypto/EVP_EncryptInit.pod和doc/ssleay. ...
- SVN打tag
SVN打tag是一个非经常常使用的功能,要谈打tag,还得从SVN官方推荐的文件夹结构说起.SVN官方推荐在一个版本号库的根文件夹下先建立trunk.branches.tags这三个文件夹 ...
- Java 零基础跑起第一个程序
Java 零基础跑起第一个程序 一 概述 1 java代码编译 编译后才干在计算机中执行.编译就是把人能看懂的代码转换成机器能看懂的形式 2 java的长处 一次编译.到处执行.由于java代码是在 ...
- js 获取手机浏览器类型,修改css文件的class的值
/*========================================= 函数功能:获取浏览器类型 =========================================*/ ...