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 代码集锦之图片处理的更多相关文章

  1. Unity 代码编译成dll 更新dll实现热更代码

    Unity 代码编译成dll 更新dll实现热更代码 实现流程 代码编译成DLL DLL打包成AssetBundle 加载AssetBundle 加载代码程序集 获取指定类 使用反射赋值 C#代码编译 ...

  2. Unity搭建简单的图片服务器

    具体要实现的目标是:将图片手动拷贝到服务器,然后在Unity中点击按钮将服务器中的图片加载到Unity中. 首先简答解释下 WAMP(Windows + Apache + Mysql + PHP),一 ...

  3. 用qt代码怎样编写图片保存格式[qt4.6]

    用qt代码怎样编写图片保存格式 qt提供了多个保存图片的接口,比较常用的接口如下 bool QPixmap::save ( const QString & fileName, const ch ...

  4. jquery常用代码集锦

    1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({     ajaxSettings : {         contentT ...

  5. 代码: 两列图片瀑布流(一次后台取数据,图片懒加载。下拉后分批显示图片。图片高度未知,当图片onload后才显示容器)

    代码: 两列图片瀑布流(一次后台取数据,无ajax,图片懒加载.下拉后分批显示图片.图片高度未知,当图片onload后才显示容器) [思路]: 图片瀑布流,网上代码有多种实现方式,也有各类插件.没找到 ...

  6. java如何从一段html代码中获取图片的src路径

    java如何从一段html代码中获取图片的src路径 package com.cellstrain.icell.Test; import java.util.ArrayList;import java ...

  7. unity 代码有调整,重新导出 iOS 最烦的就是 覆盖导出后项目不能打开

    unity  代码有调整,重新导出 iOS 最烦的就是 覆盖导出后项目不能打开,原因是 editor 里面的脚本,破坏了 Unity-iPhone.xcodeproj 里面的结构,具体是什么原因,也不 ...

  8. css代码添加背景图片常用代码

    css代码添加背景图片常用代码 1 背景颜色 { font-size: 16px; content: ""; display: block; width: 700px; heigh ...

  9. visual studio tools for unity代码提示快捷键

    visual studio tools for unity代码提示快捷键  ctrl+ shift +q

随机推荐

  1. pandas之cut(),qcut()

    功能:将数据进行离散化 可参见博客:https://blog.csdn.net/missyougoon/article/details/83986511 , 例子简易好懂 1.pd.cut函数有7个参 ...

  2. oracle 创建自增主键

    1.创建表 create table Test_Increase( userid number(10) NOT NULL primary key, /*主键,自动增加*/ username varch ...

  3. 【hdu 6342】Expression in Memories

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把所有的问号都改成'1' 然后会发现只有+0?这种情况 需要把?改成+. 看看这样的0后面的1是不是由问号改过来的就好了.是的话 再 ...

  4. POJ 2137

    水,dp[i][j][k],设为当前为i个牛,在它喜欢的j个位置,而第一个牛在k个位置,很明显了,其实不过是枚举. #include <iostream> #include <cst ...

  5. “System.IO.FileNotFoundException”类型的未经处理的异常在 mscorlib.dll 中发生

    这个错误是我在打包的时候.发现的,由于我移动了我的project的位置(从C盘移动到了D盘),看一下出错的代码: Dim strDB As String = System.Configuration. ...

  6. MySQL官方文档

    http://dev.mysql.com/doc/refman/5.7/en/index.html 没有比这更好的MySQL文档了,省的去买书了

  7. Yii学习笔记之中的一个(安装与基础环境的配置)

    0. 下载yii http://www.yiiframework.com/download/ 1. 訪问 basic 基础文件夹下的 web 文件夹 出现图1 的错误 :    Invalid Con ...

  8. Python 加载数据

    1. numpy data = np.loadtxt('./data/txtdata.csv') ⇒ data 是 numpy.ndarray 类型

  9. k8s 架构浅析

    文章目录 目录 Kubernetes 的电梯间演讲 Kubernetes 的核心层级对象 Kubernetes 的组件架构 Kubernetes 的组件通信协议/接口 Kubernetes 的分层架构 ...

  10. python程序执行原理

    Python程序的执行原理 1. 过程概述 Python先把代码(.py文件)编译成字节码,交给字节码虚拟机,然后解释器一条一条执行字节码指令,从而完成程序的执行. 1.1python先把代码(.py ...