开发者

Overlaying an image on camera preview on iOS using Phonegap

开发者 https://www.devze.com 2023-04-01 03:10 出处:网络
I am looking to create an app in which a semi transparent image is overlaid on the camera preview. I know their is no support for this in the native Phonegap camera api. 开发者_JAVA技巧I\'m wondering

I am looking to create an app in which a semi transparent image is overlaid on the camera preview. I know their is no support for this in the native Phonegap camera api. 开发者_JAVA技巧I'm wondering if anyone who has some experience with writing Phonegap plugins can give me any advice on whether this would be possible with a plugin. I think I have seen that this technique is possible through native code so it seems to me that it would be possible to write a Phonegap plugin to access this functionality, I just don't have any experience with Phonegap plugins.


I know, it's a little bit too late but there is a way (iPad only). You can use the standard org.apache.cordova.camera-Plugin. But you have to tweak it a bit

First subclasse the CDVCameraPicker so you can toggle the overlay via cordova-api:

CDVCameraPicker+Overlay.h:

#import "CDVCamera.h"

@interface CDVCameraPicker (Overlay)
@property (nonatomic, strong) id showOverlay;
@end

CDVCameraPicker+Overlay.m:

#import "CDVCameraPicker+Overlay.h"
#import <objc/runtime.h>

static void *overlayKey;

@implementation CDVCameraPicker (Overlay)
@dynamic showOverlay;

- (void) setShowOverlay:(id)showOverlay {
    objc_setAssociatedObject(self, overlayKey, showOverlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id) showOverlay {
    return objc_getAssociatedObject(self, overlayKey);
}
@end

Then add these lines to the CDVCamera.m right after checking the ImagePickerSourceType (Line 132)

    if ([cameraPicker.showOverlay intValue] == 1) {
        UIImageView *overlay = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
        overlay.contentMode = UIViewContentModeScaleAspectFill;
        overlay.image = [UIImage imageNamed:@"overlay.png"];
    }

Don't forget to import your subclassed CameraPicker in the CDVCamera.m #import "CDVCameraPicker+Overlay.h"

No you have to edit the Camera.js-File Add this line underneath the other options

var showOverlay = getValue(options.showOverlay, false);

Then add the var to the args-Array at last index. And that's it. Now you can toggle your overlay like this:

       navigator.camera.getPicture(onSuccess, onFail, { quality: 40,
                        destinationType: Camera.DestinationType.FILE_URI,
                        sourceType: Camera.PictureSourceType.CAMERA,
                        encodingType: Camera.EncodingType.JPEG,
                        correctOrientation: true,
                        saveToPhotoAlbum: true,
                        showOverlay: false
                    }); 


I tried the accepted answer, but unfortunately did not work for me.

Instead I found a much simpler solution. In the CDVCamera.m:

  + (instancetype) createFromPictureOptions(CDVPictureOptions*)pictureOptions;{

      CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
      cameraPicker.pictureOptions = pictureOptions;
      cameraPicker.sourceType = pictureOptions.sourceType;
      cameraPicker.allowsEditing = pictureOptions.allowsEditing;

if (cameraPicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
    // More code...

         // Here is where you place your overlay.
         dispatch_async(dispatch_get_main_queue(), ^{
            UIImageView *overlayImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];
            overlayImage.frame = CGRectMake(0, 0, 768, 1024);
            [cameraPicker.cameraOverlayView addSubview:overlayImage];
            [cameraPicker setCameraOverlayView:overlayImage];
     });

   //  Code left out for brevity
   }
}

It's important to perform the overlay using Grand Central Dispatch.

0

精彩评论

暂无评论...
验证码 换一张
取 消