码迷,mamicode.com
首页 > 其他好文 > 详细

phonegap入门-- Camera 摄像头

时间:2015-05-30 09:28:51      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:phonegap入门   camera 摄像头   

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

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


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

<span style="font-size:18px;">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 都会显示同一个相集。

</span>


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

<span style="font-size:18px;"><!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 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></span>


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

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‘;

phonegap入门-- Camera 摄像头

标签:phonegap入门   camera 摄像头   

原文地址:http://blog.csdn.net/itjavawfc/article/details/46240633

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!