Cordova iApp浏览器的屏幕截图?

Cordova iApp浏览器的屏幕截图?,cordova,screenshot,inappbrowser,webpage-screenshot,Cordova,Screenshot,Inappbrowser,Webpage Screenshot,我想用PhoneGap创建InApp浏览器的屏幕截图。 我找了很多,但什么也没找到。这可能吗 我的目标是:iOS和Android这个PhoneGap插件应该可以帮助您: cordova屏幕截图 用法: navigator.screenshot.save(function(error,res){ if(error){ console.error(error); }else{ console.log('ok',res.filePath); } }); 如果您想拍摄主窗口的

我想用PhoneGap创建InApp浏览器的屏幕截图。 我找了很多,但什么也没找到。这可能吗


我的目标是:iOS和Android

这个PhoneGap插件应该可以帮助您:

cordova屏幕截图

用法:

navigator.screenshot.save(function(error,res){
  if(error){
  console.error(error);
  }else{
    console.log('ok',res.filePath);
  }
});
如果您想拍摄主窗口的屏幕截图,可以使用。屏幕截图将保存在应用程序存储中,因此您不需要访问设备库

但是如果你用这个打开一个新窗口

var ref = window.open('http://www.yoursubwindow.com', '_blank')
然后您将无法拍摄
ref
的屏幕截图

原因:
ref
不是一个普通的Javascript浏览器窗口,而是一个对浏览器窗口访问受限的
InAppBrowser
对象。特别是,您将无法访问Javascript
window.navigator
对象,该对象在cordova屏幕截图插件中用于截图

因此,如果您使用
window.navigator.screenshot.save(…)
截图,它将始终截图底层窗口(主窗口),但决不会截图
ref

ref.navigator.screenshot.save(…)
将导致javascript错误,因为未定义
ref.navigator


我现在正在自己解决这个问题,但我还没有找到解决办法。如果你找到了,就告诉我

如其在评论中所述,cordova屏幕截图插件在inAppBrowser中无法正常工作。但您仍然可以在inAppBrowser插件代码中实现屏幕截图。stackoverflow上已有多个解决方案。我为Android使用了以下代码(在appbrowser.java代码中编辑):

//在我的代码中,postmessage会触发功能,因此在某些postmessage中,它会将对象作为postmessage返回到cordova,我在那里捕获并使用数据:image

public void screenShare(JSONObject obj) {
       final WebView childView = inAppWebView;
       Bitmap bitmap = Bitmap.createBitmap(childView.getWidth(), childView.getHeight(), Bitmap.Config.ARGB_8888);
       Canvas canvas = new Canvas(bitmap);
       childView.draw(canvas);
       int quality = 100;
       ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
       if (bitmap.compress(CompressFormat.JPEG, quality, jpeg_data)) {
           byte[] code = jpeg_data.toByteArray();
           byte[] output = Base64.encode(code, Base64.NO_WRAP);
           String js_out = new String(output);
           js_out = "data:image/jpeg;base64," + js_out;
           try {
               obj.put("screen", js_out);
               LOG.d(LOG_TAG, "screen result sending" );
               sendUpdate(obj, true);
           }catch (JSONException ex) {
               LOG.e(LOG_TAG, "data screenshare object passed to postMessage has caused a JSON error.");
           }
       } else{
           LOG.d(LOG_TAG, "No screen result " );
       }
   }
对于iOs,我编辑了CDVUIInappBrowser.m 我还将postmessage用于功能触发器,并使用了以下代码:

//调用功能:

NSString *imageStr = [self getScreenshot5];
//返回数据:图像

- (NSString *)getScreenshot5
{
    UIImage *viewImage = [self captureScreen:self.inAppBrowserViewController.view];
    // For error information
        NSError *error;
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/IMG_FOLDER"];

        if (![fileMgr fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

        //Get the current date and time and set as image name
        NSDate *now = [NSDate date];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd_HH-mm-ss";
        [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
        NSString *gmtTime = [dateFormatter stringFromDate:now];
        NSLog(@"The Current Time is :%@", gmtTime);

        NSData *imageData = UIImageJPEGRepresentation(viewImage, 0.9); // _postImage is your image file and you can use JPEG representation or PNG as your wish
        int imgSize = imageData.length;
        NSLog(@"SIZE OF IMAGE: %.2f Kb", (float)imgSize/1024);

        NSString *imgfileName = [NSString stringWithFormat:@"%@%@", gmtTime, @".jpg"];
         NSString *imgfilePath= [dataPath stringByAppendingPathComponent:imgfileName];
        return imgfilePath;
}

-(UIImage*)captureScreen:(UIView*) viewToCapture
{
    UIGraphicsBeginImageContext(viewToCapture.bounds.size);
    [viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

这在iOS中是如何工作的?应用程序无法访问照片的地方?你是在开发者应用程序中尝试,还是部署到你的设备中?我将其部署在iPad Air上。只要你想从主窗口截图,该插件就适用于iOS(以及任何其他操作系统)。但是拍摄InAppBrowser窗口的屏幕截图是不起作用的。你找到了相关的东西吗?你想展示一下如何使用postmessage从Javascript触发ScreenShareJava方法吗?