/****************************************************
*
* 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. [转载]C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编

    原文出处:https://www.cnblogs.com/wuhuisheng/archive/2011/03/23/1992652.html 验证电话号码的主要代码如下: public bool I ...

  2. 爬虫学习06用selenium爬取空间

    用selenium爬取空间 from selenium import webdriver from lxml import etree import time pro = webdriver.Chro ...

  3. runltp&ltp-pan

  4. P2219 [HAOI2007]修筑绿化带(单调队列)

    P2219 [HAOI2007]修筑绿化带 二维单调队列 写了这题 P2216 [HAOI2007]理想的正方形  后,你发现可以搞个二维单调队列 来保存矩形(i+1,i+A-1)(j+1,j+B-1 ...

  5. 【题解】Luogu CF915E Physical Education Lessons

    原题传送门:CF915E Physical Education Lessons 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 这道题很简单啊 每个操作就是区间赋值,顺带把总和修 ...

  6. python简说(二十)操作excel

    一.pip install xlrdpip install xlwtpip install xlutils 二.写excel import xlwtbook = xlwt.Workbook() #新建 ...

  7. 在VS2010中使用Git

    转载:https://www.cnblogs.com/oec2003/archive/2012/11/13/2768860.html 一. 安装Git命令行,下载地址:http://code.goog ...

  8. python --- 21 MRO C3算法

    一.python2.2之前用的是   经典类的MRO继承 ①深度递归继承     从左到右 ,一条路走到黑 ②广度继承           一层一层的继承 深度继承时   为   R 1 2 3 4 ...

  9. linux下如何调试Makefile?

    答:make --debug=all (输出所有的调试信息)

  10. DataTabel 与DataView之间的转化

    DataTable转为DataView,或者反之转化, 使用的是文档/试图模型,DataTable可以有多个视图,这样就可以不需要借助List类型对dataTable数据进行筛选或者排序 //Data ...