PhoneGap 3.0.0:无法实例化相机插件对象

时间:2013-10-08 18:42:26

标签: javascript android plugins cordova camera

我无法在onDeviceReady()之后实例化相机插件对象。导航器对象很好,但是navigator.camera === undefined会在后续的摄像头方法调用中爆炸。相机插件安装没有事件,但我确定它是一个配置问题。刚用尽了额外的岩石偷看。在此先感谢您的帮助。这是我的文件:

  1. JS

    <script type="text/javascript" src="../../phonegap.js"></script>
    <script type="text/javascript" src="../../js/vendor/jquery/jquery.2.0.3.min.js"></script>
    <script type="text/javascript">
    
    function formResize() {
    
        $('.btnlink').css('width', parseInt($('.form01').css('width').replace('px', '')));
        $('.form_wrapper').css('margin-top', parseInt($('.globalhdr').css('height').replace('px', '')) + 25);
        }
        $('window').resize(function() {
            formResize();
        });
    }
    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() {
            try {
                alert('onDeviceReady!');
                alert('navigator = ' + navigator);
                alert('navigator.camera = ' + navigator.camera);
                alert('navigator.camera.DestinationType = ' + navigator.camera.DestinationType);
                //                pictureSource=navigator.camera.PictureSourceType;
                destinationType = navigator.camera.DestinationType;            // navigator.camera.DestinationType;
                alert('destinationType = ' + destinationType);
            }
            catch(e) {
                alert(e);
            }
        }
        // A button will call this function
        //
        function capturePhoto() {
            alert('capturePhoto enter');
            // Take picture using device camera and retrieve image as base64-encoded string
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, destinationType: destinationType.DATA_URL });
            alert('capturePhoto exit');
        }
    
        $('#camera').on('click', function(e) {
            try {
                e.preventDefault();
                capturePhoto();
            }
            catch(e) {
                alert(e);
            }
        });
    
        // Called when a photo is successfully retrieved
        //
        function onPhotoDataSuccess(imageData) {
            alert('onPhotoDataSuccess enter');
            // Uncomment to view the base64-encoded image data
            // console.log(imageData);
    
            // Get image handle
            //
            var capturedImg = $('#capturedImg');
    
            // Unhide image elements
            //
            capturedImg.css('display', 'block');
    
            // Show the captured photo
            // The inline CSS rules are used to resize the image
            //
            capturedImg.src = "data:image/jpeg;base64," + imageData;
            alert('onPhotoDataSuccess exit');
        }
    
        // Called if something bad happens.
        //
        function onFail(message) {
            alert('Failed because: ' + message);
        }
    
  2. 的Config.xml

    <widget id="com.phonegap.hello-world" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
        <name>MyApp</name>
        <description>
            Hello World sample application that responds to the deviceready event.
        </description>
        <author email="email@email.com" href="">MyApp Team</author>
        <feature name="Camera">
            <param name="android-package" value="org.apache.cordova.CameraLauncher" />
        </feature>
        <feature name="http://api.phonegap.com/1.0/device" />
        <!--<plugins>-->
            <!--<plugin name="Camera" value="org.apache.cordova.camera" />-->
        <!--</plugins>-->
        <!-- more -->
    </widget>
    
  3. 的AndroidManifest.xml

    <?xml version='1.0' encoding='utf-8'?>
        <manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="1.0.0" android:windowSoftInputMode="adjustPan" package="com.phonegap.hello_world" xmlns:android="http://schemas.android.com/apk/res/android">
            <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
            <uses-permission android:name="android.permission.INTERNET" />
            <application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
                <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="BlackDollShows" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar">
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
            </application>
            <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        </manifest>
    

1 个答案:

答案 0 :(得分:0)

解决!

替换

<script type="text/javascript" src="../../phonegap.js"></script>

使用

<script type="text/javascript" src="../../cordova.js"></script>

很简单,但需要很多杂技。希望它对您的项目有所帮助。