InAppBrowser的屏幕截图?

时间:2014-09-23 13:22:33

标签: cordova screenshot inappbrowser webpage-screenshot

我想用PhoneGap创建InAppBrowser的屏幕截图。 我搜索了很多,但我一无所获。这甚至可能吗?

我的目标是:iOS和Android

3 个答案:

答案 0 :(得分:2)

如果您想截取主窗口的屏幕截图,cordova screenshot plugin会对您有所帮助。屏幕截图将保存在应用程序存储中,因此您无需访问设备库。

但是如果您使用InAppBrowser Plugin打开一个像这样的新窗口

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未定义。

我现在正在研究这个问题,但我还没有想出解决方案。所以如果你找到了,请告诉我!

答案 1 :(得分:0)

此PhoneGap插件可以帮助您:

cordova-screenshot

https://github.com/gitawego/cordova-screenshot

用法:

navigator.screenshot.save(function(error,res){
  if(error){
  console.error(error);
  }else{
    console.log('ok',res.filePath);
  }
});

答案 2 :(得分:0)

如其注释中所述,cordova-screenshot插件无法与inAppBrowser一起正常使用。但是您仍然可以在inAppBrowser插件代码中实现屏幕截图。 stackoverflow已经有几种解决方案。我将以下代码用于Android(已编辑的InAppBrowser.java代码):

///在我的代码中,postmessage触发了功能,因此在某些postmessage上,它以postMessage的形式将对象返回到我正在捕获并使用data:image

的cordova
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 " );
       }
   }

对于iO,我编辑了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;
}