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();

  

  

转载于:https://www.cnblogs.com/Jason-c/p/10812805.html

Unity 代码集锦之图片处理相关推荐

  1. unity代码更换ui图片_Unity3d 低分辨率UI素材换高分辨率素材填坑笔记

    迷糊 RectTransform PosX.PosY.Left .Right . Top .Bottom 以及 AchorMax/AnchorMin 的,开卷有益哟~ 背景 笔者开发的(PC)APP ...

  2. unity代码更换ui图片_关于Unity UI的动静图集分离,你需要了解这些!

    这是第101篇UWA技术知识分享的推送!(此处应有掌声)随着大家在UWA平台上的发言愈加活跃,我们也源源不断地收到了许多技术大咖的热心回复和真知灼见. UWA 问答社区:answer.uwa4d.co ...

  3. unity代码更换ui图片_Unity UI图片的一种优化方式

    项目中使用的不少UI图片是轴对称的,包括:1)横轴对称或纵轴对称 2)同时满足横轴对称和纵轴对称 图1:竖轴对称图片 可以使用镜像效果来节省图片资源.第一种情况可以省下完整资源的一半,第二种情况则可以 ...

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

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

  5. Unity代码调用网络摄像头WebCamTexture

    Unity代码调用网络摄像头WebCamTexture 初始代码 后续功能&代码更新 注意事项 初始代码 编辑器模式或Android环境和实机测试都没有问题. using System.Col ...

  6. Unity UGUI 数字使用图片显示-BMFont

    Unity UGUI 数字使用图片显示-BMFont BMFont 之前使用Cocos引擎的时候需要用到艺术字也就是将数字使用美术给的图片来代替显示,也就是要做一个新字体专门用来显示艺术字,当时用的是 ...

  7. [Unity]利用Mesh在Unity中绘制扇形图片

    背景 最近碰到个功能, 要画一个扇形图案, 如下图: 美术原图: 需求是这个图形跟随角色, 在角色背后, 并且每个角色的扇形角度可能不同. So, NGUI和UGUI很好用的FilledType是用不 ...

  8. html代码里面换图片不显示不出来了,微擎 Ueditor 百度编辑器 替换图片不显示问题...

    问题: 最近在使用微擎添加公众号平台文章时,上传图片可以显示,但是点击 html(编辑器的按钮,可以显示html代码) 时出现图片不显示问题. 我是用的135编辑器挑选的图文模板,我替换完模板中的图片 ...

  9. CSDN博客如何在有序序列中缩进代码段或图片

    今天没写什么新的内容,主要是调整了博客分类.博客标题格式.博客摘要等内容,一时兴起决定分享一下如何在有序序列中缩进代码段或图片. 先看一看不会缩进代码段的效果: 歪比巴卜 歪比巴卜 歪比歪比 歪比歪比 ...

最新文章

  1. @计算机专业大学生,毕业后如何明确自己的职业方向,该走那条路?
  2. Android 实现手写板技术
  3. c++中向任意目录下写文件
  4. oracle建表代码,oracle建表脚本当中使用默认值 (转)
  5. linux sz 大于4g,ubuntu 使用sz命令传输windows大于4G的文件
  6. html跳转到添加qq好友,浏览器点击链接跳转qq添加好友的实现方式
  7. 对比三菱, 横河PLC快速入门 第一章
  8. 【雅思】【大作文】【审题作业】关于同不同意的审题作业(重点)
  9. 商用密码应用解决方案编写指南
  10. 学习利用ce修改游戏生命参数
  11. selenium 浏览器打开新标签页
  12. mysql工作日_mysql自定义函数计算时间段内的工作日(支持跨年)
  13. 【179期】这些最常用的Linux命令都不会,你怎么敢去面试?
  14. 1000道Python编程题
  15. 1024 的那天,我这个三线的程序员是这样度过的
  16. mysql与其他连接需要的框架_VS2013与MySql建立连接;您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库 EF6使用Mysql的技巧...
  17. wow mysql dbc_魔兽SF单机中用MyDbcEditor修改Spell.dbc教程
  18. EBS WebADI:Web ADI开发@Excel LOV
  19. nanopore测序技术专题(二):一些典型应用
  20. 怎样设置计算机远程桌面,电脑如何设置远程连接,手把手教你如何远程

热门文章

  1. 单细胞数据初步处理 | drop-seq | QC | 质控 | 正则化 normalization
  2. Docker 17.12.0 发布
  3. 大型网站运维工程师的职责和前景
  4. 在Redhat中添加微软雅黑字体
  5. Useful websites for library
  6. 【转】图形学基础之透视校正插值
  7. OpenGL3.x,4.x中使用FreeImage显示图片的BUG-黑色,或颜色分量顺序错乱
  8. rhel4 x86_64 php5.2.17 make安装 支持mysqli
  9. CNCF推出云原生网络功能(CNF)Testbed
  10. 10月份个人技术指标