0

我在 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);
4

1 回答 1

0

我能够通过在实例化相机之后添加 1 秒超时来解决这个问题,就在从后向前转动它之前,还在让 takePicture 事件通过之前添加了 500 毫秒超时。

于 2013-11-11T15:59:29.697 回答