IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> cocos creator 显示截图并保存图片到手机 -> 正文阅读

[游戏开发]cocos creator 显示截图并保存图片到手机

官方给的文档里面有详细的显示截图的代码 网址如下
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: 0
    texture
    picData: Uint8Array;

    start() {
        this.init1();
        // create the capture
        this.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;
    }
    // override
    initImage() {
        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 = picData
        return picData;
    }

    // override init with Data
    createCanvas(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.8
        this.jietu2.height = height * 0.8

        this.jietu.width = width * 0.6
        this.jietu.height = height * 0.6

        this.jietu3.x *= 0.6
        this.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 solution
    filpYImage(data, width, height) {
        // create the data array
        let 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 data
            for (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 action
        this.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" />

这样就能成功截图咯

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-07-30 13:04:32  更:2021-07-30 13:04:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 17:27:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码