官方给的文档里面有详细的显示截图的代码 网址如下
https://gitee.com/mirrors_cocos-creator/example-cases/tree/v2.4.3/assets/cases/07_capture_texture

摄像机截图就是截取摄像机当前显示的画面

我的代码为

capture_scree.ts

@property(cc.Camera)camera: cc.Camera@property(cc.Node)jietu: cc.Node@property(cc.Node)jietu2: cc.Node@property(cc.Node)jietu3: cc.Node_canvas: null_width: 0_height: 0texturepicData: Uint8Array;start() {this.init1();// create the capturethis.camera.enabled = true;this.jietu3.active = false;this.schedule(() => {this.jietu3.active = true;let picData = this.initImage();this.createCanvas(picData);this.label.string = 'Showing the capture'this.saveFile(picData)this.camera.enabled = false;}, 1, 0);}init1() {this.label.string = '';let texture = new cc.RenderTexture();texture.initWithSize(cc.visibleRect.width, cc.visibleRect.height, cc.gfx.RB_FMT_S8);this.camera.targetTexture = texture;this.texture = texture;}// overrideinitImage() {let data = this.texture.readPixels();this._width = this.texture.width;this._height = this.texture.height;let picData = this.filpYImage(data, this._width, this._height);this.picData = picDatareturn picData;}// override init with DatacreateCanvas(picData) {let width = cc.winSize.width;let height = cc.winSize.height;let texture = new cc.Texture2D();texture.initWithData(picData, 32, this._width, this._height);let spriteFrame = new cc.SpriteFrame();spriteFrame.setTexture(texture);let node = this.jietu;let sprite = node.getComponent(cc.Sprite);sprite.spriteFrame = spriteFrame;// node.x = width / 2;// node.y = height / 2;this.jietu2.width = width * 0.8this.jietu2.height = height * 0.8this.jietu.width = width * 0.6this.jietu.height = height * 0.6this.jietu3.x *= 0.6this.jietu3.y *= 0.6}saveFile(picData) {console.log("进到这里了22222")if (CC_JSB) {console.log("进到这里了11111")let filePath = jsb.fileUtils.getWritablePath() + 'render_to_sprite_image.png';let success = jsb.saveImageData(picData, this._width, this._height, filePath)console.log(success, "successsuccesssuccess")if (success) {console.log("save image data success, file: " + filePath)jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "saveTextureToLocal2", "(Ljava/lang/String;)V",filePath);//jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "saveTextureToLocal", "(Ljava/lang/String;)V", filePath);}else {console.log("save image data failed!")// cc.error("save image data failed!");}}}// This is a temporary solutionfilpYImage(data, width, height) {// create the data arraylet picData = new Uint8Array(width * height * 4);console.log("11111", picData)let rowBytes = width * 4;for (let row = 0; row < height; row++) {let srow = height - 1 - row;let start = srow * width * 4;let reStart = row * width * 4;// save the piexls datafor (let i = 0; i < rowBytes; i++) {picData[reStart + i] = data[start + i];}}console.log("222222", picData)return picData;}captureAction(capture, width, height) {let scaleAction = cc.scaleTo(1, 0.3);let targetPos = cc.v2(width - width / 6, height / 4);let moveAction = cc.moveTo(1, targetPos);let spawn = cc.spawn(scaleAction, moveAction);capture.runAction(spawn);let blinkAction = cc.blink(0.1, 1);// scene actionthis.node.runAction(blinkAction);}

比较需要注意的是内部保存成功后需要把图片变成相册中的照片
就需要Java那边做操作了
  jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "saveTextureToLocal2", "(Ljava/lang/String;)V",filePath);

调用这个静态方法 把我们内部储存好的图片路径传进去

下面是saveTextureToLocal2方法

 // 获取 路径中的图片 保存到本地public static void saveTextureToLocal2( String pngPath) {
//先调用上边的方法 获取一下权限  有的时候会说你没有权限
//从路径中读取 照片Bitmap bmp = BitmapFactory.decodeFile(pngPath);// fileName ==textureName  尽量和JS保存的一致String fileName = "textureName";File file = new File(pngPath);try {FileOutputStream fos = new FileOutputStream(file);bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);fos.flush();fos.close();Log.d("保存成功",pngPath );} catch (FileNotFoundException e) {Log.d("保存错误1",e.toString());e.printStackTrace();} catch (IOException e) {Log.d("保存错误2",e.toString());e.printStackTrace();}// 其次把文件插入到系统图库try {MediaStore.Images.Media.insertImage(AppActivity.getContext().getApplicationContext().getContentResolver(),file.getAbsolutePath(), fileName, null);} catch (FileNotFoundException e) {e.printStackTrace();}// 最后通知图库更新AppActivity.getContext().getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(file.getAbsolutePath())));}

下面是引用方法
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这样就能成功截图咯

