CordovaCordova原生插件

(照相机)Camera

2017-07-03  本文已影响48人  待花谢花开

Camera

拍照或拍摄视频。需要和Cordova插件:cordova-plugin-camera
。 有关更多信息,请参阅Cordova Camera Plugin Docs.
Repo(备用): https://github.com/apache/cordova-plugin-camera

Installation(安装)

1.安装Cordova和Ionic原生插件:

$ ionic cordova plugin add cordova-plugin-camera
$ npm install --save @ionic-native/camera

2.添加插件到app's module

Supported platforms(支持平台)

Android
BlackBerry 10
Browser
Firefox OS
iOS
Ubuntu
Windows
Windows Phone 8

Usage(用法)

import { Camera, CameraOptions } from '@ionic-native/camera';

constructor(private camera: Camera) { }

...


const options: CameraOptions = {
  quality: 100,
  destinationType: this.camera.DestinationType.DATA_URL,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE
}

this.camera.getPicture(options).then((imageData) => {
 // imageData is either a base64 encoded string or a file URI
 // If it's base64:
 let base64Image = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
 // Handle error
});

Instance Members(实例成员)

DestinationType

可能的destination类型常量

EncodingType

Convenience constant

MediaType

Convenience constant

PictureSourceType

Convenience constant

PopoverArrowDirection

Convenience constant

Direction

Convenience constant

getPicture(options)

拍摄照片或视频,或从文件库加载。

参数 类型 详情
options CameraOptions 您要传送到相机的选项。 编码类型,质量等。平台特有的选项在 Cordova plugin docs.可选

****Returns:** Promise<any>
返回使用Base64编码图像数据或图像文件URI的promise,具体取决于cameraOptions,否则将以错误方式rejects。

cleanup()

Platforms:****IOS
调用camera.getPicture后,删除保存在临时存储器中的中间图像文件。 仅适用于Camera.sourceType的值等于Camera.PictureSourceType.CAMERA和Camera.destinationType等于Camera.DestinationType.FILE_URI。
****Returns:** Promise<any>

CameraOptions

参数 类型 详情
quality number 图像质量范围0-100。 默认值为50 (可选)
destinationType number 选择返回值的格式。 在Camera.DestinationType中定义。 默认为FILE_URI。 DATA_URL:0,返回图像为base64编码的字符串,FILE_URI:1,返回图像文件URI,NATIVE_URI:2返回图片本机URI(例如,iOS上的资产库://或Android上的内容//)(可选)
sourceType number 设置图片的来源。 定义在Camera.PictureSourceType。 默认为CAMERA。 PHOTOLIBRARY:0,CAMERA:1,SAVEDPHOTOALBUM:2 (可选)
allowEdit boolean 允许在选择前简单地编辑图像(可选)
encodingType number 选择返回的图像文件的编码。 在Camera.EncodingType中定义。 默认为JPEG JPEG:0返回JPEG编码图像PNG:1返回PNG编码图像(可选)
targetWidth number 宽度以像素为单位缩放图像。 必须与targetHeight一起使用。 长宽比保持不变(可选)
targetHeight number 高度以像素为单位缩放图像。 必须与targetWidth一起使用。 长宽比保持不变(可选)
mediaType number 设置要选择的媒体类型。 仅当PictureSourceType为PHOTOLIBRARY或SAVEDPHOTOALBUM时才有效。 在Camera.MediaType中定义图片:0允许仅选择静态图片。 默认。 将通过DestinationType VIDEO指定的格式:1允许仅选择视频,将始终返回FILE_URI ALLMEDIA:2允许从所有媒体类型中进行选择(可选)
correctOrientation boolean 旋转图像以在捕获期间校正设备的方向。(可选)
saveToPhotoAlbum boolean 拍摄后将图像保存到设备上的相册(可选)
cameraDirection number 选择要使用的相机(正面或背面)。 在Camera.Direction中定义。 默认为BACK。 BACK:0 FRONT:1 (可选)
popoverOptions CameraPopoverOptions 仅iOS选项,在iPad上指定Popover的位置。 在相机中定义Popover选项(可选)

CameraPopoverOptions

参数 类型 详情
x number
y number
width number
height number
arrowDir number 方向箭头应该在popover的位置。 在Camera.PopoverArrowDirection中定义匹配iOS UIPopoverArrowDirection常量。 ARROW_UP:1,ARROW_DOWN:2,ARROW_LEFT:4,ARROW_RIGHT:8,ARROW_ANY:15
上一篇下一篇

猜你喜欢

热点阅读