gpt4 book ai didi

javascript - Cordova -拍照

转载 作者:行者123 更新时间:2023-12-03 10:56:39 27 4
gpt4 key购买 nike

所以我对 Cordova 还很陌生,现在我正在使用它来制作一个应用程序,我正在尝试拍照然后保存它。现在我还不能拍照。我所拥有的只是一个按钮,在相应的 javascript 文件中,按下按钮时会调用一个函数。我首先运行“cordova plugin add org.apache.cordova.camera”。这是我尝试过的:

document.addEventListener('deviceready', onDeviceReady);

function onDeviceReady() {

$('#picture').on('click',function() {
takePicture();
}

function takePicture() {

if (!navigator.camera) {
alert("Camera API not supported", "Error");
return;
}

navigator.camera.getPicture(function(imageURI) {
alert("test");
}, function(err) {
alert("Inside err");
}, cameraOption);

/*navigator.camera.getPicture(function(imagePath) {
alert("test");
document.getElementByID("photoImg").setAttribute("src", imagePath);
alert("Got picture");
}, function() {
alert("Photo canceled");
}, {
destinationType: navigator.camera.DestinatonType.FILE_URI
});*/


}
}

我从 http://learn.ionicframework.com/formulas/cordova-camera/ 得到了这个函数我还从另一个地方尝试了第二个,但相机在任何一个中都没有出现。另外,我在里面插入了一些警报,但没有出现,所以我有点困惑。

有人对如何让相机显示有任何建议吗?

最佳答案

首先确保您在文档就绪回调中调用代码,因此:

$(document).ready(function () {
$('#picture').on('click',function() {
takePicture();
}
});

您没有显示相机选项,这里有一些适合我的代码。

function onSuccess(imageURI) {
// This will show the photo in an <img> element with id="photo"
jQuery('#photo').attr('src', imageURI).show();
// Do more stuff here
}

var folder = true; // set to false to take with camera, true to select from library
navigator.camera.getPicture(onSuccess, onFail, {
quality: 25, // 25%
// takes a photo that will fit precisely within an iPhone 6 Plus screen
targetWidth: 1080,
targetHeight: 960,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: folder ? Camera.PictureSourceType.PHOTOLIBRARY : Camera.PictureSourceType.CAMERA
});

关于javascript - Cordova -拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28248684/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com