YYPOST群发软件 发表于 2025-12-2 08:14:45

AIWROK截图缓存工具

AIWROK截图缓存工具






/**
* AIWROK截图缓存工具
* 功能:提供简单的截图捕获和缓存管理
* 作者:AIWROK开发团队
* // AIWROK软件安卓交流QQ群711841924
* //苹果内测软件QQ群648461709
*/

var ScreenshotCache = {
    // 简单缓存存储
    _cache: null,
    _cacheKey: null,
    _cacheTimestamp: null,
   
    /**
   * 捕获全屏截图并缓存
   * @param {String} key 可选的缓存键名,不提供则使用默认键
   * @returns {Object} 截图对象,如果失败则返回null
   */
    captureFullScreen: function(key) {
      // 设置默认键
      key = key || 'default';
      
      try {
            // 检查AIWROK环境
            if (typeof screen === 'undefined') {
                printl('错误: AIWROK截图模块不可用');
                return null;
            }
            
            printl('执行全屏截图...');
            var screenshot = null;
            
            // 使用AIWROK全屏截图方法
            if (typeof screen.screenShotFull === 'function') {
                screenshot = screen.screenShotFull();
               
                if (screenshot) {
                  printl('全屏截图成功');
                  // 缓存截图
                  this._cache = screenshot;
                  this._cacheKey = key;
                  this._cacheTimestamp = new Date().getTime();
                } else {
                  printl('全屏截图返回空对象');
                }
            } else {
                printl('错误: screen.screenShotFull方法不可用');
            }
            
            return screenshot;
      } catch (e) {
            printl('全屏截图失败: ' + (e.message || e));
            return null;
      }
    },
   
    /**
   * 捕获区域截图并缓存
   * @param {String} key 可选的缓存键名,不提供则使用默认键
   * @param {Number} x 起始X坐标
   * @param {Number} y 起始Y坐标
   * @param {Number} width 宽度
   * @param {Number} height 高度
   * @returns {Object} 截图对象,如果失败则返回null
   */
    captureRegion: function(key, x, y, width, height) {
      // 参数验证
      if (typeof x !== 'number' || typeof y !== 'number' ||
            typeof width !== 'number' || typeof height !== 'number' ||
            width <= 0 || height <= 0) {
            printl('错误: 截图区域参数无效');
            return null;
      }
      
      // 设置默认键
      key = key || 'default';
      
      try {
            // 检查AIWROK环境
            if (typeof screen === 'undefined') {
                printl('错误: AIWROK截图模块不可用');
                return null;
            }
            
            printl('执行区域截图: x=' + x + ', y=' + y + ', width=' + width + ', height=' + height);
            var screenshot = null;
            
            // 尝试区域截图
            if (typeof screen.screenShot === 'function') {
                // 尝试不同参数格式
                try {
                  screenshot = screen.screenShot(width, height, 100);
                } catch (e1) {
                  // 尝试四参数格式
                  try {
                        screenshot = screen.screenShot(x, y, width, height);
                  } catch (e2) {
                        printl('区域截图失败: ' + (e2.message || e2));
                  }
                }
               
                // 如果区域截图失败,尝试全屏截图作为备选
                if (!screenshot) {
                  printl('区域截图失败,尝试全屏截图作为备选');
                  screenshot = this.captureFullScreen('temp_full');
                  if (screenshot) {
                        printl('使用全屏截图作为替代');
                  }
                }
            } else {
                printl('错误: screen.screenShot方法不可用');
            }
            
            if (screenshot) {
                // 缓存截图
                this._cache = screenshot;
                this._cacheKey = key;
                this._cacheTimestamp = new Date().getTime();
            }
            
            return screenshot;
      } catch (e) {
            printl('区域截图失败: ' + (e.message || e));
            return null;
      }
    },
   
    /**
   * 获取当前缓存的截图
   * @returns {Object} 缓存的截图对象,如果没有缓存则返回null
   */
    getCachedScreenshot: function() {
      if (this._cache) {
            printl('获取缓存的截图: ' + this._cacheKey);
      } else {
            printl('没有缓存的截图');
      }
      return this._cache;
    },
   
    /**
   * 释放当前缓存的截图资源
   * @returns {Boolean} 是否释放成功
   */
    releaseCache: function() {
      if (!this._cache) {
            printl('没有可释放的缓存');
            return false;
      }
      
      try {
            // 尝试安全释放资源
            if (typeof this._cache === 'object') {
                // 这里可以添加特定的资源释放逻辑
                // 例如调用特定的释放方法等
                printl('释放截图资源');
            }
            
            // 清理缓存引用
            this._cache = null;
            this._cacheKey = null;
            this._cacheTimestamp = null;
            
            printl('缓存已成功释放');
            return true;
      } catch (e) {
            printl('释放缓存失败: ' + (e.message || e));
            return false;
      }
    },
   
    /**
   * 检查是否有缓存的截图
   * @returns {Boolean} 是否有缓存
   */
    hasCache: function() {
      return this._cache !== null;
    },
   
    /**
   * 获取当前缓存的信息
   * @returns {Object} 缓存信息
   */
    getCacheInfo: function() {
      return {
            hasCache: this._cache !== null,
            key: this._cacheKey,
            timestamp: this._cacheTimestamp,
            age: this._cacheTimestamp ? new Date().getTime() - this._cacheTimestamp : 0
      };
    },
   
    /**
   * 查看当前缓存的截图
   * 直接使用printl输出图片对象,符合AIWROK环境的实际使用方式
   * @returns {Boolean} 是否成功显示图片
   */
    viewCachedImage: function() {
      if (!this._cache) {
            printl('错误: 没有可查看的缓存图片');
            return false;
      }
      
      try {
            printl('使用printl直接输出图片对象:');
            // 根据用户反馈,直接使用printl输出图片对象是最有效的方式
            printl(this._cache);
            printl('图片打印完成');
            return true;
      } catch (e) {
            printl('显示图片失败: ' + (e.message || e));
            return false;
      }
    }
};

