unity的拍照截图,百度智能云实现unity无绿幕抠图

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using UnityEngine;
using LitJson;
using UnityEngine.UI;
using System.Text.RegularExpressions;
using UnityEditor;

public class OpenCamera : MonoBehaviour
{
    public Image img;  //显示处理完的图片的Image组件
    public string imageName; //需要处理的图片的名字例:1.png

    private void Start()
    {
        StartCoroutine(Open_Camera());
        saveImage.onClick.AddListener(SaveImage);
    }

    // 人像分割 POST
    public static String TOKEN= "输入你自己的token";
    // 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
    private static String clientId= "输入你自己的API Key";
    // 百度云中开通对应服务应用的 Secret Key
    private static String clientSecret= "输入你自己的Secret Key";

    public String getAccessToken()
    {
        String authHost = "https://aip.baidubce.com/oauth/2.0/token";
        HttpClient client = new HttpClient();
        List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>();
        paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
        paraList.Add(new KeyValuePair<string, string>("client_id", clientId));
        paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret));

        HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
        String result = response.Content.ReadAsStringAsync().Result;
        return result;
    }

    public string body_seg()
    {
        //提取鉴权
        string token = getAccessToken().Split(',')[3].Split(':')[1];
        string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg?access_token=" + token;
        Encoding encoding = Encoding.Default;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
        request.Method = "post";
        request.KeepAlive = true;
        // 图片的base64编码
        string base64 = getFileBase64(Application.streamingAssetsPath + "/my"+ "/" + imageName+ ".jpg");
        String str = "image=" + HttpUtility.UrlEncode(base64);
        byte[] buffer = encoding.GetBytes(str);
        request.ContentLength = buffer.Length;
        request.GetRequestStream().Write(buffer, 0, buffer.Length);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
        string result = reader.ReadToEnd();
        //     Debug.Log(result);
        string newbase64 = json(result);

        return newbase64;
    }

    public string json(string str)
    {
        JsonData jd;
        jd = JsonMapper.ToObject(str);
        return jd["foreground"].ToString();

    }

    public static String getFileBase64(String fileName)
    {
        FileStream filestream = new FileStream(fileName, FileMode.Open);
        byte[] arr = new byte[filestream.Length];
        filestream.Read(arr, 0, (int)filestream.Length);
        string baser64 = Convert.ToBase64String(arr);
        filestream.Close();
        return baser64;
    }

    /// <summary>
    /// 转换成图片
    /// </summary>
    /// <param name="imgComponent"></param>
    /// <param name="base64"></param>
    public void Base64ToImg(Image imgComponent, string base64)
    {
        byte[] bytes = Convert.FromBase64String(base64);
        Texture2D tex2D = new Texture2D(100, 100);
        tex2D.LoadImage(bytes);    
        File.WriteAllBytes(Application.streamingAssetsPath + "/" + TimeIcon() + ".jpg", bytes);
        Sprite s = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));
        imgComponent.sprite = s;
        Resources.UnloadUnusedAssets();
    }
  
   
    string TimeIcon()
    {
        var year = DateTime.Now.Year;
        var mouth = DateTime.Now.Month;
        var day = DateTime.Now.Day;
        var hour = DateTime.Now.Hour;
        var minute = DateTime.Now.Minute;
        var second = DateTime.Now.Second;
        string picName = year + "-" + mouth + "-" + day + "-" + hour + "-" + minute + "-" + second;
        return picName;
    }







    /// <summary>
    /// 拍照后截图
    /// </summary>

    //摄像头图像类,继承自texture
    WebCamTexture tex;

    public RawImage ma;
    public Button saveImage;

    public RawImage bgimage_02;
    // public RawImage bgimage_03;
    int i;

 

 
    IEnumerator Open_Camera()
    {
        //等待用户允许访问
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        //如果用户允许访问,开始获取图像        
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            //先获取设备
            WebCamDevice[] device = WebCamTexture.devices;

            string deviceName = device[0].name;
            //然后获取图像
            tex = new WebCamTexture(deviceName,1920,1080,60);
         
            //将获取的图像赋值
            ma.texture = tex;
            //开始实施获取
            tex.Play();

        }
    }
 
    void SaveImage()
    {
        //在上一段代码中加入该方法
        Save(tex);
        i += 1;

    }
    public void Save(WebCamTexture t)
    {
        Debug.Log(t.width + "width" + t.height + "height");
        Texture2D t2d = new Texture2D(t.width, t.height, TextureFormat.ARGB32, true);
        //将WebCamTexture 的像素保存到texture2D中
        t2d.SetPixels(t.GetPixels());
        //t2d.ReadPixels(new Rect(200,200,200,200),0,0,false);
        t2d.Apply();
        //编码
        byte[] imageTytes = t2d.EncodeToJPG();
        //if (i % 2 == 1)
        //{
        //   bgimage_02.texture= t2d;
        //}
        //else
        //{
        //    bgimage_03.texture = t2d;
        //}

        bgimage_02.texture = t2d;

        imageName = TimeIcon();
        //存储
        File.WriteAllBytes(Application.streamingAssetsPath + "/my/" + imageName + ".jpg", imageTytes);


        AssetDatabase.Refresh();



        Base64ToImg(img, body_seg());
        img.preserveAspect = true;


        Resources.UnloadUnusedAssets();
    }

    void StopCamera()
    {
        //等待用户允许访问
        //Application.RequestUserAuthorization(UserAuthorization.WebCam);
        //如果用户允许访问,开始获取图像        
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            //先获取设备
            WebCamDevice[] device = WebCamTexture.devices;

            string deviceName = device[0].name;
            //然后获取图像
            //  tex = new WebCamTexture(deviceName);
            //  //将获取的图像赋值
            // ma.material.mainTexture = tex;
            //开始实施获取
            tex.Stop();


        }
    }
    //返回按钮
    public void Back()
    {
        StopCamera();
        UnityEngine.SceneManagement.SceneManager.LoadScene(0);
    }



    /// <summary>
    /// 截图
    /// </summary>
    void CapturePicture()
    {

    }
}

