Unity_Vuforia_AR_实现手机端屏幕截图功能(不带UI界面)

关键代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class ScreenShot : MonoBehaviour {

    public Camera ARCamera;

	// Use this for initialization
	void Start () {
        ARCamera = GameObject.Find("ARCamera").GetComponent<Camera>();

    }
	
	// Update is called once per frame
	void Update () {
		
	}

    //单机截屏
    public void OnScreenShotClick() {
        //获取当前时间
       System.DateTime now = System.DateTime.Now;
       string nowTime = now.ToString();
       nowTime = nowTime.Trim();
       nowTime = nowTime.Replace("/","-");
        //创建并获取 截图的图片名称
       string fileName = "ARScreenShot" + nowTime + ".png";
        //如果运行的设备是Android 就截图
        if (Application.platform == RuntimePlatform.Android)
        {
            //包含UI的截图

            创建2D图片
            参数一 图片宽度
            参数二 图片高度
            参数三 图片格式
            参数四 是否映射
            //Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
            读取图片
            参数一 要读取的视图的矩形区域。从当前渲染目标读取像素。
            参数二 从哪里开始读取
            参数三 从哪里结束
            //texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
            调用Apply截屏 成功
            //texture.Apply();

            转化成字节数组 并进行保存
            //byte[] bytes = texture.EncodeToPNG();
            存储的路径
            //string destination = "/sdcard/DCIM/Screenshots";
            如果这个路径不存在
            //if (!Directory.Exists(destination)) {
            //    //那么就创建路径
            //    Directory.CreateDirectory(destination);
            //}
            需要保存的路径
            //string pathSave = destination + "/" + fileName;
            保存(写入) 将转化成的数组 保存到pathSave路径下面
            //File.WriteAllBytes(pathSave, bytes);


            //不包含UI的截图
            //摄像机 截取屏幕
            RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 1);
            //指定targetTexture
            ARCamera.targetTexture = rt;
            //渲染
            ARCamera.Render();

            RenderTexture.active = rt;

            //创建2D图片
            //参数一 图片宽度
            //参数二 图片高度
            //参数三 图片格式
            //参数四 是否映射
            Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
            //读取图片
            //参数一 要读取的视图的矩形区域。从当前渲染目标读取像素。
            //参数二 从哪里开始读取
            //参数三 从哪里结束
            texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
            texture.Compress(true);
            //调用Apply截屏 成功
            texture.Apply();

            //截屏成功清除 销毁
            ARCamera.targetTexture = null;
            RenderTexture.active = null;
            Destroy(rt);

            //转化成字节数组 并进行保存
            byte[] bytes = texture.EncodeToPNG();
            //存储的路径
            string destination = "/sdcard/DCIM/Screenshots";
            //如果这个路径不存在
            if (!Directory.Exists(destination))
            {
                //那么就创建路径
                Directory.CreateDirectory(destination);
            }
            //需要保存的路径 
            string pathSave = destination + "/" + fileName;
            //保存(写入) 将转化成的数组 保存到pathSave路径下面
            File.WriteAllBytes(pathSave, bytes);

            //刷新图片
            string[] paths = new string[1];
            paths[0] = pathSave;
            ScanFile(paths);

        } else if (Application.platform == RuntimePlatform.IPhonePlayer) {
            //苹果截图方法
            SaveScreenshot(fileName,null,null,null);
        }
    }
    //苹果截图
    private void SaveScreenshot(string fileName, object p1, object p2, object p3)
    {
        throw new NotImplementedException();
    }
    //苹果截图
    public void SaveScreenshot(string fileName, string albumName = "MyScreenshots", string fileType = "jpg", Rect screenArea = default(Rect))
    {

        if (screenArea == default(Rect))
            screenArea = new Rect(0, 0, Screen.width, Screen.height);
        // screenArea = new Rect(Screen.width * 0.04f, Screen.height * 0.18f, Screen.width, Screen.height); //从某个点位置开始截图
        StartCoroutine(GrabScreenshot(fileName, albumName, fileType, screenArea));
    }
    //苹果截图
    public IEnumerator GrabScreenshot(string fileName, string albumName, string fileType, Rect screenArea)
    {
        yield return new WaitForEndOfFrame();
        //不包含UI的截图
        //摄像机 截取屏幕
        RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 1);
        //指定targetTexture
        ARCamera.targetTexture = rt;
        //渲染
        ARCamera.Render();

        RenderTexture.active = rt;

        Texture2D texture = new Texture2D((int)screenArea.width, (int)screenArea.height, TextureFormat.RGB24, false);
        //Texture2D texture = new Texture2D((int)(0.92 * Screen.width), (int)(0.43 * Screen.height), TextureFormat.RGB24, false);//截图的大小


        texture.ReadPixels(screenArea, 0, 0);
        texture.Apply();

        //截屏成功清除 销毁
        ARCamera.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);


        byte[] bytes;
        string fileExt;

        if (fileType == "png")
        {
            bytes = texture.EncodeToPNG();
            fileExt = ".png";
        }
        else
        {
            bytes = texture.EncodeToJPG();
            fileExt = ".jpg";
        }

        string date = System.DateTime.Now.ToString("hh-mm-ss_dd-MM-yy");
        string screenshotFilename = fileName + "_" + date + fileExt;
        string path = Application.persistentDataPath + "/" + screenshotFilename;

        System.IO.File.WriteAllBytes(path, bytes);

    }

    //刷新图片,显示到相册中
    void ScanFile(string[] path)
    {
        using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
            using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
            {
                Conn.CallStatic("scanFile", playerActivity, path, null, null);
            }
        }
    }

}

新建一个普通Camera摄像机:修改配置如下:

画布相关配置如下

创建截图按钮并与之相关联起来:

  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值