/**
* 测试图片显示功能
* 功能:测试使用printl直接输出图片对象的方法
* @param {Object} imageObj 可选的图片对象,如果不提供则尝试截图
*/
function testImageDisplay(imageObj) {
    printl('=== 图片显示功能测试 ===');
   
    // 如果没有提供图片对象,尝试截图
    var testImage = imageObj || null;
    if (!testImage) {
      printl('\n正在捕获测试截图...');
      testImage = ScreenshotCache.captureFullScreen('test_display');
      if (!testImage) {
            printl('错误: 无法获取测试图片');
            return false;
      }
    }
   
    printl('\n使用printl直接输出图片对象...');
   
    try {
      // 根据用户反馈,直接使用printl输出图片对象是最有效的方式
      printl('图片对象信息:');
      printl(testImage);
      printl('\n测试结果: 成功使用printl输出图片');
      return true;
    } catch (e) {
      printl('\n测试结果: 失败 - ' + (e.message || e));
      return false;
    }
}

/**
* 简单使用示例
*/
function screenshotExample() {
    printl('=== AIWROK截图缓存工具示例 ===');
   
    // 1. 执行全屏截图
    printl('\n1. 执行全屏截图...');
    var screenshot = ScreenshotCache.captureFullScreen('main_screen');
   
    if (screenshot) {
      printl('全屏截图成功并已缓存');
      
      // 2. 查看缓存信息
      printl('\n2. 缓存信息:');
      var info = ScreenshotCache.getCacheInfo();
      printl('缓存键: ' + info.key);
      printl('缓存年龄: ' + info.age + ' 毫秒');
      
      // 3. 获取缓存的截图(例如用于处理)
      printl('\n3. 获取缓存的截图...');
      var cachedScreenshot = ScreenshotCache.getCachedScreenshot();
      
      if (cachedScreenshot) {
            printl('成功获取缓存的截图');
            // 4. 查看缓存的图片
            printl('\n4. 查看缓存的图片...');
            printl('提示: 使用printl直接输出图片对象,符合AIWROK环境的实际使用方式');
            var viewResult = ScreenshotCache.viewCachedImage();
            printl('图片显示: ' + (viewResult ? '成功' : '失败'));
      }
      
      // 5. 使用完毕后释放缓存
      printl('\n5. 释放缓存资源...');
      ScreenshotCache.releaseCache();
      
      // 6. 验证缓存是否已释放
      printl('\n6. 验证缓存状态:');
      printl('缓存存在: ' + (ScreenshotCache.hasCache() ? '是' : '否'));
    } else {
      printl('全屏截图失败');
    }
   
    printl('\n=== 示例执行完成 ===');
}

/**
* 主函数 - 演示基本功能
*/
function main() {
    printl('====================================');
    printl('    AIWROK截图缓存工具');
    printl('====================================');
    printl('简单实用的截图和缓存管理');
    printl('====================================');
   
    // 运行示例
    screenshotExample();
   
    printl('\n使用说明:');
    printl('1. 截图并缓存: ScreenshotCache.captureFullScreen(键名)');
    printl('   或 ScreenshotCache.captureRegion(键名, x, y, 宽, 高)');
    printl('2. 获取缓存: ScreenshotCache.getCachedScreenshot()');
    printl('3. 查看图片: ScreenshotCache.viewCachedImage()');
    printl('   (使用printl直接输出图片对象,符合AIWROK环境的实际使用方式)');
    printl('4. 释放缓存: ScreenshotCache.releaseCache()');
    printl('5. 检查缓存: ScreenshotCache.hasCache()');
    printl('6. 获取信息: ScreenshotCache.getCacheInfo()');
    printl('');
    printl('图片显示说明:');
    printl('- 已根据AIWROK环境实际使用方式优化,直接使用printl输出图片对象');
   printl('- 您也可以直接获取缓存的图片对象后手动使用printl输出:');
   printl('var img = ScreenshotCache.getCachedScreenshot();');
   printl('printl(img);');
   printl('====================================');
}

main()


页: [1]
查看完整版本: AIWROK截图缓存工具