camera
Camera模块管理设备的摄像头,可用于拍照、摄像操作,通过plus.camera获取摄像头管理对象。
方法:
- getCamera: 获取摄像头管理对象
对象:
- Camera: 摄像头对象
- CameraOptions: JSON对象,调用摄像头的参数
- PopPosition: JSON对象,弹出拍照或摄像界面指示位置
回调方法:
- CameraSuccessCallback: 调用摄像头操作成功回调
- CameraErrorCallback: 摄像头操作失败回调
权限:
5+功能模块(permissions)
{ // ... "permissions":{ // ... "Camera": { "description": "摄像头" } } }
Camera
摄像头对象
interface Camera { readonly attribute String[] supportedImageResolutions; readonly attribute String[] supportedVideoResolutions; readonly attribute String[] supportedImageFormats; readonly attribute String[] supportedVideoFormats; function void captureImage(successCB, errorCB, option); function void startVideoCapture(successCB, errorCB, option); function void stopVideoCapture(); }
属性:
- supportedImageResolutions: 字符串数组,摄像头支持的拍照分辨率
- supportedVideoResolutions: 字符串数组,摄像头支持的摄像分辨率
- supportedImageFormats: 字符串数组,摄像头支持的拍照文件格式
- supportedVideoFormats: 字符串数组,摄像头支持的摄像文件格式
方法:
- captureImage: 进行拍照操作
- startVideoCapture: 调用摄像头进行摄像操作
- stopVideoCapture: 结束摄像操作
CameraOptions
JSON对象,调用摄像头的参数
interface CameraOptions { attribute String filename; attribute String format; attribute String index; attribute Number videoMaximumDuration; attribute Boolean optimize; attribute String resolution; attribute PopPosition popover; }
属性:
- filename: (String
类型
)拍照或摄像文件保存的路径
可设置具体文件名(如"_doc/camera/a.jpg");也可只设置路径,以"/"结尾则表明是路径(如"_doc/camera/")。 如未设置文件名称或设置的文件名冲突则文件名由程序程序自动生成。
- format: (String
类型
)拍照或摄像的文件格式
可通过Camera对象的supportedImageFormats或supportedVideoFormats获取,如果设置的参数无效则使用系统默认值。
- index: (String
类型
)拍照或摄像默认使用的摄像头
拍照或摄像界面默认使用的摄像头编号,1表示主摄像头,2表示辅摄像头。
- videoMaximumDuration: (Number
类型
)视频长度
单位为秒(s),小于等于0表示不限定视频长度。 默认值为0(不限定视频长度)。 注意:仅在调用拍摄视频(startVideoCapture)时有效。
- optimize: (Boolean
类型
)是否优化图片
自动调整图片的方向,在部分设备上可能出现图片方向不正确的问题,此参数将配置是否自动调整图片方向。 可取值: true - 自动调整图片方向; false - 不调整。 默认值为true。 注意:自动调整图片方向将消耗部分系统资源,可能会导致拍照后回调触发时机延迟,将此值设置为false则可避免延迟问题。
- resolution: (String
类型
)拍照或摄像使用的分辨率
可通过Camera对象的supportedImageResolutions或supportedVideoResolutions获取,如果设置的参数无效则使用系统默认值。
- popover: (PopPosition
类型
)拍照或摄像界面弹出指示区域
对于大屏幕设备如iPad,拍照或摄像界面为弹出窗口,此时可通过此参数设置弹出窗口位置,其为JSON对象,格式如{top:"10px",left:"10px",width:"200px",height:"200px"},默认弹出位置为屏幕居中。
PopPosition
JSON对象,弹出拍照或摄像界面指示位置
属性:
- top: (String
类型
)指示区域距离容器顶部的距离
弹出拍照或摄像窗口指示区域距离容器顶部的距离,支持像素值(如"100px")和百分比(如"50%")。
- left: (String
类型
)指示区域距离容器左侧的距离
弹出拍照或摄像窗口指示区域距离容器左侧的距离,支持像素值(如"100px")和百分比(如"50%")。
- width: (String
类型
)指示区域的宽度
弹出拍照或摄像窗口指示区域的宽度,支持像素值(如"100px")和百分比(如"50%")。
- height: (String
类型
)指示区域的高度
弹出拍照或摄像窗口指示区域的高度,支持像素值(如"100px")和百分比(如"50%")。
CameraSuccessCallback
调用摄像头操作成功回调
void onSuccess( capturedFile ) { // Caputre image/video file code. }
说明:
调用摄像头操作成功的回调函数,在拍照或摄像操作成功时调用,用于返回图片或视频文件的路径。
参数:
- capturedFile:
(
String
)
必选 拍照或摄像操作保存的文件路径
返回值:
void : 无CameraErrorCallback
摄像头操作失败回调
void onError( error ) { // Handle camera error var code = error.code; // 错误编码 var message = error.message; // 错误描述信息 }
参数:
- error:
(
Exception
)
必选 摄像头操作的错误信息
可通过error.code(Number类型)获取错误编码; 可通过error.message(String类型)获取错误描述信息。