Javascript Phonegap+;JQM-摄影机API,无法查看捕获的照片
我正在尝试Phonegap的摄像头API,但遇到了一个问题。使用官方文档中的代码:Javascript Phonegap+;JQM-摄影机API,无法查看捕获的照片,javascript,jquery-mobile,cordova,Javascript,Jquery Mobile,Cordova,我正在尝试Phonegap的摄像头API,但遇到了一个问题。使用官方文档中的代码: <script type="text/javascript" charset="utf-8"> var pictureSource; // picture source var destinationType; // sets the format of returned value // Wait for PhoneGap to connect with the d
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for PhoneGap to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// PhoneGap is ready to be used!
//
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 });
}
// 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 });
}
// 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>
var pictureSource;//图像源
var destinationType;//设置返回值的格式
//等待PhoneGap与设备连接
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//PhoneGap已准备好使用!
//
函数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,{quality:50});
}
//一个按钮将调用此函数
//
函数capturePhotoEdit(){
//使用设备照相机拍照,允许编辑,并以base64编码字符串的形式检索图像
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{quality:20,allowEdit:true});
}
//一个按钮将调用此函数
//
函数getPhoto(源代码){
//从指定源检索图像文件位置
navigator.camera.getPicture(onPhotoURISuccess,onFail,{质量:50,
destinationType:destinationType.FILE\u URI,
sourceType:source});
}
//如果有什么不好的事情发生了就打电话。
//
函数onFail(消息){
警报('失败原因:'+消息);
}
还有我的按钮:
拍摄照片
和img标签:
相机可以很好地打开,拍摄照片。没有问题,但是,它不会显示在页面上
我有更多的代码,可以让你从相册中选择一个图像,这非常好,可以在不同的图像标签中显示它
我认为问题在于它找不到图像数据
拍摄的照片确实会保存到手机中,并且可以使用其他按钮显示,但我希望它在拍摄照片后直接显示
我正在使用JQM btw,并使用Phonegap:Build web编译器编译我的APK。关于Phonegap的文档是错误的。要获取base64编码图片,需要调用
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality:50, destinationType:Camera.DestinationType.DATA_URL });
使用文件URI更容易、更清晰(但如果希望图像持久化,请小心,在iOS上,图像会保存到临时文件夹)。您可以按如下方式立即显示图像:
navigator.camera.getPicture(displayPicture, function(err){}, {quality : 40,
destinationType : Camera.DestinationType.FILE_URI,
sourceType : Camera.PictureSourceType.CAMERA});
function displayPicture(file_uri){
var img_tag = '<img style="width:60px;height:60px;" id="smallImage" src="'+file_uri+'" />';
//Attach the img tag where ever you want it ... $(<some parent tag>).append(img_tag) etc.
}
navigator.camera.getPicture(displayPicture,函数(err){},{quality:40,
destinationType:Camera.destinationType.FILE\u URI,
sourceType:Camera.PictureSourceType.Camera});
函数displayPicture(文件\u uri){
var img_tag='';
//将img标记附加到任何需要的位置…$()。附加(img_标记)等。
}
在CapturePhoto()函数中,添加此选项
destinationType : Camera.DestinationType.FILE_URI,
正如Phonegap所说,使用文件URI是使用新一代手机拍照的最佳实践
要显示图像,请在getPhotoDataSuccess中使用此方法
$('#smallImage').attr("src",imageURI);
这两者都不返回base64