天天看点

creator节点截图

creator节点截图:

public static captureSprite(node : cc.Node , file : string , callBack ?: Function , target ?: any)
    {
        let bResult = true;
        let fullPath = "";
        let width = cc.winSize.width;
        let height = cc.winSize.height;
        
        // 以下width, height变量为局部截图的宽高
        let mainCamera : cc.Camera = cc.Camera.main;

        // 新建一个 RenderTexture,并且设置 camera 的 targetTexture 为新建的 RenderTexture,这样 camera 的内容将会渲染到新建的 RenderTexture 中
        let texture = new cc.RenderTexture();
        let gl = cc.game["_renderContext"];

        // 左下角为0,0获取包围盒 , 修正包围盒是在屏幕内部
        let sRect = node.getBoundingBoxToWorld();
        let ldPos = cc.v2(sRect.x , sRect.y);
        let rTpos = cc.v2(sRect.x + sRect.width , sRect.y + sRect.height);
        ldPos.x = Math.max(0 , ldPos.x);
        ldPos.y = Math.max(0 , ldPos.y);
        rTpos.x = Math.min(width , rTpos.x);
        rTpos.y = Math.min(height , rTpos.y);
        sRect.x = Math.floor(ldPos.x);
        sRect.y = Math.floor(ldPos.y);
        sRect.width = Math.floor(rTpos.x - ldPos.x);
        sRect.height = Math.floor(rTpos.y - ldPos.y);

        // 渲染摄像机 如果有Mask的话 添加 gl.STENCIL_INDEX8 参数 截取全屏数据
        texture.initWithSize(width, height , gl.STENCIL_INDEX8);
        mainCamera.targetTexture = texture;
        mainCamera.render(node);
        mainCamera.targetTexture = null;

        // 这样我们就能从 RenderTexture 中获取到数据了
        let data : any = texture.readPixels(null , sRect.x , sRect.y , sRect.width , sRect.height);
        // 以下代码将截图后默认倒置的图片回正
        let sData = new Uint8Array(sRect.width * sRect.height * 4);
        let rBytes = sRect.width * 4;
        for (let row = 0; row < sRect.height; row++)
        {
            let srow = sRect.height - 1 - row;
            let start = Math.floor(srow * sRect.width * 4);
            let reStart = row * sRect.width * 4;
            for (let i = 0; i < rBytes; i++)
            {
                sData[reStart + i] = data[start + i];
            }
        }

        // 原生调用
        if (cc.sys.isNative && jsb)
        {
            // 执行回到
            let wPath = jsb.fileUtils.getWritablePath();
            fullPath = wPath + file;
            if (jsb.fileUtils.isDirectoryExist(wPath))
            {
                jsb.fileUtils.createDirectory(wPath);
            }
            if (jsb.fileUtils.isFileExist(fullPath))
            {
                jsb.fileUtils.removeFile(fullPath);
            }
            // 保存图片
            console.log(fullPath , sRect.x , sRect.y , sRect.width , sRect.height);
            if (jsb && jsb["saveImageData"] && jsb["saveImageData"](sData, sRect.width, sRect.height, fullPath) == true)
            {
                console.log("截图成功[" + sRect.width.toString() + "x" + sRect.height.toString() + "]");
            }
            else
            {
                bResult = false;
                console.log("截图失败[" + sRect.width.toString() + "x" + sRect.height.toString() + "]");
            }
        }

        // 回调函数
        if (callBack)
        {
            callBack.call(target , bResult , fullPath , sData , sRect.width , sRect.height);
        }
    }
           

继续阅读