如何在Android手机上使用phonegap显示相机 [英] how to display camera on Android phone by using phonegap

查看:186
本文介绍了如何在Android手机上使用phonegap显示相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android手机上使用phonegap捕获图像。我尝试在Phonegap页上发布的代码,但相机不显示。非常感谢。非常感谢。非常感谢。



这是我手机上的显示



 <!DOCTYPE html> 
< html>
< head>
< title>捕获照片< / title>

< script type =text / javascriptcharset =utf-8src =cordova-1.6.0.js>< / script&
< script type =text / javascriptcharset =utf-8>

var pictureSource; // picture source
var destinationType; //设置返回值的格式

//等待Cordova连接设备
//
document.addEventListener(deviceready,onDeviceReady,false);

// Cordova已准备好使用了!
//
function onDeviceReady(){
pictureSource = navigator.camera.PictureSourceType;
destinationType = navigator.camera.DestinationType;
}

//成功检索照片时调用
//
函数onPhotoDataSuccess(imageData){
//取消注释以查看base64编码图像数据
// console.log(imageData);

//获取图像处理
//
var smallImage = document.getElementById('smallImage');

//取消隐藏图片元素
//
smallImage.style.display ='block';

//显示捕获的照片
//内联CSS规则用于调整图像大小
//
smallImage.src =data:image / jpeg ; base64,+ imageData;
}

//成功检索照片时调用
//
函数onPhotoURISuccess(imageURI){
//取消注释以查看图像文件URI
// console.log(imageURI);

//获取图像句柄
//
var largeImage = document.getElementById('largeImage');

//取消隐藏图片元素
//
largeImage.style.display ='block';

//显示捕获的照片
//内联CSS规则用于调整图像大小
//
largeImage.src = imageURI;
}

//按钮将调用此函数
//
function capturePhoto(){
//使用设备摄像头拍摄图片并检索图片作为base64编码的字符串
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{quality:50,
destinationType.DATA_URL});
}

//按钮将调用此函数
//
function capturePhotoEdit(){
//使用设备摄像头拍摄图片,允许编辑,并检索图像为base64编码的字符串
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{quality:20,allowEdit:true,
destinationType.DATA_URL});
}

//按钮将调用此函数
//
function getPhoto(source){
//从指定源检索映像文件位置
navigator.camera.getPicture(onPhotoURISuccess,onFail,{quality:50,
destinationType:destinationType.FILE_URI,
sourceType:source});
}

//如果发生错误调用。
//
function onFail(message){
alert('Failed because:'+ message);
}

< / script>
< / head>
< body>
< button onclick =capturePhoto();>捕获照片< / button> < br>
< button onclick =capturePhotoEdit();>捕获可编辑照片< / button> < br>
< button onclick =getPhoto(pictureSource.PHOTOLIBRARY);>来自照片库< / button>< br>
< button onclick =getPhoto(pictureSource.SAVEDPHOTOALBUM);>来自相簿< / button>< br>
< img style =display:none; width:60px; height:60px; id =smallImagesrc =/>
< img style =display:none; id =largeImagesrc =/>
< / body>
< / html> code c> c>

capturePhotoEdit()函数需要更新 destinationType

  navigator.camera.getPicture(
onPhotoDataSuccess,
onFail,
{
quality:50,
destinationType.DATA_URL
}
);

应该是这样,

  navigator.camera.getPicture(
onPhotoDataSuccess,
onFail,
{
quality:50,
destinationType:destinationType.DATA_URL
}
);


I want to capture an image by using phonegap on Android phone. I try the code posted on Phonegap page but the camera does not appear. Any help is appreciated.Thank you very much.

It is the display on my phone

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

    <script type="text/javascript" charset="utf-8" src="cordova-1.6.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() {
      // Take picture using device camera and retrieve image as base64-encoded string
      navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
        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.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>

解决方案

Both your capturePhoto() and capturePhotoEdit() functions need to have the destinationType updated.

navigator.camera.getPicture(
    onPhotoDataSuccess,
    onFail,
    {
        quality: 50,
        destinationType.DATA_URL
    }
);

should be like this,

navigator.camera.getPicture(
    onPhotoDataSuccess,
    onFail,
    {
        quality: 50,
        destinationType: destinationType.DATA_URL
    }
);

这篇关于如何在Android手机上使用phonegap显示相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