2014-11-24 122 views
1

我正在爲cocos2d-JS上的Facebook創建遊戲,其中需要共享遊戲的屏幕截圖。如何從RenderTexture獲取byteArray或base64字符串Cocos2d-JS

我能夠截取屏幕截圖,但現在無法將其上傳到Parse.com服務器,因爲它需要base64格式或字節數組。我無法找到任何將Sprite轉換爲此格式的解決方案..這裏是我的代碼,因此當我添加它的即將到來的適當的結果時。我還添加了我的已評論的代碼,以便它可以幫助理解我已經嘗試了很多事情,但不能實現相同。

shareToSocialNetworking: function() { 

    cc.director.setNextDeltaTimeZero(true); 

    var newsize = cc.director.getVisibleSize(); 

    var renderText = new cc.RenderTexture(newsize.width,newsize.height); 

    renderText.begin(); 
    cc.director.getRunningScene().visit(); 
    renderText.end(); 
    var result = cc.Sprite.create(renderText.getSprite().getTexture()); 
    result.flippedY = true; 
    this._mainViewNode.addChild(result,6000); 

    //renderText.saveToFile("screenshot.jpg",cc.IMAGE_FORMAT_PNG); 
    //var based = renderText.getSprite().getTexture().getStringForFormat().toString(); 
    //var data = based.getData(); 
    var file = new Parse.File("screen.jpg", { base64: this.getBase64(result) }); 
    //var file = new Parse.File("screen.jpg", data, "image/png"); 
    var self = this; 

    file.save().then(function() { 
     // The file has been saved to Parse. 
     alert(file.url); 
    this.onSharePictureInfoLink(file.url()); 

    }, function(error) { 
     // The file either could not be read, or could not be saved to Parse. 
    }); 


    // 
    //var ccImage = renderText.newCCImage(); 
    // 
    //var str = ccImage.getData(); 

}, 

有沒有可以做什麼解決辦法

+0

你可以試試這個: - https://stackoverflow.com/questions/44520608/how-to- get-sprite-pixel-alpha-information-in -cocos2d -js -c/45030826#45030826 – 2017-07-17 11:54:12

+0

你可以試試這個: - https://stackoverflow.com/questions/44520608/how-to-get-sprite-pixel- alpha-information-in-cocos2d-js-c/45030826#45030826 – 2017-07-17 11:55:23

回答

1

有一個私有變量叫_cacheCanvas,這是離屏畫布的實例

,你可以簡單地做renderText._cacheCanvas.toDataURL()

+0

有趣!這是否跨平臺工作?或者,還有另一種方法可以捕獲在Android上運行的cocos2d-js應用程序中的屏幕截圖嗎? – 2014-11-25 13:12:14

+0

這是html5 API,因此它不適用於本機應用程序的JSB。 – 2014-11-26 07:24:45

+0

這太糟糕了......您是否知道用-js製作的原生應用截圖? – 2014-11-29 02:40:52

1

這裏是如何你可以從cocos2d-JS取得骷髏圖片

screenshot: function (fileName) { 
     var tex = new cc.RenderTexture(winSize.width, winSize.height, cc.Texture2D.PIXEL_FORMAT_RGBA8888); 
     tex.setPosition(cc.p(winSize.width/2, winSize.height/2)); 
     tex.begin(); 
     cc.director.getRunningScene().visit(); 
     tex.end(); 

     var imgPath = jsb.fileUtils.getWritablePath(); 
     if (imgPath.length == 0) { 
      return; 
     } 
     var result = tex.saveToFile(fileName, cc.IMAGE_FORMAT_JPEG); 
     if (result) { 
      imgPath += fileName; 
      cc.log("save image:" + imgPath); 
      return imgPath; 
     } 
     return ""; 
    } 

然後從Javascript

使Java調用
public static void ScreenShot() 
    { 
     Bitmap imageBitmap = BitmapFactory.decodeFile(Cocos2dxHelper.getCocos2dxWritablePath() + "/" + "screenshot.png"); 

     String fileHolder = "SampleFolder"; 
     File filepathData = new File("/sdcard/" + fileHolder); 

     //~~~Create Dir 
     try { 
       if (!filepathData.exists()) 
       { 
        filepathData.mkdirs(); 
        filepathData.createNewFile(); 

        FileWriter fw = new FileWriter(filepathData + fileHolder); 
        BufferedWriter out = new BufferedWriter(fw); 
        String toSave = String.valueOf(0); 
        out.write(toSave); 
        out.close(); 
       } 
      } 
      catch (IOException e1) { 

      } 

      //~~~Create Image 
      File file = new File("/sdcard/" + "Your filename"); 

      try 
      { 
       file.createNewFile(); 
       FileOutputStream ostream = new FileOutputStream(file); 
       imageBitmap.compress(CompressFormat.PNG, 100, ostream); 
       ostream.close(); 
      } 
      catch (Exception e) {} 

      Uri phototUri = Uri.fromFile(file); 
      Intent shareIntent = new Intent(); 
      shareIntent.setAction(Intent.ACTION_SEND); 
      shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri); 
      //~~~Add Code Below 
    } 

不要忘記添加權限外部存儲