unity调用摄像头拍照
2017-06-15 10:28
225 查看
我的unity调用摄像头拍照
因为做的是pc端的,所以不知道手机上效果是怎样的,据说代码是完全相同的。
废话不说,上代码,
打开摄像头代码:
/// <summary>
/// Opens the camera
/// </summary>
/// <returns>The camera.</returns>
IEnumerator OpenCamera()
{
yield return Application.RequestUserAuthorization (UserAuthorization.WebCam);
if (Application.HasUserAuthorization (UserAuthorization.WebCam)) {
WebCamDevice[] devices = WebCamTexture.devices;
webCam = new WebCamTexture (devices [0].name, Screen.width, Screen.height, 12);
image_userIcon.texture = webCam;
webCam.Play ();
}
}保存图片代码如下:
/// <summary>
/// Gets the texture2d.
/// </summary>
/// <returns>The texture2d.</returns>
IEnumerator GetTexture2d()
{
yield return new WaitForEndOfFrame();
RectTransform rt = image_userIcon.GetComponent<RectTransform> ();
Vector2 v2Min = new Vector2( rt.anchorMin.x * Screen.width,rt.anchorMin.y * Screen.height);
Vector2 v2Max = new Vector2( rt.anchorMax.x * Screen.width,rt.anchorMax.y * Screen.height);
Texture2D t = new Texture2D((int)(v2Max.x-v2Min.x),(int)(v2Max.y-v2Min.y));//要保存图片的大小
//截取的区域
t.ReadPixels(new Rect (v2Min.x,v2Min.y,(int)(v2Max.x-v2Min.x),(int)(v2Max.y-v2Min.y)),0,0,false);
t.Apply();
image_userIcon.texture = t;
//把图片数据转换为byte数组
byte[] byt = t.EncodeToPNG();
//然后保存为图片
File.WriteAllBytes(Application.dataPath + "/StreamingAssets/shexiang/" + Time.time + ".jpg", byt);
webCam.Pause ();
}运行完这段效果大概是这样滴:
相关文章推荐
- Unity如何调用安卓手机摄像头实现拍照和录像
- Unity如何调用安卓手机摄像头实现拍照和录像
- Unity如何调用安卓手机摄像头实现拍照和录像
- unity调用摄像头进行拍照和保存照片
- unity调用摄像头拍照
- 怎样让HTML5调用手机摄像头拍照——实践就是一切
- Android移动开发-调用摄像头进行拍照的实现
- FootStep 40、如何调用S60的系统摄像头功能进行拍照与摄像?
- Android 调用摄像头功能【拍照与视频】
- web手机页面调用摄像头拍照以及上传
- 【MediaKit】WPF项目中 调用摄像头拍照的开发包
- Wp7: 调用摄像头拍照并上传图片(完整版)
- 调用摄像头拍照并显示
- html5调用摄像头实现拍照
- unity调用摄像头
- HTML5调用手机摄像头拍照
- OpenCv调用摄像头拍照代码
- Android调用摄像头拍照和从相册中选择(上传、更换头像)
- 调用手机摄像头拍照和从图库中选取照片以及得到照片后的备份
- 调用Android系统摄像头进行拍照以及从图库选择图片