cocos creator 显示截图并保存图片到手机相关推荐

  1. Cocos creator 导入 tiled map地图资源,cocos 显示地图错乱偏移

    1)Tiled map 中显示 (举例子随便画了一个) 2)Cocos creator显示的地图(严重错乱偏移) 3)修改过程 ①第一步: ②修改Trim type=none ③保存(正常显示了)

  2. Cocos Creator 从下载图片到手机本地相册

    在游戏中难免会有使用保存图片到本地的功能 比如二维码等 但是creator的方法是保存在他的游戏目录里 如果手机没有root是无法查看的 这个方法就是 把图片下载到游戏目录 然后在通过目录查找图片 然 ...

  3. cocos creator 读取android相册并显示

    cocos creator 读取android相册并显示 前言 由于功能需求,需要读取手机相册的图片并在游戏中加载出来,百度了很多,也遇到了许多坑,下面这个是比较简单的,需要的自取. cocos 调用 ...

  4. Cocos Creator 获得手机陀螺仪(Gyrometer)数据

    接触 Cocos Creator 已经一年多, 体验是酸甜苦辣俱全, 不过仍然要夸一下这东西确实神作, 可以让我这种网页小白靠着Unity开发经验直接上手. 到目前为止的 Cocos Creator ...

  5. 【cocos creator 3.x】精灵图片不显示

    精灵图片不显示 现象 原因 解决方案 现象 在cocos creator 3.2版本的使用中遇到了精灵图片无法展示的几个场景: 在prefab某个node下Sprite的图片无法显示 动态加载pref ...

  6. cocos creator 3.x 精灵不显示、加载动态图片、物理碰撞、人物跟随鼠标移动、碰撞后节点销毁

    温馨提醒:即刻转去Unity3d 精灵不显示: 不要在空节点下直接添加组件 正确的做法是:在空节点(Node)上右键创建一个精灵才会给看到 或者直接拖曳一个图片放到场景编辑器中也可 cocos cre ...

  7. 麒麟子Cocos Creator实用技巧一:如何正确地显示微信头像

    不管是游戏App,还是H5,又或者是微信小游戏.但凡接入了微信登录的应用,都可能需要显示微信头像. 在Cocos Creator中,我们常见的显示方法像下面这样 var headimg = 'http ...

  8. cocos creator 实现手机震动的效果(最全说明)

    cocos creator 实现手机震动的效果(最全说明) 之前在做creator时,需要打包调用安卓震动,看了许多其他博客的方法,虽然意思说明了但是东西不全,因此也花了不少时间.然后自己总结了套最全 ...

  9. Cocos Creator 微信小游戏无法正确显示头像 解决方案

    常见的获取微信头像方法 UserInfo:  用户授权成功后 返回的微信用户信息 UserInfo 结构体信息详情: 进入 微信如何登陆授权: 进入 let url = userInfo.avatar ...

最新文章

  1. JavaBean规范
  2. 微服务的基石--持续集成
  3. unity加载sprite_Unity 分离贴图 alpha 通道实践
  4. 怎样检查python环境是否安装好_如何搭建pytorch环境的方法步骤
  5. jdk源码分析书籍 pdf_什么?Spring5 AOP 默认使用Cglib?从现象到源码深度分析
  6. ant中的table行列不对齐问题,以及换行,隐藏等问题
  7. 李牛(Linux)打包
  8. Java并发编程实战~ReadWriteLock~
  9. 【SSH框架】之Spring系列(一)
  10. 【SQL 提示 之二】index_ss Index Skip Hint
  11. Linux服务器Cache占用过多内存导致系统内存不足问题的排查解决(续)
  12. 子类重写方法aop切不到_Spring-aop 全面解析(从应用到原理)
  13. JavaScript之流程控制
  14. html制作象棋教程入门教程,canvas 纯js 绘制中国象棋棋盘
  15. 无线PLC专用数据终端应用方案
  16. 前端基础—HTML制作课程表
  17. struts2和hibernate的简单新闻发布系统_点赞!北斗卫星导航系统28nm工艺芯片已量产,全球范围定位精度优于10米...
  18. 子域名收集 -- 提莫(teemo)
  19. 小僧去接众僧来赴道 水浒
  20. SAP物料清单MM60中如何统计输出条目数量

热门文章

  1. ElasticSearch 启动问题:“error downloading geoip database [GeoLite2-Country.mmdb]“
  2. YY游戏云的AngularJS实践
  3. 北洋雷达UST-10LX基于ROS都安装使用测试小问题
  4. 【解决方法】ld: warning: directory not found for option
  5. 《程序员的自我修养》阅读笔记(一)
  6. 六轴机器人直角坐标系建立_知识篇-六轴机器人坐标
  7. 如何从twitter上爬取数据?
  8. 主板常见故障的维修方法
  9. Pandas与SQL比较
  10. 全文检索(elasticsearch入门)