Unity 手机拍照以及相关问题

说明:用的5.6 所以也就是不可能有webplayer,以下都是在android手机上测试~

一:用Unity 自带WebCamTexture实现拍照功能

    1:首先搭建一个简易场景

                一个button按下  打开相机

                

           相机中有切换摄像头按钮 拍照按钮 预览图 

                

  2:编码

            (1):这里说明下,我看了很多帖子,都是说要先请求授权,然后当授权成功之后在打开摄像机,我在这里试了很多次,其实不用如此麻烦,比如你像下面这么写

public IEnumerator start()  
    {  
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);  
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))  
        {  
            WebCamDevice[] devices = WebCamTexture.devices;  
            deviceName= devices[0].name;  
            tex=new WebCamTexture(deviceName,300,300,12);  
            tex.Play();  
        }  
    }  
其实没有必要,这两句其实没有啥效果,第二行代码不管怎么样都是true
Application.RequestUserAuthorization(UserAuthorization.WebCam); 
Application.HasUserAuthorization(UserAuthorization.WebCam)

这是官方文档上面的说明,如果不信,你可以把上面的代码tex.play(),注释,此时发布安装 根本不会有任何弹框显示是否要你授权,      或者你把前面的两句注释,也会显示授权,真正的授权就是摄像机play()

    

    可以自己试试,建议每次安装的时候卸载前一次的安装,或者改下包名

  (2):PC和手机上的不同点

        在pc上面,用来显示摄像头拍摄的照片photoImg,不用做任何改变

        

    但是这样发布到手机上发现是歪了90度,这个时候要调节下。

        

当我们按下“切换”按钮的时候会发现图片反了,这个时候要在图片切换的地方添加一句旋转函数

       photoImage.transform.Rotate(new Vector3(180, 0, 180));

这一点很麻烦,所以用来渲染相机拍到的photoImg最好是个正方形,然后start方法时候判断当前平台是否是在手机上,如果是就旋转就是90度,然后再切换相机的时候不要添加旋转函数(下面代码没加(因为我的photoImg不是正方形),只是提供一种思路)

    (3):代码(注意在pc和手机差别)

        

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
//拍照
public class TakePhotos : MonoBehaviour
{
    public Button startPhotoBtn; //开始拍照
    public Button getPhotoBtn;    //拍照
    public Button changeBtn;    //切换相机
    public RawImage photoImage;    //相机画面
    public RawImage photoPreview;  //预览图

    private string photoPath;  //相机存贮位置

    private WebCamTexture webcam1, webcam2; //这里当作两个摄像机处理
    //   private bool isAndroid;
    void Start()
    {
        startPhotoBtn.onClick.AddListener(InitWebCam);
        getPhotoBtn.onClick.AddListener(GetPhoto);
        changeBtn.onClick.AddListener(ChangeCamera);
        //photoPath = Application.dataPath + "/Texture/";
        //if (!Directory.Exists(photoPath))
        //{ Directory.CreateDirectory(photoPath); }
    }

    private void InitWebCam()
    {
        WebCamDevice[] devices = WebCamTexture.devices;

        int webCamLength = devices.Length;
        if (webCamLength <= 0)
        {
            Debug.Log("没有获取摄像机设备");
            return;
        }
        else if (webCamLength == 1)
        {
            //所选相机可能不支持请求的参数指定的宽度,高度和帧速率。在这种情况下,将使用最接近的可用值
            //根据纹理图片拍摄大小
            Debug.Log("有一个摄像机设备");
            webcam1 = new WebCamTexture(WebCamTexture.devices[0].name,
                (int)photoImage.rectTransform.rect.width,
                (int)photoImage.rectTransform.rect.height, 12);
        }
        else if (webCamLength == 2)
        {
            Debug.Log("有两个摄像机设备");
            webcam1 = new WebCamTexture(WebCamTexture.devices[0].name,
           (int)photoImage.rectTransform.rect.width,
           (int)photoImage.rectTransform.rect.height, 12);
            webcam2 = new WebCamTexture(WebCamTexture.devices[1].name,
               (int)photoImage.rectTransform.rect.width,
               (int)photoImage.rectTransform.rect.height, 12);
        }
        if (webcam1 != null)  //当获取摄像机不为空
        {
            OpenWebCam1();
        }
    }

    /// <summary>
    /// 打开摄像头1
    /// </summary>
    private void OpenWebCam1()
    {
        //当webcam2不为空且在运行的时候
        if (webcam2 != null && webcam2.isPlaying)
        {
            webcam2.Stop();
        }
        webcam1.Play();
        photoImage.texture = webcam1;
    }
    /// <summary>
    /// 打开摄像头2
    /// </summary>
    private void OpenWebCam2()
    {
        if (webcam1 != null && webcam1.isPlaying)
        {
            webcam1.Stop();
        }
        webcam2.Play();
        photoImage.texture = webcam2;
    }
    /// <summary>
    /// 获取正在运行的摄像头
    /// </summary>
    /// <returns></returns>
    private WebCamTexture GetCurrentWebCam()
    {
        return webcam1.isPlaying ? webcam1 : webcam2;
    }
    /// <summary>
    /// 切换相机
    /// </summary>
    private void ChangeCamera()
    {
        //如果web2为空,即只有一个摄像头 返回
        if (webcam2 == null) return;
        //得到另一个相机
        WebCamTexture otherWebCam = GetCurrentWebCam() == webcam1 ? webcam2 : webcam1;
        if (otherWebCam == webcam1)
        {
            OpenWebCam1();
        }
        else
        {
            OpenWebCam2();
        }
        photoImage.transform.Rotate(new Vector3(180, 0, 180));
    }

    /// <summary>
    /// 拍照
    /// </summary>
    private void GetPhoto()
    {
        StartCoroutine(IEGetPhoto());
    }
    private IEnumerator IEGetPhoto()
    {
        //获取当前的摄像头
        WebCamTexture currentWebCam = GetCurrentWebCam();
        currentWebCam.Pause();
        //必须加上这一句yield return,不然没办法ReadPixels
        yield return new WaitForEndOfFrame();
        //根据photoImage   new一个texture2D
        Texture2D texture2D =
            new Texture2D((int)photoImage.rectTransform.rect.width, (int)photoImage.rectTransform.rect.height, TextureFormat.ARGB32, false);
        //读取像素
        texture2D.ReadPixels(new Rect(0, Screen.height - photoImage.rectTransform.rect.height, photoImage.rectTransform.rect.width, photoImage.rectTransform.rect.height), 0, 0, false);

        texture2D.Apply();

        yield return new WaitForEndOfFrame();
        photoPreview.texture = texture2D;  //预览图
        currentWebCam.Play();
        //IO写入
        //byte[] bs = texture2D.EncodeToPNG();
        // string name = DateTime.Now.ToString("yyyyMMdd_HHmmss")+".png";
        // File.WriteAllBytes(photoPath+name, bs);
    }

}

    上面注释的地方是在pc平台可以写入图片到磁盘中

在手机上拍摄写入到相册目前没有成功 按照网上的做法 连接 可行 但是不会立即出现在相册

所有可能还是要自己写插件来实现

    

猜你喜欢

转载自blog.csdn.net/K20132014/article/details/79846040