Skip to content
This repository was archived by the owner on Sep 26, 2021. It is now read-only.

Adding the ability to post process the screenshot. For example, one may ... #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/android/Screenshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

public class Screenshot extends CordovaPlugin {

public static interface ProcessingIntf {
public Bitmap postProcess(Bitmap screenshot);
}

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
// starting on ICS, some WebView methods
Expand All @@ -42,6 +46,11 @@ public void run() {
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

if (cordova.getActivity() instanceof Screenshot.ProcessingIntf) {
bitmap = ((Screenshot.ProcessingIntf)cordova.getActivity()).postProcess(bitmap);
}

File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
if (!folder.exists()) {
folder.mkdirs();
Expand All @@ -57,7 +66,7 @@ public void run() {
bitmap.compress(Bitmap.CompressFormat.JPEG, quality == null?100:quality, fos);
}
JSONObject jsonRes = new JSONObject();
jsonRes.put("filePath",f.getAbsolutePath());
jsonRes.put("filePath","file://" + f.getAbsolutePath());
PluginResult result = new PluginResult(PluginResult.Status.OK, jsonRes);
callbackContext.sendPluginResult(result);
}else{
Expand Down
6 changes: 6 additions & 0 deletions src/ios/Screenshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
#import <QuartzCore/QuartzCore.h>
#import <Cordova/CDVPlugin.h>

@protocol ScreenshotPluginDelegate <NSObject>
- (UIImage *)postProcessScreenShot:(UIImage *)screenshot;
@end

@interface Screenshot : CDVPlugin {
}

//- (void)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options;
- (void)saveScreenshot:(CDVInvokedUrlCommand*)command;

@property (nonatomic, weak) id<ScreenshotPluginDelegate> screenshotDelegate;

@end
20 changes: 16 additions & 4 deletions src/ios/Screenshot.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ @implementation Screenshot

@synthesize webView;

- (void)pluginInitialize
{
[super pluginInitialize];
if ([self.viewController conformsToProtocol:@protocol(ScreenshotPluginDelegate)])
self.screenshotDelegate = (id<ScreenshotPluginDelegate>)self.viewController;
}

//- (void)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options

- (void)saveScreenshot:(CDVInvokedUrlCommand*)command
Expand Down Expand Up @@ -51,13 +58,18 @@ - (void)saveScreenshot:(CDVInvokedUrlCommand*)command
CGContextTranslateCTM(ctx, 0, 0);
CGContextFillRect(ctx, imageRect);

[webView.layer renderInContext:ctx];
[webView.layer renderInContext:ctx];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(image,[quality floatValue]);
[imageData writeToFile:jpgPath atomically:NO];

UIGraphicsEndImageContext();
UIGraphicsEndImageContext();

UIImage *postProcessedImage = [self.screenshotDelegate postProcessScreenShot:image];
if (!postProcessedImage)
postProcessedImage = image;

NSData *imageData = UIImageJPEGRepresentation(postProcessedImage, [quality floatValue]);
[imageData writeToFile:jpgPath atomically:NO];

CDVPluginResult* pluginResult = nil;
NSDictionary *jsonObj = [ [NSDictionary alloc]
Expand Down