/****************************************************
*
* 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. jQuery获取子元素个数的方法

    //获取id=div1下的子元素的个数 $('#id').children().length; //获取id=div1下的p元素个数 $('#id').children('p').length;

  2. c--socket通信TCP篇

    https://www.cnblogs.com/ashen/p/4474360.html #include <sys/socket.h> 2 #include <stdlib.h&g ...

  3. fjwc2019

    机房搬迁.......再加上文化课.......咕了十几天才有空补上....... day0听一个教授讲理论......在学长的带领下咕掉了..... D1 T1:#178. 「2019冬令营提高组」 ...

  4. PyCharm 设置护眼背景色

    PyCharm 设置护眼背景色 一.方法 File -> Seting -> Editor -> Color Scheme -> General -> Text -> ...

  5. 错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 解决方法

    晚上花几分钟在windows下测了下pthread的用法,出现错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 ...

  6. 【题解】Luogu P2605 [ZJOI2010]基站选址

    原题传送门:P2604 [ZJOI2010]基站选址 看一眼题目,变知道这题一定是dp 设f[i][j]表示在第i个村庄修建第j个基站且不考虑i+1~n个村庄的最小费用 可以得出f[i][j] = M ...

  7. Android - Resource 之 String 小结

    简单的string: <?xml version="1.0" encoding="utf-8"?> <resources> <st ...

  8. iOS App架构相关

    一) 我们可以定义一个好的架构应该具备的特点: 任务均衡分摊给具有清晰角色的实体 可测试性通常都来自与上一条(对于一个合适的架构是非常容易) 易用性和低成本维护 二) 传统的MVC模式.:三个实体间相 ...

  9. Linux服务器搭建Nexus-Maven私服(适合新手比较基础)

    背景 在使用maven构建项目的时候,几乎都会涉及到一个“私服”的概念,那么到底什么是私服?使用私服有能够带来哪些益处? 私服:私服是指私有服务器,是架设在局域网的一种特殊的远程仓库,目的是代理远程仓 ...

  10. 2018 蓝桥杯省赛 B 组模拟赛(五)

    A模拟 代码1 #include<bits/stdc++.h> using namespace std; int n = 101; int a[120][120]; int ans = 0 ...