检查 ios PhoneGap 中相机是否存在

发布于 2024-12-09 06:40:22 字数 176 浏览 0 评论 0原文

在我的 phonegap 应用程序中拍照时,我想在显示两种源类型之前检查相机是否存在。例如,iPad 1 没有相机,因此我不想显示从相机我的照片中选择源类型的弹出窗口。 phonegap 中是否有某些内容告诉我该设备中是否存在相机?

I want to check camera existence before showing the two source types when taking a picture in my phonegap application. For example, iPad 1 doesn't have an Camera, therefore I don't want to show the popup to select source type from Camera and My Photos. Is there something in phonegap that tell me camera exists in this device or not?

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

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

发布评论

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

评论(6

指尖上的星空 2024-12-16 06:40:22

目前,似乎没有办法查询相机是否存在。

这并不理想,但对您来说可能是一个安全保障。如果设备没有摄像头,对 getPicture 的调用将落入失败处理程序,该处理程序返回一条消息。当相机不可用时,该消息是:“没有可用的相机”。因此,您可以处理该故障一次,然后将某些内容保留在用户设置中,以便您以后可以查询。就像我说的,并不理想。如果 API 可以报告此特定故障,那么它还应该提供一种检查是否存在的方法。

失败:函数(消息){
if (message == "没有可用的相机") {
// 将其保存在某处,以便下次我们不必依赖失败处理程序来告诉我们相机不存在
}
}

Currently, there doesn't appear to be a way to query camera existence.

This is not ideal, but it could be a fail safe for you. If the device doesn't have a camera, a call to getPicture will fall into the fail handler, which returns a message. When the camera isn't available, that message is: "no camera available". So you could handle that failure once, then persist something in user settings, which you could query going forward. Like I said, not ideal. If the API can report this specific failure, it should also provide a way to check existence.

fail: function (message) {
if (message == "no camera available") {
// save this somewhere so next time we don't have to rely on the fail handler to tell us the camera doesn't exist
}
}

蝶…霜飞 2024-12-16 06:40:22

我需要这样做,所以我将其添加到我制作的用于执行各种任务的插件中。目前只有 iOS 版本。

TomPhonegapUtility.h

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>
@interface TomPhonegapUtility : CDVPlugin
- (void) isCameraAvailable:(CDVInvokedUrlCommand*)command;
@end

TomPhonegapUtility.m

#import "TomPhonegapUtility.h"
#import <Cordova/CDV.h>
@implementation TomPhonegapUtility
- (void) isCameraAvailable:(CDVInvokedUrlCommand*)command {
    CDVPluginResult *pluginResult = nil;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:0];
    }
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end

TomPhonegapUtility.js

function TomPhoneGapUtility () {
    this.isCameraAvailable = function(successCallback) {
        cordova.exec(successCallback, function(){}, "TomPhonegapUtility", "isCameraAvailable", []);
    }
}

如何使用

var util = new TomPhoneGapUtility();
util.isCameraAvailable(function(hasCamera) {
    if (hasCamera) alert("YES");
    else alert("NO");
});

I needed to do just this, so I added it to a plug in that I made to do various tasks. Only iOS versions so far.

TomPhonegapUtility.h

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>
@interface TomPhonegapUtility : CDVPlugin
- (void) isCameraAvailable:(CDVInvokedUrlCommand*)command;
@end

TomPhonegapUtility.m

#import "TomPhonegapUtility.h"
#import <Cordova/CDV.h>
@implementation TomPhonegapUtility
- (void) isCameraAvailable:(CDVInvokedUrlCommand*)command {
    CDVPluginResult *pluginResult = nil;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:0];
    }
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end

TomPhonegapUtility.js

function TomPhoneGapUtility () {
    this.isCameraAvailable = function(successCallback) {
        cordova.exec(successCallback, function(){}, "TomPhonegapUtility", "isCameraAvailable", []);
    }
}

How to use

var util = new TomPhoneGapUtility();
util.isCameraAvailable(function(hasCamera) {
    if (hasCamera) alert("YES");
    else alert("NO");
});
笨死的猪 2024-12-16 06:40:22

您可以从 UIDevice 类获取设备型号字符串并检查

You can get the device model string from UIDevice class and check this

甲如呢乙后呢 2024-12-16 06:40:22

应该能够调用此代码来检查任何设备上是否存在相机。

if (typeof navigator.camera !== "undefined") { 
  // We are safe to use the camera 
} else { 
  // Bad news no camera 
} 

Should be able to call this code to check for existence of a camera on any device.

if (typeof navigator.camera !== "undefined") { 
  // We are safe to use the camera 
} else { 
  // Bad news no camera 
} 
不奢求什么 2024-12-16 06:40:22

当尝试使用 HTML5 方式拍照时,我做了类似的事情:

if (typeof navigator.device !== 'undefined' && typeof navigator.device.capture !== 'undefined' && typeof navigator.device.capture.captureImage !== 'undefined') {
   // Can take a picture
} else {
   // No camera
}

请记住,如果您在 IOS 模拟器上使用过此解决方案或前一个解决方案,它会给您一个可靠的答案,模拟器似乎会返回它有一个相机,但无法模拟拍照,并且会返回错误 20 ...

我对 if 语句不满意,如果有人知道如何以更合乎逻辑的方式编写它,请让我我知道,我的 JS 知识有限。

I have done something like this when trying to use the HTML5 way of taking a picture :

if (typeof navigator.device !== 'undefined' && typeof navigator.device.capture !== 'undefined' && typeof navigator.device.capture.captureImage !== 'undefined') {
   // Can take a picture
} else {
   // No camera
}

Keep in mind that if you have used this solution or the previous one on the IOS simulator, it will give you a reliable answer, the simulator seem to return that it has a camera but is not able to simulate the taking of a picture and will return an error 20 ...

I am not happy with the if statement, if anybody know how to write this in a more logical way, please let me know, my JS knowhow is limited.

挖鼻大婶 2024-12-16 06:40:22

我认为这很好地解释了这一点,如果您还没有找到答案: http://docs.phonegap.com/en/1.4.1/phonegap_camera_camera.md.html#Camera

I think this explains it well, if you haven't found the answer as of yet: http://docs.phonegap.com/en/1.4.1/phonegap_camera_camera.md.html#Camera

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