phonegap入门-- Camera 摄像头

今天看了一下phonegap 的Camera摄像头章节入门知识,从官网ApI上看了例子,然后分析了一下:

配置可以看一下官网的API:http://www.phonegap100.com/doc/cordova_camera_camera.md.html#Camera


通过看官网API,下面总结一下相关方法和属性参数:

Camera Api简单介绍 
Camera选择使用摄像头拍照,或从设备相册中获取一张照片。图片以 base64 编码的
字符串或图片URI 形式返回。 
 
方法:   
1. camera.getPicture     拍照获取相册图片 
navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] ); 
cameraSuccess:提供图像数据的onSuccess 回调函数。 
cameraError:提供错误信息的 onError回调函数。 
cameraOptions:定制摄像头设置的可选参数 
2. camera.cleanup       清除拍照后设备的缓存图片 
navigator.camera.cleanup( cameraSuccess, cameraError ); 
 
3.cameraOptions 参数: 
 
定制摄像头设置的可选参数。 
quality :  存储图像的质量,范围是[0,100]。 
destinationType :选择返回数据的格式。 
sourceType :设定图片来源。data:image/jpeg;base64, 
allowEdit :在选择图片进行操作之前允许对其进行简单编辑。(好像只有ios支持) 
encodingType :选择返回图像文件的编码方 encodingType: Camera.EncodingType.JPEG 
targetWidth :以像素为单位的图像缩放宽度指定图片展示的时候的宽度 
targetHeight :以像素为单位的图像缩放高度指定图片展示的时候的高度 
saveToPhotoAlbum:拍完照片后是否将图像保存在设备上的相册 
mediaType  设置选择媒体的类型 
cameraDirection   选择前置摄像头还是后置摄像头 
 
注意:在Android中。 
忽略allowEdit 参数。 
Camera.PictureSourceType.PHOTOLIBRARY
Camera.PictureSourceType.SAVEDPHOTOALBUM 都会显示同一个相集。



下面给予官网上的例子,相关代码说明已经非常清晰:

<!DOCTYPE html>
<html>
  <head>
    <title>Capture Photo</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">
    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() {
        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: 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 οnclick="capturePhoto();">Capture Photo</button> <br>
    <button οnclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
    <button οnclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
    <button οnclick="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>


经过上述实例,学到知识点:

1)会用相关参数,特别是cameraOptions 参数

2)当返回类型是destinationType: destinationType.DATA_URL时候:在success返回的函数里面要用到base64解码smallImage.src = "data:image/jpeg;base64," + imageData;

3)当返回类型是destinationType: destinationType.FILE_URI时候:在success返回的函数里面要用到largeImage.src = imageURI,直接给该file_url路径即可。

4)html中设置块级元素:smallImage.style.display = 'block';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值