Javascript Phonegap(3.0.0)摄像头首次尝试时未成功
出于测试目的,我复制了在上找到的完整示例,并在Javascript Phonegap(3.0.0)摄像头首次尝试时未成功,javascript,android,cordova,camera,Javascript,Android,Cordova,Camera,出于测试目的,我复制了在上找到的完整示例,并在onPhotoDataSuccess上放置了一个警报,以在函数启动时进行测试。在拍摄的第一张照片上,警报不会显示。但是,第一次尝试后,保存照片后将显示警报 有什么建议吗?如果有什么不清楚的地方,我很乐意说得更具体一些 我在我的Android Galaxy S3上测试了下面的代码 <!DOCTYPE html> <html> <head> <title>Capture Photo<
onPhotoDataSuccess
上放置了一个警报,以在函数启动时进行测试。在拍摄的第一张照片上,警报不会显示。但是,第一次尝试后,保存照片后将显示警报
有什么建议吗?如果有什么不清楚的地方,我很乐意说得更具体一些
我在我的Android Galaxy S3上测试了下面的代码
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for device API libraries to load
//
document.addEventListener("deviceready",onDeviceReady,false);
// device APIs are available
//
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64-encoded image data
// console.log(imageData);
// Get image handle
//
var smallImage = document.getElementById('smallImage');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallImage.src = "data:image/jpeg;base64," + imageData;
}
// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
// Unhide image elements
//
largeImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
largeImage.src = imageURI;
}
// A button will call this function
//
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.DATA_URL });
}
// A button will call this function
//
function capturePhotoEdit() {
// Take picture using device camera, allow edit, and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,
destinationType: destinationType.DATA_URL });
}
// A button will call this function
//
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>
拍摄照片
var pictureSource;//图像源
var destinationType;//设置返回值的格式
//等待加载设备API库
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//设备API可用
//
函数ondevicerady(){
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.destinationType;
}
//成功检索照片时调用
//
函数onPhotoDataSuccess(imageData){
//取消注释以查看base64编码的图像数据
//控制台日志(imageData);
//获取图像句柄
//
var smallImage=document.getElementById('smallImage');
//取消隐藏图像元素
//
smallImage.style.display='block';
//显示捕获的照片
//内联CSS规则用于调整图像大小
//
smallImage.src=“数据:图像/jpeg;base64,”+imageData;
}
//成功检索照片时调用
//
函数onPhotoURISuccess(imageURI){
//取消注释以查看图像文件URI
//log(imageURI);
//获取图像句柄
//
var largeImage=document.getElementById('largeImage');
//取消隐藏图像元素
//
largeImage.style.display='block';
//显示捕获的照片
//内联CSS规则用于调整图像大小
//
largeImage.src=imageURI;
}
//一个按钮将调用此函数
//
函数capturePhoto(){
//使用设备摄像头拍照,并将图像作为base64编码字符串检索
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{质量:50,
destinationType:destinationType.DATA_URL});
}
//一个按钮将调用此函数
//
函数capturePhotoEdit(){
//使用设备照相机拍照,允许编辑,并以base64编码字符串的形式检索图像
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{quality:20,allowEdit:true,
destinationType:destinationType.DATA_URL});
}
//一个按钮将调用此函数
//
函数getPhoto(源代码){
//从指定源检索图像文件位置
navigator.camera.getPicture(onPhotoURISuccess,onFail,{质量:50,
destinationType:destinationType.FILE\u URI,
sourceType:source});
}
//如果有什么不好的事情发生了就打电话。
//
函数onFail(消息){
警报('失败原因:'+消息);
}
拍摄照片
捕获可编辑照片
从照片库
来自相册
----------更新1------------------
我已经在另一段代码上进行了测试:
(function () {
$scroller = $('.scroller'),
// Take a picture using the camera or select one from the library
takePicture = function (e) {
var options = {
quality: 45,
targetWidth: 1000,
targetHeight: 1000,
destinationType: Camera.DestinationType.FILE_URI,
encodingType: Camera.EncodingType.JPEG,
sourceType: Camera.PictureSourceType.CAMERA
};
navigator.camera.getPicture(
function (imageURI) {
console.log(imageURI);
alert('test');
$scroller.append('<img src="' + imageURI + '"/>');
},
function (message) {
// We typically get here because the use canceled the photo operation. Fail silently.
}, options);
return false;
};
$('.camera-btn').on('click', takePicture);
}());
(函数(){
$scroller=$('.scroller'),
//使用相机拍照或从库中选择一张
拍摄照片=功能(e){
变量选项={
质量:45,
目标宽度:1000,
目标:1000,
destinationType:Camera.destinationType.FILE\u URI,
编码类型:Camera.encodingType.JPEG,
源类型:Camera.PictureSourceType.Camera
};
navigator.camera.getPicture(
函数(imageURI){
log(imageURI);
警报(“测试”);
$scroller.append(“”);
},
功能(信息){
//我们之所以会出现这种情况,通常是因为用户取消了拍照操作。静默失败。
},选项);
返回false;
};
$('camera btn')。在('click',takePicture')上;
}());
这也有同样的效果。它在第一次快照期间不执行任何操作,但在第二次快照之后显示图片。我还刚刚发现,第二张是快照之后显示的图片是我拍摄的第一张照片。getPicture中的第一个参数似乎不会在第一次捕捉时触发。这是令人沮丧的,因为logcat并没有真正向我展示任何可以使用的东西
----------------更新2----------------
我刚刚在Phonegap Build上试用过,效果不错。所以它一定与插件有关…我不知道这是否是正确的解决方案,但它对我来说非常有效。若能追踪你们的原木猫并找到确切的问题,那个就太棒了 尝试使用
导航器.camera.PictureSourceType
安装在pictureSource
中。看来
<button onclick="getPhoto(navigator.camera.PictureSourceType.PHOTOLIBRARY);">From Photo Library</button><br>
或
更新:
尝试在本地保存您的corodova.js并从本地dir调用,这样您的dir就会
assets/www/js/cordova.js
<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
希望这对你有帮助 从3.0.0升级到3.1.0后,我也遇到了同样的问题。延迟摄像机、无地理定位等 查看文件
platforms\android\cordova\version
是否声明了旧版本。
然后你需要更新你的平台。这就是我所做的
- 删除所有插件:
cordova plugin rm org.apache.cordova.camera
- 删除平台:
(将删除您对*.java文件所做的更改)cordova平台删除android
- 添加平台:
cordova平台添加android
- 添加所有插件:
cordova插件添加org.apache.cordova.camera
- 检查权限
- 建造它
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, destinationType: navigator.camera.DestinationType.DATA_URI });
<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
public class MainActivity extends CordovaActivity {
//...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == MyCustomActivity) {
doMyCustomActivity(requestCode);
}
}
//...
}
public class MainActivity extends CordovaActivity {
//...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == MyCustomActivity) {
doMyCustomActivity(requestCode);
}
else { //this was missing - call other Activity of plugins
super.onActivityResult(requestCode, resultCode, intent);
}
}
//...
}