目录
1、Unity Android 之 移动端打开相机拍照并保存
1、测试了一下,罗技720会很卡,低配的摄像头以及华为m6不会卡
一、目的:
1、将手机相机画面显示出来
二、参考:
1、Unity Android 之 移动端打开相机拍照并保存
https://jingyan.baidu.com/article/03b2f78cd9fd125ea337ae48.html
①总结:good:电脑端确实能够rawImage显示摄像头画面+手机端无法显示摄像头画面+保存图片不了:最终我对其进行修改了,变成Plane来显示相机画面,手机相机画面可以在Plane上显示,下面是我修改后的
三.操作:一:完成:打开相机+再RawImage中显示
1.注意
- Texture何Material需要空的,否则相机打开了,但是画面是黑色的,
- webCamTexture = new WebCamTexture(webCamName, 1024, 768); 里面的分辨率不能太高,否则会很卡
1.代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 功能:canvas_takePicture 所有函数都在此
/// </summary>
public class MyCanvas_takePicture : MonoBehaviour
{
/// <summary>RawImage:显示摄像头画面</summary>
[Tooltip("RawImage:显示摄像头画面")]
[Header("RawImage:显示摄像头画面")]
public RawImage RawImage_showWebCamTexture;
/// <summary>WebCamTexture:摄像机图片参数</summary>
private WebCamTexture webCamTexture;
// Use this for initialization
void Start()
{
StartCoroutine("CoroutineOpenCamera");
}
/// <summary>
/// 使用协程打开相机函数
/// </summary>
/// <returns></returns>
IEnumerator CoroutineOpenCamera()
{
// 申请相机权限
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
// 判断是否有相机权限
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
// 获取相机设备
WebCamDevice[] webCamDevices = WebCamTexture.devices;
// 判断是否有相机设别
if (webCamDevices != null && webCamDevices.Length > 0)
{
// 把 0 号设备(移动端后置摄像头)名称赋值
string webCamName = webCamDevices[0].name;
//不判断的话,下一次打开后texture一直会空白的
if (webCamTexture==null)
{
// 设置相机渲染宽高,并运行相机
webCamTexture = new WebCamTexture(webCamName, 1024, 768);
if (!webCamTexture.isPlaying)
{
webCamTexture.Play();
rawImage_showWebCamTexture.GetComponent<RawImage>().texture = webCam