本文首先使用摄像头进行拍照,然后将拍摄的照片进行无绿幕抠图,我使用的是百度智能云的人像分割功能,免费的,登录网址百度AI开放平台-全球领先的人工智能服务平台首先登录会有免费10000次使用机会,

将sdk下载了之后需要在unity里面创建一个Plugins文件夹,然后将net45和litjson插件导入,此时你需要将api compatibility level改成.Net Standard 2.0

所需要的文件在下方链接

https://download.csdn.net/download/baidu_27193941/87620523icon-default.png?t=N2N8https://download.csdn.net/download/baidu_27193941/87620523

绿幕扣完之后可以替换自己想要的图片作为背景,然后进行一次二次合成,此时对二次合成的图片进行区域截图




using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
using UnityEditor;

public class CapturePicture : MonoBehaviour
{
    public Button shootButton;
    public RectTransform UIRect;
    public RawImage _showshoot;
    private void Start()
    {

        shootButton.onClick.AddListener(shoot);
    }
    void Update()
    {

      

    }
    public void shoot()
    {
     
        string fileName = Application.streamingAssetsPath + "/shoot/" + TimeIcon() + ".jpg";
        IEnumerator coroutine = CaptureByUI(UIRect, fileName);
        StartCoroutine(coroutine);
    }
    public IEnumerator CaptureByUI(RectTransform UIRect, string mFileName)
    {
        //等待帧画面渲染结束
        yield return new WaitForEndOfFrame();

        int width = (int)(UIRect.rect.width);
        int height = (int)(UIRect.rect.height);

        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
        _showshoot.texture = tex;
        //左下角为原点(0, 0)
        //float leftBtmX = UIRect.transform.position.x + UIRect.rect.xMin;
        //float leftBtmY = UIRect.transform.position.y + UIRect.rect.yMin;

        //从屏幕读取像素, leftBtmX/leftBtnY 是读取的初始位置,width、height是读取像素的宽度和高度
        //   tex.ReadPixels(new Rect(leftBtmX, leftBtmY, 960, 540), 0, 0);
        tex.ReadPixels(new Rect(0, 0, 960, 540), 0, 0);
        //执行读取操作
        tex.Apply();
        //获取位数组
        byte[] bytes = tex.EncodeToPNG();
        //保存
        System.IO.File.WriteAllBytes(mFileName, bytes);
        AssetDatabase.Refresh();
    }
    string TimeIcon()
    {
        var year = DateTime.Now.Year;
        var mouth = DateTime.Now.Month;
        var day = DateTime.Now.Day;
        var hour = DateTime.Now.Hour;
        var minute = DateTime.Now.Minute;
        var second = DateTime.Now.Second;
        string picName = year + "-" + mouth + "-" + day + "-" + hour + "-" + minute + "-" + second;
        return picName;
    }

}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值