我想创建一个屏幕截图的InAppBrowser与PhoneGap。我找了很多,但什么也没找到。这有可能吗?
我的目标是: iOS和安卓
发布于 2014-10-01 19:01:11
如果你想截取主窗口的屏幕截图,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,ref.navigator.screenshot.save(...)将导致javascript错误。
我现在正在自己解决这个问题,但我还没有想出一个解决方案。所以如果你找到了,让我知道!
发布于 2014-09-23 21:35:25
这个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);
}
});发布于 2019-04-16 21:59:37
正如它在评论中所写的那样,cordova-screenshot插件在inAppBrowser上不能正常工作。但是你仍然可以在inAppBrowser插件代码中实现截图。堆栈溢出上已经有几种解决方案。我在Android上使用了以下代码(编辑过的InAppBrowser.java代码):
//在我的代码中,code消息触发了该功能,因此在一些code消息中,它将对象作为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];//返回数据:image
- (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;
}https://stackoverflow.com/questions/25996256
复制相似问题