简体   繁体   English

Cordova-为什么camera.getpicture不起作用?

[英]Cordova - Why is camera.getpicture not working?

I am trying to get the camera feature to work. 我正在尝试使相机功能正常工作。 I am testing on a connected samsung galaxy tablet. 我正在连接的三星银河平板电脑上进行测试。 The app launches successfully, but navigator.camera.getpicture does not launch the camera. 该应用程序成功启动,但是navigator.camera.getpicture无法启动相机。 Can you help with this? 你能帮忙吗?

Here are the steps I followed: I created a cordova hello project using the following steps on (Windows 7): 我遵循的步骤如下:在(Windows 7)上使用以下步骤创建了cordova hello项目:

PART A: (setup cordova) (steps from this answer here ): C:> npm install -g cordova A部分:(安装cordova) (从此处获得此答案的步骤): C:> npm install -g cordova

From command prompt, navigate to the folder you want to create your project using: 在命令提示符下,使用以下命令导航到要创建项目的文件夹:

cordova create hello com.example.hello HelloWorld
cd hello
Define the OS you want to suppport for example:

cordova platform add wp8
Install plugins (If needed). For example we want the following:

cordova plugin add org.apache.cordova.device
cordova plugin add org.apache.cordova.camera
cordova plugin add org.apache.cordova.media-capture
cordova plugin add org.apache.cordova.media

Part B: Add code for testing camera features (from code from Cordova camera docs ) Replaced index.html code with the following code: B部分:添加用于测试相机功能的代码(来自Cordova camera docs的代码)用以下代码替换index.html代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Capture Photo</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
    <script type="text/javascript" charset="utf-8">

        var pictureSource;   // picture source
        var destinationType; // sets the format of returned value 

        // Wait for Cordova to connect with the device
        //
        document.addEventListener("deviceready", onDeviceReady, false);

        // Cordova is ready to be used!
        //
        function onDeviceReady() {
            pictureSource = navigator.camera.PictureSourceType;
            destinationType = navigator.camera.DestinationType;
        }

        // Called when a photo is successfully retrieved
        //
        function onPhotoDataSuccess(imageData) {
            // Uncomment to view the base64 encoded image data
            // console.log(imageData);

            // Get image handle
            //
            var smallImage = document.getElementById('smallImage');

            // Unhide image elements
            //
            smallImage.style.display = 'block';

            // Show the captured photo
            // The inline CSS rules are used to resize the image
            //
            smallImage.src = "data:image/jpeg;base64," + imageData;
        }

        // Called when a photo is successfully retrieved
        //
        function onPhotoURISuccess(imageURI) {
            // Uncomment to view the image file URI 
            // console.log(imageURI);

            // Get image handle
            //
            var largeImage = document.getElementById('largeImage');

            // Unhide image elements
            //
            largeImage.style.display = 'block';

            // Show the captured photo
            // The inline CSS rules are used to resize the image
            //
            largeImage.src = imageURI;
        }

        // A button will call this function
        //
        function capturePhoto() {
            alert('taking photo');
            // Take picture using device camera and retrieve image as base64-encoded string
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
                quality: 50,
                destinationType: destinationType.DATA_URL
            });
        }

        // A button will call this function
        //
        function capturePhotoEdit() {
            // Take picture using device camera, allow edit, and retrieve image as base64-encoded string  
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
                quality: 20, allowEdit: true,
                destinationType: destinationType.DATA_URL
            });
        }

        // A button will call this function
        //
        function getPhoto(source) {
            // Retrieve image file location from specified source
            navigator.camera.getPicture(onPhotoURISuccess, onFail, {
                quality: 50,
                destinationType: destinationType.FILE_URI,
                sourceType: source
            });
        }

        // Called if something bad happens.
        // 
        function onFail(message) {
            alert('Failed because: ' + message);
        }

    </script>
  </head>
  <body>
    <button onclick="capturePhoto();">Capture Photo</button> <br>
    <button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
    <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
    <button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
    <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
    <img style="display:none;" id="largeImage" src="" />
  </body>
</html>

Part C :Run the app on connected device (Samsung Galaxy tab) C部分:在连接的设备上运行应用程序(三星Galaxy标签)

cordova run android

The app launches successfully on the tablet, BUT the buttons in index.html do not launch the camera. 该应用程序可在平板电脑上成功启动,但index.html中的按钮不会启动相机。 Why is this? 为什么是这样? I am not sure how to even debug this situation. 我不确定如何调试这种情况。 Can you shed some light on this? 您能对此有所了解吗?

I don't know why you have src="cordova-2.2.0.js" , simply change it to src=cordova.js and it should work. 我不知道为什么会有src="cordova-2.2.0.js" ,只需将其更改为src=cordova.js使用。

For debugging have a look at adb logcat . 要进行调试,请查看adb logcat

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

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