使用 Phonegap 在 iOS 上的相机预览上叠加图像

发布于 2024-12-03 01:31:57 字数 194 浏览 1 评论 0原文

我正在创建一个应用程序,其中半透明图像叠加在相机预览上。我知道他们的原生 Phonegap 相机 api 不支持这一点。我想知道是否有编写 Phonegap 插件经验的人可以给我任何关于插件是否可以实现这一点的建议。我想我已经看到这种技术可以通过本机代码实现,所以在我看来,可以编写一个 Phonegap 插件来访问此功能,我只是没有任何使用 Phonegap 插件的经验。

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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无名指的心愿 2024-12-10 01:31:57

我知道,有点晚了,但有办法(仅限 iPad)。您可以使用标准的 org.apache.cordova.camera 插件。但你必须稍微调整一下它

首先将 CDVCameraPicker 子类化,以便你可以通过 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

然后将这些行添加到 CDVCamera。 m 检查完 ImagePickerSourceType(第 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"];
    }

不要忘记在 CDVCamera.m 中导入您的子类 CameraPicker
#import "CDVCameraPicker+Overlay.h"

不,您必须编辑 Camera.js 文件
将此行添加到其他选项下方

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

,然后将 var 添加到 args-Array 的最后一个索引处。就是这样。现在您可以像这样切换叠加层:

       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 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
                    }); 
孤千羽 2024-12-10 01:31:57

我尝试了接受的答案,但不幸的是对我不起作用。

相反,我找到了一个更简单的解决方案。在 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
   }
}

使用 Grand Central Dispatch 执行叠加非常重要。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文