Với mục đích thử nghiệm, tôi đã sao chép toàn bộ ví dụ được tìm thấy trên phonegap camera API và tôi đặt cảnh báo trên onPhotoDataSuccess
để kiểm tra khi nào chức năng được kích hoạt. Trên bức ảnh đầu tiên chụp cảnh báo sẽ không hiển thị. Tuy nhiên sau lần thử đầu tiên, cảnh báo sẽ hiển thị sau khi ảnh được lưu.Phonegap (3.0.0) Máy ảnh không thành công khi thử lần đầu tiên
Bạn có lời khuyên nào không? Tôi sẽ rất vui khi được cụ thể hơn nếu có điều gì đó không rõ ràng.
Tôi đã thử nghiệm mã dưới đây vào Android Galaxy S3 của tôi
<!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>
---------- UPDATE 1 ----------------- -
tôi đã thử nghiệm nó trên một chút mã:
(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);
}());
Và điều này có tác dụng tương tự. Nó không làm gì trong lần chụp đầu tiên nhưng nó hiển thị hình ảnh sau lần chụp thứ hai. Tôi cũng chỉ phát hiện ra rằng hình ảnh cho thấy sau khi thứ hai là snap là bức ảnh đầu tiên mà tôi đã chụp. Dường như đối số đầu tiên trong getPicture không kích hoạt trên snap đầu tiên. Điều này là bực bội như logcat không thực sự cho tôi thấy bất cứ điều gì để làm việc với.
---------------- CẬP NHẬT 2 ----------------
Tôi vừa thử trên Phonegap Build và nó hoạt động. Vì vậy, nó phải có một cái gì đó để làm với các plugin ...
thực sự có thể sử dụng một câu trả lời ở đây nếu có ai biết. Đây có phải là 3.0.0 hoặc 3.0.0rc1 không? –
Bạn có thể dán mã bạn đang sử dụng và cho chúng tôi biết bạn đang thử nghiệm điện thoại nào không. – Zorayr
Bạn đang phát triển cho iOS? Từ [Tài liệu PhoneGap] (http://docs.phonegap.com/en/3.0.0/cordova_camera_camera.md.html#camera.getPicture): "Bao gồm cảnh báo JavaScript() trong một trong hai chức năng gọi lại có thể gây ra sự cố Quấn cảnh báo trong một setTimeout() để cho phép bộ chọn hình ảnh iOS hoặc popover đóng hoàn toàn trước khi cảnh báo hiển thị: "Nếu bạn thay đổi cảnh báo thành console.log, công việc gọi lại đầu tiên có được không? – ville