Crosswalk下cordova-screenshot截屏生成黑屏图片

cordova-screenshot插件
XWALK-2233

在Crosswalk(版本10.39.235.16)下使用cordova-screenshot插件截屏,生成的图片为黑屏,与其说是Crosswalk的问题,不如说是cordova-screenshot插件的BUG。

从BUG 2233下的讨论,发现Tony Homer已经解决了黑屏的问题,参考他的代码,修改getBitmap()方法,如下:

public class Screenshot extends CordovaPlugin {
    
    private Bitmap getBitmap() {
        Bitmap bitmap = null;
        
        boolean isCrosswalk = false;
        try {
//            Class.forName("org.crosswalk.engine.XWalkWebViewEngine");
            Class.forName("org.xwalk.core.XWalkView"); // 这里
            isCrosswalk = true;
        } catch (Exception e) {
        }
        
        if(isCrosswalk) {
            try {
                
//                TextureView textureView = findXWalkTextureView((ViewGroup)webView.getView());
                TextureView textureView = findXWalkTextureView((ViewGroup)webView); // 这里
                if (textureView != null) {
                    bitmap = textureView.getBitmap();
                    return bitmap;
                }
            } catch(Exception e) {
            }
        } 

//            View view = webView.getView().getRootView();
            View view = webView.getRootView(); // 这里
        view.setDrawingCacheEnabled(true);
        bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);

        
        return bitmap;
    }
}

另外在config.xml中添加:


修改Screentshot.js,原JS文件没有设置插件ID,现在将其ID设置为com.darktalker.cordova.screenshot.ScreenshotPlugin

cordova.define("com.darktalker.cordova.screenshot.ScreenshotPlugin", function (require, exports, module) {

    var exec = require('cordova/exec'),
        cordova = require('cordova'), formats = ['png','jpg'];

    function ScreenshotPlugin() {
    }

    ScreenshotPlugin.prototype = {
            save:function(callback,format,quality, filename) {
                format = (format || 'png').toLowerCase();
                filename = filename || 'screenshot_'+Math.round((+(new Date()) + Math.random()));
                if(formats.indexOf(format) === -1){
                    return callback && callback(new Error('invalid format '+format));
                }
                quality = typeof(quality) !== 'number'?100:quality;
                exec(function(res){
                    callback && callback(null,res);
                }, function(error){
                    callback && callback(error);
                }, "Screenshot", "saveScreenshot", [format, quality, filename]);
            },

            URI:function(callback, quality){
                quality = typeof(quality) !== 'number'?100:quality;
                exec(function(res){
                    callback && callback(null, res);
                }, function(error){
                    callback && callback(error);
                }, "Screenshot", "getScreenshotAsURI", [quality]);

            }
    }

    module.exports = new ScreenshotPlugin();

});

调用插件截屏保存图片:

navigator.screenshot.save(function(error,res){
    if(error){
        console.error(error);
    }else{
        console.log('ok',res.filePath); //should be path/to/myScreenshot.jpg
    }
},'jpg',50,'myScreenShot');

你可能感兴趣的:(Crosswalk下cordova-screenshot截屏生成黑屏图片)