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
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(實例成員)
可能的destination類型常量
Convenience constant
Convenience constant
Convenience constant
Convenience constant
Convenience constant
拍攝照片或視頻,或從文件庫加載。
參數 | 類型 | 詳情 |
---|---|---|
options | CameraOptions | 您要傳送到相機的選項。 編碼類型,質量等。平臺特有的選項在 Cordova plugin docs.可選 |
****Returns:** Promise<any>
返回使用Base64編碼圖像數據或圖像文件URI的promise,具體取決于cameraOptions,否則將以錯誤方式rejects。
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 |