/****************************************************
*
* unity屏幕截图,并转换成Base64码
* 作者: lyb
* 日期:2017年7月25日
*
*****************************************************/

using System;
using System.IO;
using UnityEngine;
using System.Collections;

public class Tools_TexScreenshot : MonoBehaviour
{
public delegate void WeChatTexScreenShotDelegate(bool isSucc);
public WeChatTexScreenShotDelegate WeChatTexScreenShotCallBack;

private static Tools_TexScreenshot _Instance;
public static Tools_TexScreenshot Instance
{
get { return _Instance; }
}

void Awake()
{
_Instance = this;
}

void OnDestroy()
{
_Instance = null;

Texture_ShotDelete();
}

/// <summary>
/// 截图图片保存路径
/// </summary>
private string texShotPath
{
get
{
return projectQ.PathHelper.PersistentPath + "/screencapture.jpg";
}
}
/// <summary>
/// 压缩后的截图图片保存路径
/// </summary>
private string texZipShotPath
{
get
{
return projectQ.PathHelper.PersistentPath + "/texShare.jpg";
}
}

public byte[] GetCaptureTexture(Texture2D _tex2d)
{
_tex2d.Compress(false);
Texture2D texZip = Tools_TexGzip.TexGzipBegin(_tex2d, 0.5f);
return texZip.EncodeToJPG();
}

/// <summary>
/// 屏幕截图保存
/// </summary>
/// <param name="startPoint">截图起点坐标</param>
/// <param name="shotSize">截图大小</param>
public void Texture_Screenshot(Vector2 startPoint, Vector2 shotSize, WeChatTexScreenShotDelegate shotBack)
{
StartCoroutine(GetCapture(startPoint, shotSize, shotBack));
}

IEnumerator GetCapture(Vector2 startPoint, Vector2 shotSize, WeChatTexScreenShotDelegate shotBack)
{
// 截屏之前,删除截屏缓存文件
Texture_ShotDelete();

yield return new WaitForEndOfFrame();

int width = (int)shotSize.x;

int height = (int)shotSize.y;

Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

tex.ReadPixels(new Rect(startPoint.x, startPoint.y, width, height), 0, 0, true);

//对屏幕缓存进行压缩
tex.Compress(false);

//对图片进行压缩
Texture2D texZip = Tools_TexGzip.TexGzipBegin(tex, 0.5f);

//转化为jpg图
byte[] imagebytes = texZip.EncodeToJPG();

//存储png图
File.WriteAllBytes(texShotPath, imagebytes);

yield return null;

if (shotBack != null)
{
shotBack(true);
}
}

/// <summary>
/// 图片压缩
/// </summary>
void Texture_Gzip(int height, int width, WeChatTexScreenShotDelegate shotBack)
{
if (shotBack != null)
{
shotBack(true);
}
}

/// <summary>
/// 将图片数据转换为Base64字符串
/// </summary>
public string Texture_ToBase64()
{
if (!System.IO.File.Exists(texShotPath))
{
Debug.Log(" 图片路径无效: " + texShotPath);
return null;
}

var base64Img = Convert.ToBase64String(System.IO.File.ReadAllBytes(texShotPath));

Debug.Log(" #[图片转换成Base64]# 成功 图片信息" + base64Img);

// 分享完成,图片删除
Texture_ShotDelete();

return base64Img.ToString();
}

/// <summary>
/// 将指定路径图片转换为Base64字符串
/// </summary>
public string Texture_FixedToBase64(string texFixedPath)
{
var base64Img = Convert.ToBase64String(System.IO.File.ReadAllBytes(texFixedPath));

return base64Img.ToString();
}

/// <summary>
/// 将本地图片转换为Base64字符串
/// </summary>
public string Texture_LocalToBase64(byte[] _bytes)
{
var base64Img = Convert.ToBase64String(_bytes);

return base64Img.ToString();
}

/// <summary>
/// 将保存下来的用于分享的图片删除掉
/// </summary>
public void Texture_ShotDelete()
{
if (File.Exists(texShotPath))
{
Debug.Log(" #[如果截图的图片还在则删掉]# ");

File.Delete(texShotPath);
}

if (File.Exists(texZipShotPath))
{
Debug.Log(" #[图片转换成Base64成功后删除]# ");

File.Delete(texZipShotPath);
}
}
}

