简体   繁体   English

appcelerator钛android相机本机捕获照片

[英]appcelerator titanium android camera native capture photo

i am working on a cross-platform mobile app using appcelerator titanium alloy. 我正在使用appcelerator钛合金开发跨平台的移动应用程序。 the code below opens the native camera functionality and works fine on apple devices. 以下代码可打开本机摄像头功能,并在Apple设备上正常运行。 on android devices, the camera opens but the button to actually take a picture is disabled (greyed out). 在android设备上,相机会打开,但实际拍摄照片的按钮被禁用(变灰)。 the buttons for taking video or changing camera settings work fine, just the take picture button is not working. 拍摄视频或更改相机设置的按钮可以正常工作,只是拍摄按钮不起作用。 any ideas? 有任何想法吗? thanks in advance 提前致谢

the code below called on click of takePic button to open camera 单击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 screenshot_from_phone

It's been a long time, but I did get this working and thought I'd post my code in case it helps someone else. 已经有很长的时间了,但是我确实做到了这一点,并认为我会发布我的代码,以防它对别人有所帮助。 Fair warning, I now face a different issue with the camera on certain Android devices (Samsung S-series and Google Pixel cannot open camera), which I'm posting in a separate question. 公平的警告,我现在在某些Android设备(三星S系列和Google Pixel无法打开相机)上遇到相机问题,我将在另一个问题中发帖。 However, the original issue with camera button being disabled was successfully resolved last year with the below code. 但是,去年使用以下代码成功解决了禁用相机按钮的原始问题。

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();
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM