unity android保存摄像头(Vuforia AR)拍照,载入UGUI image组件
来源:互联网 发布:淘宝企业开店 编辑:程序博客网 时间:2024/05/22 20:24
为考虑pc/android/ios兼容性,另外也避免访问系统相册不同系统不一致的麻烦,
我们采用 application.persistentDataPath 来进行图像读写访问。
步骤1. 保存图像
cameraTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
UnityEngine.Rect rect = new UnityEngine.Rect (0, 0, cameraTexture.width, cameraTexture.height);
cameraTexture.ReadPixels (rect, 0, 0, true);
cameraTexture.Apply ();
//把图片数据转换为byte数组
byte[] byt = cameraTexture.EncodeToPNG();
//然后保存为图片
System.DateTime now = System.DateTime.Now;
string timeStr = now.Year.ToString()+now.Month.ToString()+now.Day.ToString()+
now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString();
File.WriteAllBytes(Application.persistentDataPath + "/CutImage.png", byt);
步骤2.读取图像
有两种方法,一种是SystemIO,一种是www方式,这里采用了SystemIO方式,
string filePath = Application.persistentDataPath + "/CutImage.png";
//创建文件读取流
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
//创建文件长度缓冲区
byte[] bytes = new byte[fileStream.Length];
//读取文件
fileStream.Read(bytes, 0, (int)fileStream.Length);
//释放文件读取流
fileStream.Close();
fileStream.Dispose();
fileStream = null;
步骤3.将图像转成Texture2D,然后转成Sprite贴到Image组件上
//创建Texture
int width=800;
int height=640;
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(bytes);
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
Background.sprite = sprite;
收工,在PC,android都确认通过
- unity android保存摄像头(Vuforia AR)拍照,载入UGUI image组件
- vuforia + unity “Hello AR”
- 基于 vuforia + unity + android studio 的AR开发(1)
- 基于 vuforia + unity + android studio 的AR开发(2)
- AR 学习记录 unity+vuforia+ android 初次搭建
- Vuforia SDK---- AR开发vuforia实现拍照截屏功能
- Vuforia unity开发摄像头问题
- Vuforia unity开发摄像头问题
- unity调用摄像头进行拍照和保存照片
- [UGUI][Unity插件]载入框插件载入
- Vuforia Unity Camera Image Access
- Vuforia Unity Camera Image Access
- unity调用摄像头拍照
- unity调用摄像头拍照
- unity+vuforia(AR)(Cloud Recognition)
- unity+vuforia(AR)(Virtual Buttons)
- unity使用vuforia实现模型AR效果
- Vuforia+Unity AR场景播放音频控制
- 【leetcode】c++ map按照value排序
- 初学C语言,有什么好书推荐?
- PythonGui->菜单栏
- JavaScript中的事件
- linux 部署nginx
- unity android保存摄像头(Vuforia AR)拍照,载入UGUI image组件
- 【leetcode】167. Two Sum II - Input array is sorted
- SGU 210 Beloved Sons (KM)
- Java NIO系列教程(三) Buffer
- leetcode 347 Top K Frequent Elements C++
- [USACO]3.1.5 Contact
- 希尔排序(比较难)
- 【京东校招笔试】采购单
- SQLAlchemy_定义(一对一/一对多/多对多)关系