appcelerator钛机器人相机原生捕获照片

时间:2017-08-18 18:39:55

标签: android-camera appcelerator-titanium titanium-alloy

我正在使用appcelerator钛合金开发跨平台移动应用程序。下面的代码打开本机相机功能,并在苹果设备上正常工作。在Android设备上,相机打开但实际拍摄照片的按钮被禁用(灰显)。拍摄视频或更改相机设置的按钮工作正常,只是拍照按钮不起作用。有任何想法吗?提前谢谢

下面的代码点击takePic按钮打开相机

takePic.addEventListener('click', function(e) {
    var win = Titanium.UI.createWindow({ //Open Camera

    });
    Titanium.Media.showCamera({
         success:function(event){
            Ti.API.debug('Our type was: '+event.mediaTpe);
            if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO){

                ***win = Titanium.UI.currentWindow;
                if(osName == "android"){
                    win.open();
                }***

                try {                    
                    var image_name = "siteing.jpg";
                    var fileImage = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, image_name);
                    fileImage.write(event.media);
                    Ti.API.log(event.media);

                    picURL = fileImage;//event.media;
                    picRaw = event.media; //Raw bytes of picture to save to database

                    pictureSet = true;
                    $.videoButton.enabled = false;
                    $.videoButton.backgroundColor = "#DDDDDD";
                    //$.audioButton.enabled = false;
                    format = "Picture";
                    $.savePic.show();

                } catch(e){
                    alert("An Error:" + e.message);
                }

            } else {
                var alert = createErrorAlert({text:"An error occured getting the media. Please check file size and format and try again."});
                $.yesLatLon.add(alert);
                alert.show();
            }
            if(osName == "android"){
                win.open();
            }
        }, cancel:function(){
                 //called when user cancels taking a picture
                 if(osName == "android"){
                    //win.close();
                }
             }, error:function(error){
                 //called when there's an error
                 var a = Titanium.UI.createAlertDialog({title:'Camera'});
                 if(error.code == Titanium.Media.NO_CAMERA){
                     a.setMessage('Please run this test on device');
                 } else {
                     a.setMessage('Unexpected error: ' + error.code);
                 }
                 a.show();
             }, saveToPhotoGallery:true,
             //allowEditing and mediaTypes are iOS-only settings
        allowEditing:true,
        mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO, Ti.Media.MEDIA_TYPE_PHOTO]
     });
     alert.hide();
     $.yesLatLon.removeEventListener('androidback', arguments.callee);
});

screenshot_from_phone

1 个答案:

答案 0 :(得分:0)

已经很长时间了,但是我确实做到了这一点,并认为我会发布我的代码,以防它对其他人有所帮助。公平的警告,我现在在某些Android设备(三星S系列和Google Pixel无法打开相机)上遇到相机问题,我将在另一个问题中发布。但是,去年使用以下代码成功解决了禁用相机按钮的原始问题。

takePic.addEventListener('click', function(e) {
        var win = Titanium.UI.createWindow({//Open Camera

        });

        Titanium.Media.showCamera({
            success : function(event) {
                if (event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {

                    win = Titanium.UI.currentWindow;

                    if (osName == "android") {
                        var img_view = Titanium.UI.createImageView({
                            height : '100%',
                            width : '100%',
                        });

                        win.add(img_view);
                    }
                    try {
                        picURL = event.media;
                        picRaw = event.media;
                        pictureSet = true;
                        $.videoButton.enabled = false;
                        $.videoButton.backgroundColor = "#DDDDDD";
                        $.savePic.show();
                        format = "Picture";
                    } catch(e) {
                        alert("An Error:" + e.message);
                    }

                } else {
                    var alert = createErrorAlert({
                        text : "An error occured getting the media. Please check file size and format and try again."
                    });
                    $.yesLatLon.add(alert);
                    alert.show();
                }

            },
            cancel : function() {
                //called when user cancels taking a picture

            },
            error : function(error) {
                //called when there's an error
                var a = Titanium.UI.createAlertDialog({
                    title : 'Camera'
                });
                if (error.code == Titanium.Media.NO_CAMERA) {
                    a.setMessage('Please run this test on device');
                } else {
                    a.setMessage('Unexpected error: ' + error.code);
                }
                a.show();
            },
            saveToPhotoGallery : true,
            allowEditing : false,
            autohide : true, //Important!
            mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
        });
        alert.hide();
    });