我在使用iPad上的Titanium相机切换时遇到了问题,到目前为止,我猜这也是Android上的一个问题。现在这不会一直发生,但也许每10次我试图拍照。这是我从iPad控制台获得的:
<Warning>: UIImagePickerController: ignoring request to take picture; camera is changing modes.
以下是代码:
var takePicture = Ti.UI.createButton({
width: '260dp',
height: '80dp',
backgroundColor: '#62bb47',
backgroundImage: 'none',
top: '520dp',
borderRadius: 10
});
var takePictureIcon = Ti.UI.createImageView({
image: '/images/icons/10_device_access_camera.png',
left: '10dp'
});
var takePictureLabel = Ti.UI.createLabel({
text: Alloy.CFG.customL.strings('take_photo'),
width: '60%',
color: '#fff',
backgroundColor: 'transparent',
font: {
fontSize: '24dp'
},
textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER
});
takePicture.add(takePictureIcon);
takePicture.add(takePictureLabel);
takePicture.addEventListener('click', function(e){
Ti.Media.takePicture();
});
takePicture.addEventListener('doubletap', function(e){
return false;
});
takePicture.addEventListener('touchstart', function(e){
takePicture.setBackgroundColor('#34aadc');
});
takePicture.addEventListener('touchend', function(e){
//Ti.Media.takePicture();
takePicture.setBackgroundColor('#62bb47');
});
container.add(takePicture);
overlay.add(container);
// DIRECTORIES AND STORING PICS
// get a handle to the as-yet non-existent directory
var masterPicsDir = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'masterPics');
masterPicsDir.createDirectory(); // this creates the directory
// get a handle to the as-yet non-existent directory
var transactionPicsDir = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'transactionPics');
transactionPicsDir.createDirectory(); // this creates the directory
$.cameraView.hide();
takePicture.hide();
Titanium.Media.showCamera({
success:function(event)
{
var image = event.media;
//alert(JSON.stringify(event.media));
Ti.API.debug('Our type was: '+event.mediaType);
if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
{
//var imageView = Ti.UI.createImageView({width:$.cameraView.width,height:$.cameraView.height,image:event.media});
var image = event.media;
var base64 = Ti.Utils.base64encode(event.media).toString();
//alert(base64);
//alert('we should save the picture to gallery and update the employee model with the src of the picture');
//
// IF EMPLOYEE DOES HAVE A MASTER PHOTO FILE NAME
if(!employee.get('photoFileName') || employee.get('photoFileName').indexOf('MasterPhoto') === 0){
//alert( 'Make new master' + employee.get('photoFileName') );
var masterFilePath = transactionType + '_' + employee.get('badge') + '_' + transactionTime + '.png';
var transactionFilePath = transactionType + '_' + employee.get('badge') + '_' + transactionTime + '.png';
var masterPicture = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, masterFilePath);
//masterPicture.write(image);
masterPicture.write(base64);
masterPicture.move('masterPics/' + masterFilePath);
masterPicture = null;
var employeePicture = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, transactionFilePath);
//employeePicture.write(image);
employeePicture.write(base64);
employeePicture.move('transactionPics/' + transactionFilePath);
employeePicture = null;
//alert(masterFilePath);
employee.set({photoFileName: 'masterPics/' + masterFilePath});
employee.save();
transactionEntry.set({photoFileName: 'transactionPics/' + transactionFilePath});
transactionEntry.set({photoTime: transactionTime});
//transactionEntry.set({photoData: base64});
clockHistoryEntry.set({photoFileName: 'transactionPics/' + transactionFilePath});
clockHistoryEntry.set({photoTime: transactionTime});
}
else{
//alert( 'We have master!' + employee.get('photoFileName') );
var transactionFilePath = transactionType + '_' + employee.get('badge') + '_' + transactionTime + '.png';
var employeePicture = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, transactionFilePath);
//employeePicture.write(image);
employeePicture.write(base64);
employeePicture.move('transactionPics/' + transactionFilePath);
employeePicture = null;
transactionEntry.set({photoFileName: 'transactionPics/' + transactionFilePath});
transactionEntry.set({photoTime: transactionTime});
//transactionEntry.set({photoData: base64});
clockHistoryEntry.set({photoFileName: 'transactionPics/' + transactionFilePath});
clockHistoryEntry.set({photoTime: transactionTime});
}
//alert('about to fire the go to confirmation event');
$.cameraView.fireEvent('go_to_confirmation', {
employee: args.data.employee,
departmentName: departmentName,
transactionEntry: transactionEntry,
clockHistoryEntry: clockHistoryEntry
});
//$.cameraView.add(imageView);
}
else
{
alert("got the wrong type back = " + event.mediaType);
}
},
cancel:function()
{
alert('You canceled the action.');
},
error:function(error)
{
// create alert
var a = Titanium.UI.createAlertDialog({title:'Camera'});
// set message
if (error.code == Titanium.Media.NO_CAMERA)
{
a.setMessage('Please run this test on device');
console.log("Firing after error on simulator, badge number to pass: " + args.data.employee);
$.cameraView.fireEvent('go_to_confirmation', {
employee: args.data.employee,
transactionEntry: transactionEntry,
clockHistoryEntry: clockHistoryEntry
});
}
else
{
a.setMessage('Unexpected error: ' + /*error.code*/ JSON.stringify(error));
console.log('Camera error: ' + /*error.code*/ JSON.stringify(error));
Alloy.CFG.log('Error', 'Camera error: ' + /*error.code*/ JSON.stringify(error));
}
// show alert
a.show();
},
overlay : overlay,
saveToPhotoGallery:false,
allowEditing:false,
mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO],
showControls : false,
autohide : true,
//make the picture inside the camera smaller so that we can than place an overlay around it
transform: Ti.UI.create2DMatrix({
scale : 0.5
})
});
Ti.Media.switchCamera(Ti.Media.CAMERA_FRONT);
//$.cameraView.show();
setTimeout( function(){ takePicture.show(); }, 1500);
答案 0 :(得分:0)
我能够通过在实例化摄像机之后添加1秒的超时来解决这个问题,然后在从后向前转动它之前添加,并且在让takePicture事件通过之前还添加了500毫秒的超时。