camera

  • : 获取摄像头管理对象

对象:

  • Camera: 摄像头对象
  • : JSON对象,调用摄像头的参数
  • PopPosition: JSON对象,弹出拍照或摄像界面指示位置

回调方法:

权限:

5+功能模块(permissions)

getCamera

获取摄像头管理对象

  1. Camera plus.camera.getCamera( index );

说明:

获取需要操作的摄像头对象,如果要进行拍照或摄像操作,需先通过此方法获取摄像头对象。

参数:

  • index: (Number)可选 要获取摄像头的索引值指定要获取摄像头的索引值,1表示主摄像头,2表示辅摄像头。如果没有设置则使用系统默认主摄像头。

返回值:

: 摄像头对象

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Camera Example</title>
  6. <script type="text/javascript">
  7. // 扩展API加载完毕后调用onPlusReady回调函数
  8. document.addEventListener( "plusready", onPlusReady, false );
  9. var r = null;
  10. // 扩展API加载完毕,现在可以正常调用扩展API
  11. function onPlusReady() {
  12. // 获取设备默认的摄像头对象
  13. var cmr = plus.camera.getCamera();
  14. // ......
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. </body>
  20. </html>

uni-app使用plus注意事项

Camera

摄像头对象

  1. interface Camera {
  2. readonly attribute String[] supportedVideoResolutions;
  3. readonly attribute String[] supportedImageFormats;
  4. readonly attribute String[] supportedVideoFormats;
  5. function void captureImage(successCB, errorCB, option);
  6. function void startVideoCapture(successCB, errorCB, option);
  7. function void stopVideoCapture();
  8. }

属性:

  • : 字符串数组,摄像头支持的拍照分辨率
  • supportedVideoResolutions: 字符串数组,摄像头支持的摄像分辨率
  • : 字符串数组,摄像头支持的拍照文件格式
  • supportedVideoFormats: 字符串数组,摄像头支持的摄像文件格式

方法:

  • : 进行拍照操作
  • startVideoCapture: 调用摄像头进行摄像操作
  • : 结束摄像操作

supportedImageResolutions

字符串数组,摄像头支持的拍照分辨率

说明:

Array类型 只读属性

属性类型为String[],若不支持此属性则返回空数组对象。摄像头支持的拍照图片分辨率字符串形式“WIDTHHeight”,如“400800”;如果支持任意自定义分辨率则“*”。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Camera Example</title>
  6. <script type="text/javascript">
  7. // 扩展API加载完毕后调用onPlusReady回调函数
  8. document.addEventListener( "plusready", onPlusReady, false );
  9. // 扩展API加载完毕,现在可以正常调用扩展API
  10. function onPlusReady() {
  11. var cmr = plus.camera.getCamera();
  12. alert( "Camera supperted image resolutions: " + cmr.supportedImageResolutions );
  13. }
  14. </script>
  15. </head>
  16. <body>
  17. </body>
  18. </html>

uni-app使用plus注意事项

supportedVideoResolutions

字符串数组,摄像头支持的摄像分辨率

Array类型 只读属性

属性类型为String[],若不支持此属性则返回空数组对象。摄像头支持的视频分辨率字符串形式为“WIDTHHeight”,如“400800”;如果支持任意自定义分辨率则“*”。

示例:

supportedImageFormats

字符串数组,摄像头支持的拍照文件格式

说明:

属性类型为String[],若不支持此属性则返回空数组对象。摄像头支持的图片文件格式字符串形式为文件格式后缀名,如“jpg”、“png”、“bmp”。

示例:

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title>Camera Example</title>
  5. <script type="text/javascript">
  6. // 扩展API加载完毕后调用onPlusReady回调函数
  7. document.addEventListener( "plusready", onPlusReady, false );
  8. // 扩展API加载完毕,现在可以正常调用扩展API
  9. function onPlusReady() {
  10. var cmr = plus.camera.getCamera();
  11. alert( "Camera supperted image formats: " + cmr.supportedImageFormats );
  12. }
  13. </script>
  14. </head>
  15. <body>
  16. </body>
  17. </html>

uni-app使用plus注意事项

supportedVideoFormats

字符串数组,摄像头支持的摄像文件格式

说明:

Array类型 只读属性

属性类型为String[],若不支持此属性则返回空数组对象。摄像头支持的视频文件格式字符串形式为文件格式后缀名,如“3gp”、“mp4”、“avi”。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Camera Example</title>
  6. <script type="text/javascript">
  7. // 扩展API加载完毕后调用onPlusReady回调函数
  8. document.addEventListener( "plusready", onPlusReady, false );
  9. // 扩展API加载完毕,现在可以正常调用扩展API
  10. function onPlusReady() {
  11. var cmr = plus.camera.getCamera();
  12. alert( "Camera supperted video formats: " + cmr.supportedVideoFormats );
  13. }
  14. </script>
  15. </head>
  16. <body>
  17. </body>
  18. </html>

captureImage

进行拍照操作

  1. cmr.captureImage(successCB, errorCB, options);

说明:

摄像头资源为独占资源,如果其它程序或页面已经占用摄像头,再次操作则失败。拍照操作成功将通过successCB返回拍照获取的图片路径。可通过option设置摄像头的各种属性参数。

参数:

  • successCB: (CameraSuccessCallback)必选 拍照操作成功的回调函数
  • errorCB: ()可选 拍照操作失败的回调函数
  • options: (CameraOptions)必选 摄像头拍照参数

返回值:

void: 无

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Camera Example</title>
  6. <script type="text/javascript">
  7. // 扩展API加载完毕后调用onPlusReady回调函数
  8. document.addEventListener( "plusready", onPlusReady, false );
  9. function onPlusReady() {
  10. console.log("plusready");
  11. }
  12. // 拍照
  13. function captureImage(){
  14. var cmr = plus.camera.getCamera();
  15. var res = cmr.supportedImageResolutions[0];
  16. console.log("Resolution: "+res+", Format: "+fmt);
  17. cmr.captureImage( function( path ){
  18. alert( "Capture image success: " + path );
  19. },
  20. function( error ) {
  21. alert( "Capture image failed: " + error.message );
  22. },
  23. {resolution:res,format:fmt}
  24. );
  25. }
  26. </script>
  27. </head>
  28. <body>
  29. <button onclick="captureImage()">拍照</button>
  30. </body>
  31. </html>

startVideoCapture

调用摄像头进行摄像操作

说明:

摄像头资源为独占资源,如果其它程序或页面已经占用摄像头,再次操作则失败。拍照操作成功将通过successCB返回摄像获取的视频文件路径。可通过option设置摄像头的各种属性参数。

参数:

  • successCB: (CameraSuccessCallback)必选 摄像操作成功的回调函数
  • errorCB: ()可选 拍摄像操作失败的回调函数
  • option: (CameraOptions)必选 摄像头拍照参数

void: 无

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Camera Example</title>
  6. <script type="text/javascript">
  7. // 扩展API加载完毕后调用onPlusReady回调函数
  8. document.addEventListener( "plusready", onPlusReady, false );
  9. // 扩展API加载完毕,现在可以正常调用扩展API
  10. function onPlusReady() {
  11. console.log("plusready");
  12. }
  13. // 摄像
  14. function videoCapture(){
  15. var cmr = plus.camera.getCamera();
  16. var res = cmr.supportedVideoResolutions[0];
  17. var fmt = cmr.supportedVideoFormats[0];
  18. console.log("Resolution: "+res+", Format: "+fmt);
  19. cmr.startVideoCapture( function( path ){
  20. alert( "Capture video success: " + path );
  21. },
  22. function( error ) {
  23. alert( "Capture video failed: " + error.message );
  24. },
  25. {resolution:res,format:fmt}
  26. );
  27. }
  28. </script>
  29. </head>
  30. <body>
  31. <button onclick="videoCapture()">摄像</button>
  32. </body>
  33. </html>

stopVideoCapture

结束摄像操作

  1. cmr.stopVideoCapture();

说明:

参数:

返回值:

void: 无

平台支持:

  • Android- ALL(不支持): 暂不支持调用此API停止摄像,需要手动操作停止。
  • iOS- ALL(不支持): 暂不支持调用此API停止摄像,需要手动操作停止,也可通过videoMaximumDuration属性设置视频长度。

示例:


<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <title>Camera Example</title>
    <script type="text/javascript">
// 扩展API加载完毕后调用onPlusReady回调函数 
document.addEventListener( "plusready", onPlusReady, false );
// 扩展API加载完毕,现在可以正常调用扩展API 
function onPlusReady() {
    console.log("plusready");
}
var cmr=null;
// 摄像
function videoCapture(){
    cmr = plus.camera.getCamera();
    var res = cmr.supportedVideoResolutions[0];
    var fmt = cmr.supportedVideoFormats[0];
    console.log("Resolution: "+res+", Format: "+fmt);
    cmr.startVideoCapture( function( path ){
            alert( "Capture video success: " + path );  
        },
        function( error ) {
            alert( "Capture video failed: " + error.message );
        },
        {resolution:res,format:fmt}
    );
    // 拍摄10s后自动完成 
    setTimeout( stopCapture, 10000 );
}
// 停止摄像
function stopCapture(){
    console.log("stopCapture");
    cmr.stopVideoCapture();
}
    </script>
    </head>
    <body>
        <button onclick="videoCapture()">摄像</button><br/>
        <button onclick="stopCapture()">停止摄像</button>
    </body>
</html>
                        

uni-app使用plus注意事项

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表示辅摄像头。

  • Android - 2.2+ (不支持): 暂不支持设置默认使用的摄像头,忽略此属性值。打开拍摄界面后可操作切换。
  • iOS - 4.3+ (支持)
    • videoMaximumDuration: (Number类型)视频长度单位为秒(s),小于等于0表示不限定视频长度。默认值为0(不限定视频长度)。注意:仅在调用拍摄视频(startVideoCapture)时有效。
  • Android - ALL (不支持): HBuilderX1.9.7及以上版本支持。取决于ROM的拍照组件是否实现此功能,如果没实现此功能则忽略此属性。
  • iOS - ALL (支持)
    • optimize: (Boolean类型)是否优化图片自动调整图片的方向,在部分设备上可能出现图片方向不正确的问题,此参数将配置是否自动调整图片方向。可取值:true - 自动调整图片方向;false - 不调整。默认值为true。注意:自动调整图片方向将消耗部分系统资源,可能会导致拍照后回调触发时机延迟,将此值设置为false则可避免延迟问题。
  • Android - ALL (支持)
  • iOS - ALL (不支持): 忽略此属性。
    • resolution: (String类型)拍照或摄像使用的分辨率可通过Camera对象的supportedImageResolutions或supportedVideoResolutions获取,如果设置的参数无效则使用系统默认值。
  • Android - 2.2+ (不支持): 忽略此属性。
  • iOS - 4.3+ (支持): 设置摄像的分辨率,分辨率越高越清晰,文件也越大。
    • popover: (类型)拍照或摄像界面弹出指示区域对于大屏幕设备如iPad,拍照或摄像界面为弹出窗口,此时可通过此参数设置弹出窗口位置,其为JSON对象,格式如{top:"10px",left:"10px",width:"200px",height:"200px"},默认弹出位置为屏幕居中。
  • Android - ALL (不支持): 忽略此属性值
  • iOS - 5.0+ (支持): 仅iPad设备支持此属性,iPhone/iTouch上忽略此属性值

PopPosition

JSON对象,弹出拍照或摄像界面指示位置

属性:

  • top: (String类型)指示区域距离容器顶部的距离弹出拍照或摄像窗口指示区域距离容器顶部的距离,支持像素值(如"100px")和百分比(如"50%")。

  • left: (String类型)指示区域距离容器左侧的距离弹出拍照或摄像窗口指示区域距离容器左侧的距离,支持像素值(如"100px")和百分比(如"50%")。

  • width: (String类型)指示区域的宽度弹出拍照或摄像窗口指示区域的宽度,支持像素值(如"100px")和百分比(如"50%")。

  • height: (String类型)指示区域的高度弹出拍照或摄像窗口指示区域的高度,支持像素值(如"100px")和百分比(如"50%")。

CameraSuccessCallback

调用摄像头操作成功回调

说明:

调用摄像头操作成功的回调函数,在拍照或摄像操作成功时调用,用于返回图片或视频文件的路径。

参数:

  • capturedFile: (String)必选 拍照或摄像操作保存的文件路径

返回值:

void: 无

CameraErrorCallback




返回值:

void: 无