cea485b01736ad53a0f1ac0d3554478bf2315075.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, Color, Texture2D, ImageUtil, _crd;
  4. _export("ImageUtil", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. __checkObsolete__ = _cc.__checkObsolete__;
  9. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  10. Color = _cc.Color;
  11. Texture2D = _cc.Texture2D;
  12. }],
  13. execute: function () {
  14. _crd = true;
  15. _cclegacy._RF.push({}, "ebdf3rRnEdIYpKgGdW8gSmZ", "ImageUtil", undefined);
  16. /*
  17. * @Author: dgflash
  18. * @Date: 2022-09-01 18:00:28
  19. * @LastEditors: dgflash
  20. * @LastEditTime: 2022-09-02 14:49:42
  21. */
  22. /**
  23. * 图像工具
  24. */
  25. __checkObsolete__(['Color', 'Texture2D']);
  26. _export("ImageUtil", ImageUtil = class ImageUtil {
  27. /**
  28. * 获取纹理中指定像素的颜色,原点为左上角,从像素 (1, 1) 开始。
  29. * @param texture 纹理
  30. * @param x x 坐标
  31. * @param y y 坐标
  32. * @example
  33. // 获取纹理左上角第一个像素的颜色
  34. const color = ImageUtil.getPixelColor(texture, 1, 1);
  35. cc.color(50, 100, 123, 255);
  36. */
  37. static getPixelColor(texture, x, y) {
  38. var _texture$image;
  39. const canvas = document.createElement('canvas');
  40. const ctx = canvas.getContext('2d');
  41. canvas.width = texture.width;
  42. canvas.height = texture.height;
  43. const image = (_texture$image = texture.image) == null ? void 0 : _texture$image.data;
  44. ctx.drawImage(image, 0, 0, texture.width, texture.height);
  45. const imageData = ctx.getImageData(0, 0, texture.width, texture.height);
  46. const pixelIndex = (y - 1) * texture.width * 4 + (x - 1) * 4;
  47. const pixelData = imageData.data.slice(pixelIndex, pixelIndex + 4);
  48. const color = new Color(pixelData[0], pixelData[1], pixelData[2], pixelData[3]);
  49. image.remove();
  50. canvas.remove();
  51. return color;
  52. }
  53. /**
  54. * 将图像转为 Base64 字符(仅 png、jpg 或 jpeg 格式资源)(有问题)
  55. * @param url 图像地址
  56. * @param callback 完成回调
  57. */
  58. static imageToBase64(url, callback) {
  59. return new Promise(res => {
  60. var _exec;
  61. let extname = (_exec = /\.png|\.jpg|\.jpeg/.exec(url)) == null ? void 0 : _exec[0]; //@ts-ignore
  62. if (['.png', '.jpg', '.jpeg'].includes(extname)) {
  63. const canvas = document.createElement('canvas');
  64. const ctx = canvas.getContext('2d');
  65. const image = new Image();
  66. image.src = url;
  67. image.onload = () => {
  68. canvas.height = image.height;
  69. canvas.width = image.width;
  70. ctx.drawImage(image, 0, 0);
  71. extname = extname === '.jpg' ? 'jpeg' : extname.replace('.', '');
  72. const dataURL = canvas.toDataURL(`image/${extname}`);
  73. callback && callback(dataURL);
  74. res(dataURL);
  75. image.remove();
  76. canvas.remove();
  77. };
  78. } else {
  79. console.warn('Not a jpg/jpeg or png resource!');
  80. callback && callback("");
  81. res("");
  82. }
  83. });
  84. }
  85. /**
  86. * 将 Base64 字符转为 cc.Texture2D 资源(有问题)
  87. * @param base64 Base64 字符
  88. */
  89. static base64ToTexture(base64) {
  90. const image = document.createElement('img');
  91. image.src = base64;
  92. const texture = new Texture2D(); //@ts-ignore
  93. texture.initWithElement(image);
  94. image.remove();
  95. return texture;
  96. }
  97. /**
  98. * 将 Base64 字符转为二进制数据(有问题)
  99. * @param base64 Base64 字符
  100. */
  101. static base64ToBlob(base64) {
  102. const strings = base64.split(','); //@ts-ignore
  103. const type = /image\/\w+|;/.exec(strings[0])[0];
  104. const data = window.atob(strings[1]);
  105. const arrayBuffer = new ArrayBuffer(data.length);
  106. const uint8Array = new Uint8Array(arrayBuffer);
  107. for (let i = 0; i < data.length; i++) {
  108. uint8Array[i] = data.charCodeAt(i) & 0xff;
  109. }
  110. return new Blob([uint8Array], {
  111. type: type
  112. });
  113. }
  114. });
  115. _cclegacy._RF.pop();
  116. _crd = false;
  117. }
  118. };
  119. });
  120. //# sourceMappingURL=cea485b01736ad53a0f1ac0d3554478bf2315075.js.map