unity 截图 压缩 处理的更多相关文章

  1. Unity --- 纹理压缩基本知识点

    1.Unity支持的压缩格式的分类,这里主要指Android平台和IOS平台: DXT格式 --- Nvidia Tegra(图睿)提供ETC  --- 安卓原生支持的,OPNEGL2.0都支持,ET ...

  2. Unity截图

    什么都不说了,直接上代码. using UnityEngine; using System.Collections; using System.IO; public class CutImage : ...

  3. Unity 截图选择框,中间全透明,边缘半透明

    效果:点击白色框可拖拽选择区域 代码: using System.Collections; using System.Collections.Generic; using UnityEngine; u ...

  4. Unity项目接入应用宝SDK实现截图功能

    Unity项目接入应用宝SDK实现截图功能 问题由来 点击应用宝悬浮窗 如图所示 左下角有一个截图按钮 需要解决那些问题 截图信息需要由游戏引擎提供 SDK获取截图信息为同步 但是Unity引擎没有提 ...

  5. 【Unity技巧】调整画质(贴图)质量

    写在前面 当我们在Unity中,使用图片进行2D显示时,会发现显示出来的画面有明显的模糊或者锯齿,但是美术给的原图却十分清晰. 要改善这一状况实际上很简单. 造成这样的原因,是Unity在导入图片(或 ...

  6. Unity骨骼动画资源解析与优化

    一,背景 最近发现项目的动画文件有点大,不光内存大,而且文件也很大,所以从这2个方面下手处理 二,动画文件大小优化 为了优化动画文件大小,我们可以先分析下文件,Ctrl+D将动画文件从FBX拷贝出来, ...

  7. 构造Huffman以及实现

    构造Huffman 题目 在作业本上分别针对权值集合W=(6,5,3,4,60,18,77)和W=(7,2,4,5,8)构造哈夫曼树,提交构造过程的照片 错误回答 错误原因:遵循左边小于根右边大于根的 ...

  8. unity3d代码优化标准

    转载自:https://blog.csdn.net/m0_37283423/article/details/84378384 代码优化 ● 尽可能使用for来代替foreach:每次foreach会产 ...

  9. iOS-格式化金额,三位一逗号

    代码地址如下:http://www.demodashi.com/demo/11244.html 项目版本更新迭代中, 新增需求: 所有金额必须用标准会计表示方式(¥94,862.57). 而之前金额展 ...

随机推荐

  1. vue路由6:导航钩子

    <div id="app"> <div> <router-link to="/">首页</router-link> ...

  2. git push跳过用户名和密码认证配置教程

    在使用git commit命令将修改从暂存区提交到本地版本库后,只剩下最后一步将本地版本库的分支推送到远程服务器上对应的分支了,如果不清楚版本库的构成,可以查看我的另一篇,git 仓库的基本结构. 新 ...

  3. puts函数

    1.puts函数是gets函数的输出版本,它把指定的字符串写到标准输出并在末尾添加一个换行符 #include <stdio.h> #include <stdlib.h> in ...

  4. jsoi2018 R1R2

    Jsoi2018 R1: D1:T1:签到题,状压dp,(思考:讲题人说可以卡一卡空间,怎么做?) T2:50pts:贪心,因为无重复 100pts:线段树合并? T3:25pts 树形dp D1:T ...

  5. Tensorflow学习笔记01

    Tensorflow官方网站:http://tensorflow.org/ 极客学院Tensorflow中文版:http://wiki.jikexueyuan.com/project/tensorfl ...

  6. gcc对c++标准的支持

    GCC 4.8.1完全支持c++11核心部分,对应的glibc为2.17 gcc 4.9支持c++11正则表达式,卧槽...4.8.5会报terminate called after throwing ...

  7. CCF 推荐国际国内会议及中文核心期刊要目总览

    CCF 推荐国际国内会议及<中文核心期刊要目总览> Ref :http://www.ccf.org.cn/xspj/rgzn/ Notes: dblp 是一个好网站,上面有各种主要会议的论 ...

  8. MS08_067漏洞渗透攻击实践

    MS08_067漏洞渗透攻击实践 实验前准备 1.两台虚拟机,其中一台为kali,一台为windows xp sp3(英文版). 2.在VMware中设置两台虚拟机网络为NAT模式,自动分配IP地址, ...

  9. 经典算法分析:n与lgn

    顺序查找O(n) 二分查找O(lgn) 通过代码来感受性能差别 package recursion; /** * @author zsh * @company wlgzs * @create 2019 ...

  10. 前端 --- 4 js

    一.js  描述 JavaScript 是脚本语言 JavaScript 是一种轻量级的编程语言.后来出现了node.js,可以作为后端语言来开发项目, js是一个既能做前端又能做后端的语言 Java ...