0

我想用 PhoneGap 创建 InAppBrowser 的屏幕截图。我已经搜索了很多,但我什么也没找到。这甚至可能吗?

我的目标是:iOS 和 Android

4

3 回答 3

2

如果您想截取主窗口的屏幕截图,cordova 屏幕截图插件可为您提供帮助屏幕截图将保存在应用程序存储中,因此您无需访问设备库。

但是如果你使用InAppBrowser 插件打开一个像这样的新窗口

var ref = window.open('http://www.yoursubwindow.com', '_blank')

那么您将无法截取ref.

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

因此,如果您使用 截屏window.navigator.screenshot.save(...),它将始终截取底层窗口(主窗口)的截屏,但绝不截取ref.

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

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

于 2014-10-01T11:01:11.210 回答
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);
  }
});
于 2014-09-23T13:35:24.893 回答
0

正如其在评论中所写的那样,cordova-screenshot 插件无法与 inAppBrowser 一起正常工作。但是您仍然可以在 inAppBrowser 插件代码中实现屏幕截图。stackoverflow 上已经有几种解决方案。我为 Android 使用了以下代码(编辑的 InAppBrowser.java 代码):

//在我的代码中,postmessage 会触发功能,因此在某些 postmessage 中,它将对象作为 postMessage 返回到我正在捕获并使用数据的科尔多瓦:图像

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;
}
于 2019-04-16T13:59:36.887 回答