InAppBrowser的屏幕截图?

问题描述 投票:0回答:3

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

我的目标是:iOS和Android

cordova screenshot inappbrowser webpage-screenshot
3个回答
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

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


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);
  }
});

0
投票

正如其在评论中所写,cordova-screenshot插件无法正常使用inAppBrowser。但您仍然可以在inAppBrowser插件代码中实现屏幕截图。 stackoverflow上已经有几种解决方案。我使用以下Android代码(编辑的InAppBrowser.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;
}
© www.soinside.com 2019 - 2024. All rights reserved.