• unity(WebGL) 截图拼接并保存本地,下载PDF


    目录

    1.调用代码

    2.截图 拼图 代码


    截图参考:Unity3D 局部截图、全屏截图、带UI截图三种方法_unity 截图_野区捕龙为宠的博客-CSDN博客

    文档下载: Unity WebGL 生成doc保存到本地电脑_unity webgl 保存文件_野区捕龙为宠的博客-CSDN博客

    中文输入:Unity WebGL中文输入 支持输入法跟随 支持全屏(内附Dome)_unity中文插件-CSDN博客 

    1.调用代码

    话不多说直接上代码:

    1. private void Awake()
    2. {
    3. //点击提交下载 按钮
    4. Btn_download.onClick.AddListener(() =>
    5. {
    6. Btn_download.gameObject.SetActive(value: false);
    7. GetComponent<Capture_Long>().Cap_LongTex();//截图拼图
    8. //下载PDF
    9. GetComponent<Capture_Long>().CapCallBack = ((png_byte) =>
    10. {
    11. string filename = CQOOC.Instance.userName + "_" + DateTime.Now.ToString("g");
    12. Btn_download.gameObject.SetActive(true);
    13. Application.ExternalCall("downloadPng", png_byte, filename);//调用 webgl 方法
    14. // Debug.Log("filename="+ filename);
    15. });
    16. //上传 截图
    17. GetComponent<Capture_Long>().CapByteCallBack = ((png_byte) =>
    18. {
    19. string filename = CQOOC.Instance.userName + "_" + DateTime.Now.ToString("g");
    20. Debug.Log("filename=" + filename);
    21. //上传实验报告 截图
    22. CQOOC.Instance.UploadFile(png_byte, filename+".png", ((b) => {
    23. if (b)
    24. {
    25. Debug.Log("上传截图成功!");
    26. }
    27. else
    28. {
    29. Debug.Log("上传截图失败!");
    30. }
    31. }));
    32. //上传实验报告 数据
    33. CQOOC.Instance.UploadData_((b) => {
    34. if (b)
    35. {
    36. UITipDialogMini.Instance.SetState(true, "提交数据成功!");
    37. }
    38. else
    39. {
    40. UITipDialogMini.Instance.SetState(true, "提交数据失败!");
    41. }
    42. });
    43. });
    44. });
    45. DateTime now = DateTime.Now.AddMinutes(-15);
    46. string formattedTime = now.ToString("yyyy 年 M 月 d 日 HH:mm:ss");
    47. text_StartTime.text = formattedTime;
    48. DateTime now02 = DateTime.Now;
    49. string formattedTime02 = now02.ToString("yyyy 年 M 月 d 日 HH:mm:ss");
    50. text_EndTime.text = formattedTime02;
    51. // text_StartTime.text = DateTime.Now.AddMinutes(-15).ToString("F");
    52. //text_EndTime.text = DateTime.Now.ToString("F");
    53. text_UserName.text = CQOOC.Instance.userName;
    54. text_Score.text = UnityEngine.Random.Range(80f, 95f).ToString("0");
    55. }

     

    2.截图 拼图 代码

    由于图片太长需要拼接

    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class Capture_Long : MonoBehaviour
    6. {
    7. public RectTransform svrt;
    8. public RectTransform contentRT;
    9. public Action<string> CapCallBack;
    10. public Action<byte[]> CapByteCallBack;
    11. public Vector2 vec;
    12. /// <summary>
    13. /// 接长图,进行拼接
    14. /// </summary>
    15. /// <param name="SVRT">Scroll Rect</param>
    16. /// <param name="ContentRT">Content</param>
    17. public void Cap_LongTex()
    18. {
    19. StartCoroutine(Cap(svrt, contentRT));
    20. }
    21. private IEnumerator Cap(RectTransform SVRT, RectTransform ContentRT)
    22. {
    23. //画布中的高度 950
    24. float SV_Y = SVRT.sizeDelta.y;
    25. //content显示的区域高度 0
    26. float Content_Y = ContentRT.anchoredPosition.y;
    27. //图片的整体高度 2660
    28. float Content_Height = contentRT.sizeDelta.y+ SV_Y;
    29. var mult = (float)(Content_Height / SV_Y);
    30. //整数倍
    31. int mult_int = (int)mult;
    32. //最后的小数倍
    33. float mult_float = mult - mult_int;
    34. //滚动条的宽度
    35. float verticalScrollbar_weight = SVRT.GetComponent<ScrollRect>().verticalScrollbar.GetComponent<RectTransform>().sizeDelta.x;
    36. yield return new WaitForEndOfFrame();
    37. //合成图片的总高度
    38. int totalHeight = (int)Content_Height;
    39. Texture2D endTex = new Texture2D((int)vec.y, totalHeight, TextureFormat.RGBA32, false);
    40. int x = 0, y = 0;
    41. Color32[] colors;
    42. //整数倍的截图
    43. for (int i = 0; i < mult_int; i++)
    44. {
    45. ContentRT.anchoredPosition = new Vector2(0, SV_Y * i);
    46. yield return new WaitForEndOfFrame();
    47. //Rect rect_int = new Rect(Screen.width / 2 - SVRT.sizeDelta.x / 2 + SVRT.anchoredPosition.x, Screen.height / 2 - SVRT.sizeDelta.y / 2 + SVRT.anchoredPosition.y, SVRT.sizeDelta.x - verticalScrollbar_weight, SVRT.sizeDelta.y);
    48. //Rect rect_int = new Rect(420, 166.5f, 1365, SVRT.sizeDelta.y);
    49. Rect rect_int = new Rect(420, vec.x, vec.y, SVRT.sizeDelta.y);
    50. var tex_int = CaptureScreenshot(rect_int, i + "");
    51. //合成
    52. colors = tex_int.GetPixels32(0);
    53. if (i > 0)
    54. {
    55. y -= tex_int.height;
    56. }
    57. else
    58. {
    59. y = totalHeight - tex_int.height;
    60. }
    61. endTex.SetPixels32(x, y, tex_int.width, tex_int.height, colors);
    62. }
    63. //小数倍的截图
    64. ContentRT.anchoredPosition = new Vector2(0, SV_Y * (mult - 1));
    65. yield return new WaitForEndOfFrame();
    66. //Rect rect_float = new Rect(Screen.width / 2 - SVRT.sizeDelta.x / 2 + SVRT.anchoredPosition.x, Screen.height / 2 - SVRT.sizeDelta.y / 2 + SVRT.anchoredPosition.y, SVRT.sizeDelta.x - verticalScrollbar_weight, SVRT.sizeDelta.y * mult_float);
    67. Rect rect_float = new Rect(420, vec.x, vec.y, SVRT.sizeDelta.y * mult_float);
    68. var tex_float = CaptureScreenshot(rect_float, "end");
    69. //合成
    70. colors = tex_float.GetPixels32(0);
    71. y -= tex_float.height;
    72. endTex.SetPixels32(x, y, tex_float.width, tex_float.height, colors);
    73. endTex.Apply();
    74. byte[] bytes = endTex.EncodeToPNG();//然后将这些纹理数据,成一个png图片文件
    75. var strPng = Convert.ToBase64String(bytes);
    76. #if UNITY_EDITOR
    77. string filename = Application.dataPath + "/PNG/合成.png";
    78. System.IO.File.WriteAllBytes(filename, bytes);
    79. Debug.Log(string.Format("截屏了一张图片: {0}", filename));
    80. #endif
    81. SVRT.GetComponent<ScrollRect>().verticalNormalizedPosition = 1;
    82. CapCallBack?.Invoke(strPng);
    83. CapByteCallBack?.Invoke(bytes);
    84. }
    85. /// <summary>
    86. /// 开始截图
    87. /// </summary>
    88. /// <returns>The screenshot2.</returns>
    89. /// <param name="rect">Rect.截图的区域,左下角为o点</param>
    90. private Texture2D CaptureScreenshot(Rect rect, string name)
    91. {
    92. //Debug.Log(rect.ToString());
    93. Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGBA32, false);//先创建一个的空纹理,大小可根据实现需要来设置
    94. screenShot.ReadPixels(rect, 0, 0);//读取屏幕像素信息并存储为纹理数据,
    95. screenShot.Apply();
    96. #if UNITY_EDITOR
    97. byte[] bytes = screenShot.EncodeToPNG();//然后将这些纹理数据,成一个png图片文件
    98. string filename = Application.dataPath + "/PNG/Screenshot" + name + ".png";
    99. System.IO.File.WriteAllBytes(filename, bytes);
    100. //Debug.Log(string.Format("截屏了一张图片: {0}", filename));
    101. #endif
    102. return screenShot;
    103. }
    104. }

    3.web端代码

    1. <!DOCTYPE html>
    2. <html lang="en-us">
    3. <head>
    4. <meta charset="utf-8">
    5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    6. <title>XiaoLuoDai</title>
    7. <link rel="shortcut icon" href="TemplateData/favicon.ico">
    8. <link rel="stylesheet" href="TemplateData/style.css">
    9. <script src="TemplateData/UnityProgress.js"></script>
    10. <script src="Build/UnityLoader.js"></script>
    11. <script>
    12. var unityInstance = UnityLoader.instantiate("unityContainer", "Build/WebGL.json", {onProgress: UnityProgress});
    13. function GetWebGLURI()
    14. {
    15. unityInstance.SendMessage("CQOOC", "GetURI_CallBack", window.location.href);
    16. }
    17. </script>
    18. <script src="https://cdn.bootcss.com/jspdf/1.3.4/jspdf.debug.js"></script>
    19. <script>
    20. function downloadPng(base64, filename) {
    21. var baseData = 'data:image/png;base64,' + base64
    22. ///
    23. // 获取图片文件的宽高
    24. ///
    25. let image = new Image();
    26. image.src = baseData;
    27. image.onload = function () {
    28. console.log(image.width, image.height);
    29. var contentWidth = image.width;
    30. var contentHeight = image.height;
    31. //一页pdf显示html页面生成的canvas高度;
    32. var pageHeight = contentWidth / 592.28 * 841.89;
    33. //未生成pdf的html页面高度
    34. var leftHeight = contentHeight;
    35. //页面偏移
    36. var position = 0;
    37. //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
    38. var imgWidth = 595.28;
    39. var imgHeight = 592.28 / contentWidth * contentHeight;
    40. //有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)
    41. //当内容未超过pdf一页显示的范围,无需分页
    42. var pdf = new jsPDF('', 'pt', 'a4');
    43. if (leftHeight < pageHeight) {
    44. pdf.addImage(baseData, 'PNG', 0, 0, imgWidth, imgHeight);
    45. } else {
    46. while (leftHeight > 0) {
    47. pdf.addImage(baseData, 'PNG', 0, position, imgWidth, imgHeight)
    48. leftHeight -= pageHeight;
    49. position -= 841.89;
    50. //避免添加空白页
    51. if (leftHeight > 0) {
    52. pdf.addPage();
    53. }
    54. }
    55. }
    56. pdf.save(filename + ".pdf")
    57. }
    58. }
    59. </script>
    60. <script>
    61. function FullScreenMetthod()//中文输入
    62. {
    63. document.getElementById('unityContainer').requestFullscreen();
    64. }
    65. </script>
    66. </head>
    67. <body>
    68. <div class="webgl-content">
    69. <div id="unityContainer" style="width: 1920px; height: 1080px"></div>
    70. <div class="footer">
    71. <div class="webgl-logo"></div>
    72. <div class="fullscreen" onclick="FullScreenMetthod()"></div>
    73. <div class="title">XiaoLuoDai</div>
    74. </div>
    75. </div>
    76. </body>
    77. </html>

  • 相关阅读:
    【FPGA图像融合】基于vivado HLS的图像融合算法的FPGA实现
    YOLOv8改进实战 | 更换主干网络Backbone之PoolFormer篇
    网络基础—网关、网段、子网掩码
    最优控制问题中的折扣因子
    物联网网关硬件和云端分别实现了哪些功能?-天拓四方
    Netty架构详解
    常用(闭源、开源)关系型数据库架构和实现原理
    主流开发语言和开发环境介绍
    栈&队列OJ练习题(C语言版)
    Nginx 安装第三方模块 不停机 平滑升级 方法2
  • 原文地址:https://blog.csdn.net/qq_37524903/article/details/133855092