zoukankan      html  css  js  c++  java
  • 结合NGUI做的手机拍照(可自定义相框)

    原地址:http://www.unity蛮牛.com/thread-18220-1-1.html

    在次此之前我们先要了解一下下面的我要讲的几个内容:

    一、为什么要用NGUI,因为NGUI的可以做屏幕自适应,在各大不同手机分辨率的屏幕上想要实现基本的显示而且不乱象的效果最后还又能快速开发出来,那么无疑还是NGUI比较的好。
    二、既然是涉及到手机拍照,那么无疑使可以调用前置或者后置的摄像头都可以进行拍照咯。


    三、实际上我做的所谓的拍照时利用了手机的截图功能,只是我会在最后保存路径上会将它保存到手机的相册上(这是个难点),
    论坛里看到过一篇转自雨松MOMO的有关安卓上打开摄像头拍照和打开本地相册的帖子的朋友一定会问为什么不使用这个方法来调用摄像机拍照呢。这个帖子我也大致的浏览了一下(MOMO的工程文件我还在网上找了下载了发到手机上不知道为啥不能运行成功),并不能满足我要做一个自己定义我的摄像机里面要有自定义的素材,比如拍一个大头贴,或者和选择一个明星照片作为背景之类的,总之肯定是不能调用手机自带的摄像机软件来拍照,因为那个软件是手机自带的根本无法用程序来控制所以也就做不到这点。那么我们只能是采用另外一种办法,摄像头无疑还是可以打开的,因为这个是可以通过调用硬件的驱动程序来实现。好在unity有这个的代码类的接口程序给我调用。然后再采用截图的方法实现拍照,这样在有NGUI的强大功能下,我们想实现什么自定义背景或者相框甚至是一个模型都是可控的。

    前面唠叨了拿了多,开始我正式的讲解把。首先,还是一个NGUI的自适应问题,相信很多大婶朋友都是了如指掌了,但是我还是要在这里贴出一些我的做法和想法,大婶朋友如果发我的做法有不对的地方还望你不吝指出,在下感激不尽

    一、要做到自适应无非就是两个NGUI的脚本使用,一个是
    UIAnchor,另一个是UIStretch。作为界面显示使用2DUIRoot并做如图的设置


    针对图片直接将UIStretch脱到图片上,然后再如图


    。针对按钮就要两个同时使用,
    按钮的大小和位置都要保证自适应。如果你不是使用NGUI的图片按钮控件,而是自己做的一个按钮,比如都在就在一个物体上那么还要做如下设置 这样就能保证按钮的碰撞体随着缩放而自动调整,不至于出现按钮图片自适应但是碰撞体没有自适应。


    二、下面是怎么调用摄像头的问题。首先我们要如下的代码

    [C#] 纯文本查看 复制代码
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    public WebCamTexture cameraTexture; 
        public string cameraName=""
        private bool isPlay = false;
        public UITexture uiCameraTexture;[/size][size=3]    float zoomRate = 2;
        // Use this for initialization  
        void Start() 
        {
            StartCoroutine(openCamera(0));  //在程序运行的时候就打开摄像头
        
       
        // Update is called once per frame  
        void Update() 
        {
            if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home))//手机上按到home或者返回键退出程序
            {
     
                Application.Quit();
     
            }
        
       
        IEnumerator openCamera(int whichOne)  //打开摄像头,参数0表示后置摄像头,1表示前置摄像头
        
            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);  //获取手机的权限
            if (Application.HasUserAuthorization(UserAuthorization.WebCam)) 
            
                //可以通过下面的这个数组的索引来选择调用手机上的前置或者后置摄像头,另外这个可以自动对焦(这是个非常有用的功能)
                WebCamDevice[] devices = WebCamTexture.devices;
                if (devices.Length <= whichOne)
                {
                    cameraName = devices[0].name;
                }
                else
                {
                    cameraName = devices[whichOne].name;
                }
          
                cameraTexture = new WebCamTexture(cameraName,Screen.height,Screen.width,15); 
                cameraTexture.Play();
                isPlay = true
            
        
       
        void OnGUI() 
        
            if (isPlay) 
            {
                uiCameraTexture.mainTexture = cameraTexture;
                NGUIDebug.Log(cameraName);[/size][size=3]            uiCameraTexture.width = cameraTexture.width ;
                uiCameraTexture.height = cameraTexture.height;
                uiCameraTexture.transform.localScale = new Vector3(zoomRate, zoomRate, zoomRate);
            
        }[/size]
    [size=3]  //双击拍照键就可以实现切换后置摄像头
        void ImgBtnTakePictureOnDoubleclik()
        {
            //NGUIDebug.Log("ssss");
            cameraTexture.Stop();
            StopAllCoroutines();
            StartCoroutine(openCamera(1));
        }




    代码都有比较详细的说明,在这里我主要讲一下这段代码
    uiCameraTexture.width = cameraTexture.width ; 
    uiCameraTexture.height = cameraTexture.height;
    uiCameraTexture.transform.localScale = new Vector3(zoomRate, zoomRate, zoomRate);
    经过我的多次真机测试发现摄像头获取的图像的分辨率是和摄像头设备硬件直接相关的,我用红米手机的前置摄像头测试的时候得到的图像是600*800然而使用电脑上的微软的摄像头得到的是480*600,所以如果只是单纯的这样
    uiCameraTexture.width = cameraTexture.width ; 
    uiCameraTexture.height = cameraTexture.height;赋值那么得到的图像势必不是适应屏幕的,就一定会是小了。所以就有了后面那句等比例放大的代码。其实我本来前面还有一句代码,是根据手机的分辨率来判断我应该放大多大

    [C#] 纯文本查看 复制代码
    1
    2
    3
    float heightRate = Screen.height / cameraTexture.height;
               float widthRate = Screen.width / cameraTexture.width;
               zoomRate = heightRate > widthRate ? heightRate : widthRate;


    发现得到的ZoomRate都比较大(红米测试机上得到的是61),所以得到的图像基本上都是放大了几十倍的,最后是测试得到放到2倍是最理想的。其实这个给了我最后一个想法就是可以实现手指放大图像或者缩小来摄像类似于真正的手机上的照相软件那样。最后还要添加一个UITexture控件到scene上就OK啦。那么到这里基本上框架已经搭建好了而且一些设备的细节问题也得到了妥善的解决。最后当然是重头戏——怎么拍照。

    三、怎么样实现不同手机上(安卓和苹果)的截图保存到手机上相册功能
    想要实现不同的手机上都可以保存到相册中,这个无疑要借助到第三方的插件。插件的地址在我文章的后面可以下载。这里我主要讲一下腰注意的事项,如图

    其实就是发布的时候获取手机的SD卡读取权限在图中的Writer Access里面选择External(SDCard)这一项。
    然后修改之前的代码如下

    [C#] 纯文本查看 复制代码
    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    022
    023
    024
    025
    026
    027
    028
    029
    030
    031
    032
    033
    034
    035
    036
    037
    038
    039
    040
    041
    042
    043
    044
    045
    046
    047
    048
    049
    050
    051
    052
    053
    054
    055
    056
    057
    058
    059
    060
    061
    062
    063
    064
    065
    066
    067
    068
    069
    070
    071
    072
    073
    074
    075
    076
    077
    078
    079
    080
    081
    082
    083
    084
    085
    086
    087
    088
    089
    090
    091
    092
    093
    094
    095
    096
    097
    098
    099
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    public AudioSource shootSnd;
        public UITexture uiCameraTexture;
        public UISprite ImgBtnTakePictureUISprite;
        public BoxCollider ImgBtnTakePictureBoxCollider;
        WebCamTexture cameraTexture;
        float zoomRate;
        string cameraName = "";
        bool isPlay = false;
        bool saved = false;
         
        // Use this for initialization  
        void Start() 
        {
            StartCoroutine(openCamera(0));
            zoomRate = 1;
            ScreenshotManager.ScreenshotFinishedSaving += ScreenshotSaved;
        }
     
        void ScreenshotSaved()
        {
            saved = true;
        
       
        // Update is called once per frame  
        void Update() 
        {
            if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home))
            {
     
                Application.Quit();
     
            }
        
       
        IEnumerator openCamera(int whichOne) 
        
            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); 
            if (Application.HasUserAuthorization(UserAuthorization.WebCam)) 
            
                //可以通过下面的这个数组的索引来选择调用手机上的前置或者后置摄像头,另外这个可以自动对焦
                WebCamDevice[] devices = WebCamTexture.devices;
                if (devices.Length <= whichOne)
                {
                    cameraName = devices[0].name;
                }
                else
                {
                    cameraName = devices[whichOne].name;
                    if (whichOne > 0)
                    {
                        Quaternion temp = uiCameraTexture.transform.localRotation;
                        temp.eulerAngles = new Vector3(0, 0, 90);
                        uiCameraTexture.transform.localRotation = temp;
                    }
                }
                cameraTexture = new WebCamTexture(cameraName, Screen.width, Screen.height,15);
                cameraTexture.anisoLevel = 9;
                float heightRate = Screen.height / cameraTexture.height;
                float widthRate = Screen.width / cameraTexture.width;
                //Debug.Log(heightRate.ToString());
                //Debug.Log(widthRate.ToString());
                zoomRate = heightRate > widthRate ? heightRate : widthRate;
                if (zoomRate > 2)
                {
                    zoomRate = 2;
                }
                cameraTexture.Play();
                 
                isPlay = true
            
        
       
        void OnGUI() 
        
            if (isPlay) 
            {
                uiCameraTexture.mainTexture = cameraTexture;
                //NGUIDebug.Log(" cameraTexture.height:" + cameraTexture.height.ToString() + "cameraTexture." + cameraTexture.width.ToString());
                uiCameraTexture.width = cameraTexture.width ;
                uiCameraTexture.height = cameraTexture.height;
                uiCameraTexture.transform.localScale = new Vector3(zoomRate, zoomRate, zoomRate);
     
                 
            }
            if (saved)
            {
    //这个逻辑代码的意思就是截图的时候可以不用拍到那个按钮
                ImgBtnTakePictureUISprite.enabled = true;
                ImgBtnTakePictureBoxCollider.enabled = true;
                cameraTexture.Play();
                saved = false;
            }
        }
        void ImgBtnTakePictureOnclik()
        {
            cameraTexture.Pause();
            ImgBtnTakePictureUISprite.enabled = false;
            ImgBtnTakePictureBoxCollider.enabled = false;
            if (!shootSnd.audio.isPlaying)
                shootSnd.audio.Play();
            StartCoroutine(ScreenshotManager.Save("NinthRoomShotAR", "NinthRoom", true));
        }
        void ImgBtnTakePictureOnDoubleclik()
        {
            saved = true;
            cameraTexture.Stop();
            StopAllCoroutines();
            StartCoroutine(openCamera(1));
        }




    好了大功告成,一个有模有样的拍照软件就做成了,你可以自主选择相框叠加到里面去,这个都可以根据自己的爱好随意添加。帖子最后我会给出工程文件和插件的下载地址,谢谢你的耐心阅读!如有什么好的建议和任何疑问欢迎回复跟帖!

  • 相关阅读:
    你有认真了解过自己的“Java对象”吗? 渣男
    布隆过滤器,你也可以处理十几亿的大数据
    阻塞队列——手写生产者消费者模式、线程池原理面试题真正的答案
    Java集合面试题汇总篇
    Github 骚操作
    责任链模式——更灵活的if else
    时间复杂度到底怎么算
    创造DotNet Core轻量级框架【二】
    创造DotNet Core轻量级框架【一】
    小胖李的面试之旅(二)
  • 原文地址:https://www.cnblogs.com/123ing/p/3918673.html
Copyright © 2011-2022 走看看