首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InAppBrowser的截图?

InAppBrowser的截图?
EN

Stack Overflow用户
提问于 2014-09-23 21:22:33
回答 3查看 1.4K关注 0票数 0

我想创建一个屏幕截图的InAppBrowser与PhoneGap。我找了很多,但什么也没找到。这有可能吗?

我的目标是: iOS和安卓

EN

回答 3

Stack Overflow用户

发布于 2014-10-01 19:01:11

如果你想截取主窗口的屏幕截图,cordova screenshot plugin可以帮助你。屏幕截图将保存在应用程序存储中,因此您不需要访问设备库。

但是如果您使用InAppBrowser Plugin打开一个新窗口,如下所示

代码语言:javascript
复制
var ref = window.open('http://www.yoursubwindow.com', '_blank')

然后你就不能截取ref的屏幕截图了。

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

所以如果你用window.navigator.screenshot.save(...)截图,它总是会截取底层窗口(主窗口)的截图,但不会截取ref的截图。

由于未定义ref.navigatorref.navigator.screenshot.save(...)将导致javascript错误。

我现在正在自己解决这个问题,但我还没有想出一个解决方案。所以如果你找到了,让我知道!

票数 2
EN

Stack Overflow用户

发布于 2014-09-23 21:35:25

这个PhoneGap插件可以帮助你:

cordova-screenshot

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

用法:

代码语言:javascript
复制
navigator.screenshot.save(function(error,res){
  if(error){
  console.error(error);
  }else{
    console.log('ok',res.filePath);
  }
});
票数 0
EN

Stack Overflow用户

发布于 2019-04-16 21:59:37

正如它在评论中所写的那样,cordova-screenshot插件在inAppBrowser上不能正常工作。但是你仍然可以在inAppBrowser插件代码中实现截图。堆栈溢出上已经有几种解决方案。我在Android上使用了以下代码(编辑过的InAppBrowser.java代码):

//在我的代码中,code消息触发了该功能,因此在一些code消息中,它将对象作为postmessage返回给cordova,我在那里捕获并使用数据:image

代码语言:javascript
复制
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作为功能触发器,我使用了以下代码:

//功能调用:

代码语言:javascript
复制
NSString *imageStr = [self getScreenshot5];

//返回数据:image

代码语言:javascript
复制
- (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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25996256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档