Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
d082df7d
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
d082df7d
编写于
8月 28, 2023
作者:
O
openharmony_ci
提交者:
Gitee
8月 28, 2023
浏览文件
操作
浏览文件
下载
差异文件
!23154 【monthly0815】cameraFramework ArkTS规范整改
Merge pull request !23154 from 章超123/cherry-pick-1693040205
上级
65568f8a
5402f994
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
574 addition
and
575 deletion
+574
-575
zh-cn/application-dev/media/camera-device-input.md
zh-cn/application-dev/media/camera-device-input.md
+23
-23
zh-cn/application-dev/media/camera-metadata.md
zh-cn/application-dev/media/camera-metadata.md
+14
-14
zh-cn/application-dev/media/camera-mode.md
zh-cn/application-dev/media/camera-mode.md
+102
-103
zh-cn/application-dev/media/camera-performance-improvement.md
...n/application-dev/media/camera-performance-improvement.md
+30
-30
zh-cn/application-dev/media/camera-preview.md
zh-cn/application-dev/media/camera-preview.md
+31
-31
zh-cn/application-dev/media/camera-recording-case.md
zh-cn/application-dev/media/camera-recording-case.md
+120
-120
zh-cn/application-dev/media/camera-recording.md
zh-cn/application-dev/media/camera-recording.md
+63
-63
zh-cn/application-dev/media/camera-session-management.md
zh-cn/application-dev/media/camera-session-management.md
+20
-20
zh-cn/application-dev/media/camera-shooting-case.md
zh-cn/application-dev/media/camera-shooting-case.md
+105
-105
zh-cn/application-dev/media/camera-shooting.md
zh-cn/application-dev/media/camera-shooting.md
+66
-66
未找到文件。
zh-cn/application-dev/media/camera-device-input.md
浏览文件 @
d082df7d
...
...
@@ -15,9 +15,9 @@
2.
通过getCameraManager()方法,获取cameraManager对象。
```
ts
let
cameraManager
;
let
context
:
any
=
getContext
(
this
);
cameraManager
=
camera
.
getCameraManager
(
context
)
let
cameraManager
:
camera
.
CameraManager
;
let
context
:
Context
=
getContext
(
this
);
// [各类Context的获取方式](../application-models/application-context-stage.md)
cameraManager
=
camera
.
getCameraManager
(
context
)
;
```
> **说明:**
...
...
@@ -27,17 +27,17 @@
3.
通过cameraManager类中的getSupportedCameras()方法,获取当前设备支持的相机列表,列表中存储了设备支持的所有相机ID。若列表不为空,则说明列表中的每个ID都支持独立创建相机对象;否则,说明当前设备无可用相机,不可继续后续操作。
```
ts
let
cameraArray
=
cameraManager
.
getSupportedCameras
();
if
(
cameraArray
.
length
<=
0
)
{
console
.
error
(
"
cameraManager.getSupportedCameras error
"
);
return
;
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
if
(
cameraArray
!=
undefined
&&
cameraArray
.
length
<=
0
)
{
console
.
error
(
"
cameraManager.getSupportedCameras error
"
);
return
;
}
for
(
let
index
=
0
;
index
<
cameraArray
.
length
;
index
++
)
{
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
}
```
...
...
@@ -45,24 +45,24 @@
```
ts
// 创建相机输入流
let
cameraInput
;
let
cameraInput
:
camera
.
CameraInput
;
try
{
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
error
.
code
);
}
// 监听cameraInput错误信息
let
cameraDevice
=
cameraArray
[
0
];
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
)
=>
{
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
})
let
cameraDevice
:
camera
.
CameraDevice
=
cameraArray
[
0
];
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
})
;
// 打开相机
await
cameraInput
.
open
();
// 获取相机设备支持的输出流能力
let
cameraOutputCapability
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
let
cameraOutputCapability
:
camera
.
CameraOutputCapability
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
if
(
!
cameraOutputCapability
)
{
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
);
return
;
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
);
return
;
}
console
.
info
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCapability
));
```
...
...
@@ -75,8 +75,8 @@
通过注册cameraStatus事件,通过回调返回监听结果,callback返回CameraStatusInfo参数,参数的具体内容可参考相机管理器回调接口实例
[
CameraStatusInfo
](
../reference/apis/js-apis-camera.md#camerastatusinfo
)
。
```
ts
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
,
c
ameraStatusInfo
)
=>
{
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
C
ameraStatusInfo
)
=>
{
console
.
info
(
`camera:
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
})
})
;
```
zh-cn/application-dev/media/camera-metadata.md
浏览文件 @
d082df7d
...
...
@@ -11,13 +11,13 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
1.
调用CameraOutputCapability类中的supportedMetadataObjectTypes()方法,获取当前设备支持的元数据类型,并通过createMetadataOutput()方法创建元数据输出流。
```
ts
let
metadataObjectTypes
=
cameraOutputCapability
.
supportedMetadataObjectTypes
;
let
metadataOutput
;
let
metadataObjectTypes
:
Array
<
camera
.
MetadataObjectType
>
=
cameraOutputCapability
.
supportedMetadataObjectTypes
;
let
metadataOutput
:
camera
.
MetadataOutput
;
try
{
metadataOutput
=
cameraManager
.
createMetadataOutput
(
metadataObjectTypes
);
metadataOutput
=
cameraManager
.
createMetadataOutput
(
metadataObjectTypes
);
}
catch
(
error
)
{
// 失败返回错误码error.code并处理
console
.
info
(
error
.
code
);
console
.
info
(
error
.
code
);
}
```
...
...
@@ -25,9 +25,9 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
```
ts
metadataOutput
.
start
().
then
(()
=>
{
console
.
info
(
'
Callback returned with metadataOutput started.
'
);
console
.
info
(
'
Callback returned with metadataOutput started.
'
);
}).
catch
((
err
)
=>
{
console
.
info
(
'
Failed to metadataOutput start
'
+
err
.
code
);
console
.
info
(
'
Failed to metadataOutput start
'
+
err
.
code
);
});
```
...
...
@@ -35,9 +35,9 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
```
ts
metadataOutput
.
stop
().
then
(()
=>
{
console
.
info
(
'
Callback returned with metadataOutput stopped.
'
);
console
.
info
(
'
Callback returned with metadataOutput stopped.
'
);
}).
catch
((
err
)
=>
{
console
.
info
(
'
Failed to metadataOutput stop
'
+
err
.
code
);
console
.
info
(
'
Failed to metadataOutput stop
'
+
err
.
code
);
});
```
...
...
@@ -48,9 +48,9 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
-
通过注册监听获取metadata对象,监听事件固定为metadataObjectsAvailable。检测到有效metadata数据时,callback返回相应的metadata数据信息,metadataOutput创建成功时可监听。
```
ts
metadataOutput
.
on
(
'
metadataObjectsAvailable
'
,
(
err
,
metadataObjectArr
)
=>
{
console
.
info
(
`metadata output metadataObjectsAvailable`
);
})
metadataOutput
.
on
(
'
metadataObjectsAvailable
'
,
(
err
:
BusinessError
,
metadataObjectArr
:
Array
<
camera
.
MetadataObject
>
)
=>
{
console
.
info
(
`metadata output metadataObjectsAvailable`
);
})
;
```
> **说明:**
...
...
@@ -60,7 +60,7 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
-
通过注册回调函数,获取监听metadata流的错误结果,callback返回metadata输出接口使用错误时返回的错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
```
ts
metadataOutput
.
on
(
'
error
'
,
(
metadataOutputError
)
=>
{
console
.
info
(
`Metadata output error code:
${
metadataOutputError
.
code
}
`
);
})
metadataOutput
.
on
(
'
error
'
,
(
metadataOutputError
:
BusinessError
)
=>
{
console
.
info
(
`Metadata output error code:
${
metadataOutputError
.
code
}
`
);
})
;
```
zh-cn/application-dev/media/camera-mode.md
浏览文件 @
d082df7d
...
...
@@ -11,248 +11,247 @@
## 完整示例
```
ts
import
camera
from
'
@ohos.multimedia.camera
'
import
image
from
'
@ohos.multimedia.image
'
import
media
from
'
@ohos.multimedia.media
'
import
camera
from
'
@ohos.multimedia.camera
'
;
import
image
from
'
@ohos.multimedia.image
'
;
import
media
from
'
@ohos.multimedia.media
'
;
// 创建CameraManager对象
context
:
any
=
getContext
(
this
)
let
cameraManager
=
camera
.
getCameraManager
(
this
.
context
)
let
context
:
Context
=
getContext
(
this
);
// [各类Context的获取方式](../application-models/application-context-stage.md
)
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
if
(
!
cameraManager
)
{
console
.
error
(
"
camera.getCameraManager error
"
)
return
;
console
.
error
(
"
camera.getCameraManager error
"
);
return
;
}
// 创建ModeManager对象
context
:
any
=
getContext
(
this
)
let
modeManager
=
camera
.
getModeManager
(
this
.
context
)
let
modeManager
:
camera
.
ModeManager
=
camera
.
getModeManager
(
context
);
if
(
!
cameraManager
)
{
console
.
error
(
"
camera.getModeManager error
"
)
return
;
console
.
error
(
"
camera.getModeManager error
"
);
return
;
}
// 监听相机状态变化
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
,
c
ameraStatusInfo
)
=>
{
console
.
info
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
})
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
C
ameraStatusInfo
)
=>
{
console
.
info
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
})
;
// 获取相机列表
let
cameraArray
=
cameraManager
.
getSupportedCameras
();
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
if
(
cameraArray
.
length
<=
0
)
{
console
.
error
(
"
cameraManager.getSupportedCameras error
"
)
return
;
console
.
error
(
"
cameraManager.getSupportedCameras error
"
);
return
;
}
for
(
let
index
=
0
;
index
<
cameraArray
.
length
;
index
++
)
{
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
}
// 获取模式列表
let
cameraModeArray
=
modeManager
.
getSupportedModes
(
cameraArray
[
0
]);
let
cameraModeArray
:
Array
<
camera
.
CameraMode
>
=
modeManager
.
getSupportedModes
(
cameraArray
[
0
]);
if
(
cameraModeArray
.
length
<=
0
)
{
console
.
error
(
"
modeManager.getSupportedModes error
"
)
return
;
console
.
error
(
"
modeManager.getSupportedModes error
"
);
return
;
}
// 创建相机输入流
let
cameraInput
let
cameraInput
:
camera
.
CameraInput
;
try
{
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
error
.
code
);
}
// 监听cameraInput错误信息
let
cameraDevice
=
cameraArray
[
0
];
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
)
=>
{
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
let
cameraDevice
:
camera
.
CameraDevice
=
cameraArray
[
0
];
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
})
// 打开相机
await
cameraInput
.
open
();
// 获取当前模式相机设备支持的输出流能力
let
cameraOutputCap
=
modeManager
.
getSupportedOutputCapability
(
cameraArray
[
0
],
cameraModeArray
[
0
]);
let
cameraOutputCap
:
camera
.
CameraOutputCapability
=
modeManager
.
getSupportedOutputCapability
(
cameraArray
[
0
],
cameraModeArray
[
0
]);
if
(
!
cameraOutputCap
)
{
console
.
error
(
"
modeManager.getSupportedOutputCapability error
"
)
return
;
console
.
error
(
"
modeManager.getSupportedOutputCapability error
"
)
return
;
}
console
.
info
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCap
));
let
previewProfilesArray
=
cameraOutputCap
.
previewProfiles
;
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
previewProfiles
;
if
(
!
previewProfilesArray
)
{
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
)
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
)
}
let
photoProfilesArray
=
cameraOutputCap
.
photoProfiles
;
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
photoProfiles
;
if
(
!
photoProfilesArray
)
{
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
)
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
)
}
// 创建预览输出流,其中参数 surfaceId 参考上文 XComponent 组件,预览流为XComponent组件提供的surface
let
previewOutput
let
previewOutput
:
camera
.
PreviewOutput
;
try
{
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
)
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
}
catch
(
error
)
{
console
.
error
(
"
Failed to create the PreviewOutput instance.
"
)
console
.
error
(
"
Failed to create the PreviewOutput instance.
"
);
}
// 监听预览输出错误信息
previewOutput
.
on
(
'
error
'
,
(
error
)
=>
{
console
.
info
(
`Preview output error code:
${
error
.
code
}
`
);
previewOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Preview output error code:
${
error
.
code
}
`
);
})
// 创建ImageReceiver对象,并设置照片参数:分辨率大小是根据前面 photoProfilesArray 获取的当前设备所支持的拍照分辨率大小去设置
let
imageReceiver
=
await
image
.
createImageReceiver
(
1920
,
1080
,
4
,
8
)
let
imageReceiver
:
image
.
ImageReceiver
=
await
image
.
createImageReceiver
(
1920
,
1080
,
4
,
8
)
// 获取照片显示SurfaceId
let
photoSurfaceId
=
await
imageReceiver
.
getReceivingSurfaceId
()
let
photoSurfaceId
:
string
=
await
imageReceiver
.
getReceivingSurfaceId
()
// 创建拍照输出流
let
photoOutput
let
photoOutput
:
camera
.
PhotoOutput
;
try
{
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
)
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
error
.
code
);
}
//创建portrait会话
let
portraitSession
let
portraitSession
:
camera
.
CaptureSession
;
try
{
portraitSession
=
modeManager
.
createCaptureSession
(
cameraModeArray
[
0
])
portraitSession
=
modeManager
.
createCaptureSession
(
cameraModeArray
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
error
.
code
);
}
// 监听portraitSession错误信息
portraitSession
.
on
(
'
error
'
,
(
error
)
=>
{
console
.
info
(
`Capture session error code:
${
error
.
code
}
`
);
})
portraitSession
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Capture session error code:
${
error
.
code
}
`
);
})
;
// 开始配置会话
try
{
portraitSession
.
beginConfig
()
portraitSession
.
beginConfig
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
}
// 向会话中添加相机输入流
try
{
portraitSession
.
addInput
(
cameraInput
)
portraitSession
.
addInput
(
cameraInput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addInput. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addInput. errorCode =
'
+
error
.
code
);
}
// 向会话中添加预览输出流
try
{
portraitSession
.
addOutput
(
previewOutput
)
portraitSession
.
addOutput
(
previewOutput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
error
.
code
);
}
// 向会话中添加拍照输出流
try
{
portraitSession
.
addOutput
(
photoOutput
)
portraitSession
.
addOutput
(
photoOutput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(photoOutput). errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addOutput(photoOutput). errorCode =
'
+
error
.
code
);
}
// 提交会话配置
await
portraitSession
.
commitConfig
()
await
portraitSession
.
commitConfig
()
;
// 启动会话
await
portraitSession
.
start
().
then
(()
=>
{
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
})
// 获取支持的美颜类型
let
beautyTypes
let
beautyTypes
:
Array
<
camera
.
BeautyType
>
;
try
{
beautyTypes
=
portraitSession
.
getSupportedBeautyTypes
()
beautyTypes
=
portraitSession
.
getSupportedBeautyTypes
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to get the beauty types. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to get the beauty types. errorCode =
'
+
error
.
code
);
}
// 获取支持的美颜类型对应的美颜强度范围
let
beautyRanges
let
beautyRanges
:
Array
<
number
>
;
try
{
beautyRanges
=
portraitSession
.
getSupportedBeautyRanges
(
beautyTypes
[
0
])
beautyRanges
=
portraitSession
.
getSupportedBeautyRanges
(
beautyTypes
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to get the beauty types ranges. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to get the beauty types ranges. errorCode =
'
+
error
.
code
);
}
// 设置美颜类型及对应的美颜强度
try
{
portraitSession
.
setBeauty
(
beautyTypes
[
0
],
beautyRanges
[
0
])
portraitSession
.
setBeauty
(
beautyTypes
[
0
],
beautyRanges
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the beauty type value. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to set the beauty type value. errorCode =
'
+
error
.
code
);
}
// 获取已经设置的美颜类型对应的美颜强度
let
beautyLevel
let
beautyLevel
:
number
;
try
{
beautyLevel
=
portraitSession
.
getBeauty
(
beautyTypes
[
0
])
beautyLevel
=
portraitSession
.
getBeauty
(
beautyTypes
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to get the beauty type value. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to get the beauty type value. errorCode =
'
+
error
.
code
);
}
// 获取支持的滤镜类型
let
filterTypes
let
filterTypes
:
Array
<
camera
.
FilterType
>
;
try
{
filterTypes
=
portraitSession
.
getSupportedFilters
()
filterTypes
=
portraitSession
.
getSupportedFilters
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to get the filter types. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to get the filter types. errorCode =
'
+
error
.
code
);
}
// 设置滤镜类型
try
{
portraitSession
.
setFilter
(
filterTypes
[
0
])
portraitSession
.
setFilter
(
filterTypes
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the filter type value. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to set the filter type value. errorCode =
'
+
error
.
code
);
}
// 获取已经设置的滤镜类型
let
filter
let
filter
:
number
;
try
{
filter
=
portraitSession
.
getFilter
()
filter
=
portraitSession
.
getFilter
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to get the filter type value. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to get the filter type value. errorCode =
'
+
error
.
code
);
}
// 获取支持的虚化类型
let
portraitTypes
let
portraitTypes
:
Array
<
camera
.
PortraitEffect
>
;
try
{
portraitTypes
=
portraitSession
.
getSupportedPortraitEffects
()
portraitTypes
=
portraitSession
.
getSupportedPortraitEffects
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to get the portrait effects types. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to get the portrait effects types. errorCode =
'
+
error
.
code
);
}
// 设置虚化类型
try
{
portraitSession
.
setPortraitEffect
(
portraitTypes
[
0
])
portraitSession
.
setPortraitEffect
(
portraitTypes
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the portrait effects value. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to set the portrait effects value. errorCode =
'
+
error
.
code
);
}
// 获取已经设置的虚化类型
let
effect
let
effect
:
camera
.
PortraitEffect
;
try
{
effect
=
portraitSession
.
getPortraitEffect
()
effect
=
portraitSession
.
getPortraitEffect
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to get the portrait effects value. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to get the portrait effects value. errorCode =
'
+
error
.
code
);
}
// 使用当前拍照设置进行拍照
photoOutput
.
capture
(
settings
,
async
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
photoOutput
.
capture
(
settings
,
async
(
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
});
// 停止当前会话
portraitSession
.
stop
()
portraitSession
.
stop
()
;
// 释放相机输入流
cameraInput
.
close
()
cameraInput
.
close
()
;
// 释放预览输出流
previewOutput
.
release
()
previewOutput
.
release
()
;
// 释放拍照输出流
photoOutput
.
release
()
photoOutput
.
release
()
;
// 释放会话
portraitSession
.
release
()
portraitSession
.
release
()
;
// 会话置空
portraitSession
=
null
portraitSession
=
null
;
```
zh-cn/application-dev/media/camera-performance-improvement.md
浏览文件 @
d082df7d
...
...
@@ -36,10 +36,10 @@ import camera from '@ohos.multimedia.camera';
function
async
preview
(
context
:
Context
,
cameraInfo
:
camera
.
Device
,
previewProfile
:
camera
.
Profile
,
photoProfile
:
camera
.
Profile
,
surfaceId
:
string
):
Promise
<
void
>
{
const
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
const
cameraInput
camera
.
CameraInput
=
await
cameraManager
.
createCameraInput
(
cameraInfo
)
const
cameraInput
camera
.
CameraInput
=
await
cameraManager
.
createCameraInput
(
cameraInfo
)
;
const
previewOutput
:
camera
.
PreviewOutput
=
await
cameraManager
.
createDeferredPreviewOutput
(
previewProfile
);
const
photoOutput
:
camera
.
PhotoOutput
=
await
cameraManager
.
createPhotoOutput
(
photoProfile
);
const
session
:
camera
.
CaptureSession
=
await
this
.
mCameraManager
.
createCaptureSession
();
const
session
:
camera
.
CaptureSession
=
await
mCameraManager
.
createCaptureSession
();
await
session
.
beginConfig
();
await
session
.
addInput
(
cameraInput
);
await
session
.
addOutput
(
previewOutput
);
...
...
@@ -80,32 +80,32 @@ function async preview(context: Context, cameraInfo: camera.Device, previewProfi
![](
figures/quick-thumbnail-sequence-diagram.png
)
```
js
import
camera
from
'
@ohos.multimedia.camera
'
this
.
cameraManager
=
camera
.
getCameraManager
(
globalThis
.
abilityC
ontext
);
let
cameras
=
this
.
cameraManager
.
getSupportedCameras
()
import
camera
from
'
@ohos.multimedia.camera
'
;
let
context
:
Context
=
getContext
(
this
);
// [各类Context的获取方式](../application-models/application-context-stage.md)
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
c
ontext
);
let
cameras
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
// 创建CaptureSession实例
this
.
captureSession
=
await
this
.
cameraManager
.
createCaptureSession
()
let
captureSession
:
camera
.
CaptureSession
=
await
cameraManager
.
createCaptureSession
();
// 开始配置会话
await
this
.
captureSession
.
beginConfig
()
await
captureSession
.
beginConfig
();
// 把CameraInput加入到会话
this
.
cameraInput
=
await
this
.
cameraManager
.
createCameraInput
(
cameras
[
0
])
await
this
.
cameraInput
.
open
()
await
this
.
captureSession
.
addInput
(
this
.
cameraInput
)
let
cameraInput
:
camera
.
CameraInput
=
await
cameraManager
.
createCameraInput
(
cameras
[
0
]);
await
cameraInput
.
open
();
await
captureSession
.
addInput
(
cameraInput
);
// 把PhotoOutPut加入到会话
this
.
photoOutPut
=
await
this
.
cameraManager
.
createPhotoOutput
(
photoProfile
,
surfaceId
)
await
this
.
captureSession
.
addOutput
(
this
.
photoOutPut
)
boolean
isSupported
=
this
.
photoOutPut
.
isQuickThumbnailSupported
()
let
photoOutPut
:
camera
.
PhotoOutput
=
await
cameraManager
.
createPhotoOutput
(
photoProfile
,
surfaceId
);
await
captureSession
.
addOutput
(
photoOutPut
);
let
isSupported
:
boolean
=
photoOutPut
.
isQuickThumbnailSupported
();
if
(
isSupported
)
{
// 使能快速缩略图
this
.
photoOutPut
.
enableQuickThumbnail
(
true
)
this
.
photoOutPut
.
on
(
'
quickThumbnail
'
,
(
err
,
pixelm
ap
)
=>
{
// 使能快速缩略图
photoOutPut
.
enableQuickThumbnail
(
true
);
photoOutPut
.
on
(
'
quickThumbnail
'
,
(
err
:
BusinessError
,
pixelmap
:
image
.
PixelM
ap
)
=>
{
if
(
err
||
pixelmap
===
undefined
)
{
Logger
.
error
(
this
.
tag
,
'
photoOutPut on thumbnail failed
'
)
return
console
.
error
(
'
photoOutPut on thumbnail failed
'
);
return
;
}
// 显示或保存pixelmap
this
.
showOrSavePicture
(
pixelmap
)
showOrSavePicture
(
pixelmap
);
})
}
```
...
...
@@ -138,13 +138,13 @@ if (isSupported) {
-
**桌面应用**
```
js
import
camera
from
'
@ohos.multimedia.camera
'
import
camera
from
'
@ohos.multimedia.camera
'
;
this
.
c
ameraManager
=
camera
.
getCameraManager
(
globalThis
.
abilityContext
);
let
cameraManager
:
camera
.
C
ameraManager
=
camera
.
getCameraManager
(
globalThis
.
abilityContext
);
try
{
this
.
cameraManager
.
prelaunch
();
cameraManager
.
prelaunch
();
}
catch
(
error
)
{
console
.
error
(
`catch error: Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
)
console
.
error
(
`catch error: Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
)
;
}
```
...
...
@@ -155,15 +155,15 @@ if (isSupported) {
具体申请方式及校验方式,请参考
[
访问控制授权申请指导
](
../security/accesstoken-guidelines.md
)
。
```
js
import
camera
from
'
@ohos.multimedia.camera
'
import
camera
from
'
@ohos.multimedia.camera
'
;
this
.
c
ameraManager
=
camera
.
getCameraManager
(
globalThis
.
abilityContext
);
let
cameras
=
this
.
cameraManager
.
getSupportedCameras
()
if
(
this
.
cameraManager
.
isPrelaunchSupported
(
cameras
[
0
]))
{
cameraManager
:
camera
.
C
ameraManager
=
camera
.
getCameraManager
(
globalThis
.
abilityContext
);
let
cameras
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
if
(
cameraManager
.
isPrelaunchSupported
(
cameras
[
0
]))
{
try
{
this
.
cameraManager
.
setPrelaunchConfig
({
cameraDevice
:
cameras
[
0
]});
cameraManager
.
setPrelaunchConfig
({
cameraDevice
:
cameras
[
0
]});
}
catch
(
error
)
{
console
.
error
(
`catch error: Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
)
console
.
error
(
`catch error: Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
)
;
}
}
```
zh-cn/application-dev/media/camera-preview.md
浏览文件 @
d082df7d
...
...
@@ -14,36 +14,36 @@
// 创建XComponentController
mXComponentController
:
XComponentController
=
new
XComponentController
;
build
()
{
Flex
()
{
// 创建XComponent
XComponent
({
id
:
''
,
type
:
'
surface
'
,
libraryname
:
''
,
controller
:
this
.
mXComponentController
})
.
onLoad
(()
=>
{
// 设置Surface宽高(1920*1080),预览尺寸设置参考前面 previewProfilesArray 获取的当前设备所支持的预览分辨率大小去设置
this
.
mXComponentController
.
setXComponentSurfaceSize
({
surfaceWidth
:
1920
,
surfaceHeight
:
1080
});
// 获取Surface ID
globalThis
.
surfaceId
=
this
.
mXComponentController
.
getXComponentSurfaceId
();
})
.
width
(
'
1920px
'
)
.
height
(
'
1080px
'
)
}
Flex
()
{
// 创建XComponent
XComponent
({
id
:
''
,
type
:
'
surface
'
,
libraryname
:
''
,
controller
:
this
.
mXComponentController
})
.
onLoad
(()
=>
{
// 设置Surface宽高(1920*1080),预览尺寸设置参考前面 previewProfilesArray 获取的当前设备所支持的预览分辨率大小去设置
this
.
mXComponentController
.
setXComponentSurfaceSize
({
surfaceWidth
:
1920
,
surfaceHeight
:
1080
});
// 获取Surface ID
globalThis
.
surfaceId
=
this
.
mXComponentController
.
getXComponentSurfaceId
();
})
.
width
(
'
1920px
'
)
.
height
(
'
1080px
'
)
}
}
```
2.
通过CameraOutputCapability类中的previewProfiles()方法获取当前设备支持的预览能力,返回previewProfilesArray数组 。通过createPreviewOutput()方法创建预览输出流,其中,createPreviewOutput()方法中的两个参数分别是previewProfilesArray数组中的第一项和步骤一中获取的surfaceId。
```
ts
let
previewProfilesArray
=
cameraOutputCapability
.
previewProfiles
;
let
previewOutput
;
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCapability
.
previewProfiles
;
let
previewOutput
:
camera
.
PreviewOutput
;
try
{
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
}
catch
(
error
)
{
console
.
error
(
"
Failed to create the PreviewOutput instance.
"
+
error
);
console
.
error
(
"
Failed to create the PreviewOutput instance.
"
+
error
);
}
```
...
...
@@ -51,9 +51,9 @@
```
ts
previewOutput
.
start
().
then
(()
=>
{
console
.
info
(
'
Callback returned with previewOutput started.
'
);
}).
catch
((
err
)
=>
{
console
.
info
(
'
Failed to previewOutput start
'
+
err
.
code
);
console
.
info
(
'
Callback returned with previewOutput started.
'
);
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
info
(
'
Failed to previewOutput start
'
+
err
.
code
);
});
```
...
...
@@ -66,22 +66,22 @@
```
ts
previewOutput
.
on
(
'
frameStart
'
,
()
=>
{
console
.
info
(
'
Preview frame started
'
);
})
console
.
info
(
'
Preview frame started
'
);
})
;
```
-
通过注册固定的frameEnd回调函数获取监听预览结束结果,previewOutput创建成功时即可监听,预览完成最后一帧时触发,有该事件返回结果则认为预览流已结束。
```
ts
previewOutput
.
on
(
'
frameEnd
'
,
()
=>
{
console
.
info
(
'
Preview frame ended
'
);
})
console
.
info
(
'
Preview frame ended
'
);
})
;
```
-
通过注册固定的error回调函数获取监听预览输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
```
ts
previewOutput
.
on
(
'
error
'
,
(
previewOutputError
)
=>
{
console
.
info
(
`Preview output error code:
${
previewOutputError
.
code
}
`
);
})
previewOutput
.
on
(
'
error
'
,
(
previewOutputError
:
BusinessError
)
=>
{
console
.
info
(
`Preview output error code:
${
previewOutputError
.
code
}
`
);
})
;
```
zh-cn/application-dev/media/camera-recording-case.md
浏览文件 @
d082df7d
...
...
@@ -10,238 +10,238 @@
## 完整示例
```
ts
import
camera
from
'
@ohos.multimedia.camera
'
import
media
from
'
@ohos.multimedia.media
'
import
camera
from
'
@ohos.multimedia.camera
'
;
import
media
from
'
@ohos.multimedia.media
'
;
// 创建CameraManager对象
context
:
any
=
getContext
(
this
)
let
cameraManager
=
camera
.
getCameraManager
(
this
.
context
)
let
context
:
Context
=
getContext
(
this
);
// [各类Context的获取方式](../application-models/application-context-stage.md
)
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
if
(
!
cameraManager
)
{
console
.
error
(
"
camera.getCameraManager error
"
)
return
;
console
.
error
(
"
camera.getCameraManager error
"
);
return
;
}
// 监听相机状态变化
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
,
c
ameraStatusInfo
)
=>
{
console
.
log
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
console
.
log
(
`status:
${
cameraStatusInfo
.
status
}
`
);
})
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
C
ameraStatusInfo
)
=>
{
console
.
log
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
console
.
log
(
`status:
${
cameraStatusInfo
.
status
}
`
);
})
;
// 获取相机设备支持的输出流能力
let
cameraOutputCap
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
let
cameraOutputCap
:
camera
.
CameraOutputCapability
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
if
(
!
cameraOutputCap
)
{
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
)
return
;
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
)
return
;
}
console
.
log
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCap
));
let
previewProfilesArray
=
cameraOutputCap
.
previewProfiles
;
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
previewProfiles
;
if
(
!
previewProfilesArray
)
{
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
)
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
);
}
let
photoProfilesArray
=
cameraOutputCap
.
photoProfiles
;
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
photoProfiles
;
if
(
!
photoProfilesArray
)
{
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
)
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
}
let
videoProfilesArray
=
cameraOutputCap
.
videoProfiles
;
let
videoProfilesArray
:
Array
<
camera
.
VideoProfile
>
=
cameraOutputCap
.
videoProfiles
;
if
(
!
videoProfilesArray
)
{
console
.
error
(
"
createOutput videoProfilesArray == null || undefined
"
)
console
.
error
(
"
createOutput videoProfilesArray == null || undefined
"
);
}
let
metadataObjectTypesArray
=
cameraOutputCap
.
supportedMetadataObjectTypes
;
let
metadataObjectTypesArray
:
Array
<
camera
.
MetadataObjectType
>
=
cameraOutputCap
.
supportedMetadataObjectTypes
;
if
(
!
metadataObjectTypesArray
)
{
console
.
error
(
"
createOutput metadataObjectTypesArray == null || undefined
"
)
console
.
error
(
"
createOutput metadataObjectTypesArray == null || undefined
"
);
}
// 配置参数以实际硬件设备支持的范围为准
let
AVRecorderProfile
=
{
audioBitrate
:
48000
,
audioChannels
:
2
,
audioCodec
:
media
.
CodecMimeType
.
AUDIO_AAC
,
audioSampleRate
:
48000
,
fileFormat
:
media
.
ContainerFormatType
.
CFT_MPEG_4
,
videoBitrate
:
2000000
,
videoCodec
:
media
.
CodecMimeType
.
VIDEO_MPEG4
,
videoFrameWidth
:
640
,
videoFrameHeight
:
480
,
videoFrameRate
:
30
}
audioBitrate
:
48000
,
audioChannels
:
2
,
audioCodec
:
media
.
CodecMimeType
.
AUDIO_AAC
,
audioSampleRate
:
48000
,
fileFormat
:
media
.
ContainerFormatType
.
CFT_MPEG_4
,
videoBitrate
:
2000000
,
videoCodec
:
media
.
CodecMimeType
.
VIDEO_MPEG4
,
videoFrameWidth
:
640
,
videoFrameHeight
:
480
,
videoFrameRate
:
30
}
;
let
AVRecorderConfig
=
{
audioSourceType
:
media
.
AudioSourceType
.
AUDIO_SOURCE_TYPE_MIC
,
videoSourceType
:
media
.
VideoSourceType
.
VIDEO_SOURCE_TYPE_SURFACE_YUV
,
profile
:
AVRecorderProfile
,
url
:
'
fd://
'
,
// 文件需先由调用者创建,赋予读写权限,将文件fd传给此参数,eg.fd://45--file:///data/media/01.mp4
rotation
:
0
,
// 合理值0、90、180、270,非合理值prepare接口将报错
location
:
{
latitude
:
30
,
longitude
:
130
}
}
let
avRecorder
media
.
createAVRecorder
((
error
,
r
ecorder
)
=>
{
if
(
recorder
!=
null
)
{
avRecorder
=
recorder
;
console
.
log
(
'
createAVRecorder success
'
);
}
else
{
console
.
log
(
`createAVRecorder fail, error:
${
error
}
`
);
}
audioSourceType
:
media
.
AudioSourceType
.
AUDIO_SOURCE_TYPE_MIC
,
videoSourceType
:
media
.
VideoSourceType
.
VIDEO_SOURCE_TYPE_SURFACE_YUV
,
profile
:
AVRecorderProfile
,
url
:
'
fd://
'
,
// 文件需先由调用者创建,赋予读写权限,将文件fd传给此参数,eg.fd://45--file:///data/media/01.mp4
rotation
:
0
,
// 合理值0、90、180、270,非合理值prepare接口将报错
location
:
{
latitude
:
30
,
longitude
:
130
}
}
;
let
avRecorder
:
media
.
AVRecorder
;
media
.
createAVRecorder
((
error
:
BusinessError
,
recorder
:
media
.
AVR
ecorder
)
=>
{
if
(
recorder
!=
null
)
{
avRecorder
=
recorder
;
console
.
log
(
'
createAVRecorder success
'
);
}
else
{
console
.
log
(
`createAVRecorder fail, error:
${
error
}
`
);
}
});
avRecorder
.
prepare
(
AVRecorderConfig
,
(
er
r
)
=>
{
if
(
err
==
null
)
{
console
.
log
(
'
prepare success
'
);
}
else
{
console
.
log
(
'
prepare failed and error is
'
+
err
.
message
);
}
avRecorder
.
prepare
(
AVRecorderConfig
:
media
.
AVRecorderConfig
,
(
err
:
BusinessErro
r
)
=>
{
if
(
err
==
null
)
{
console
.
log
(
'
prepare success
'
);
}
else
{
console
.
log
(
'
prepare failed and error is
'
+
err
.
message
);
}
})
let
videoSurfaceId
=
null
;
// 该surfaceID用于传递给相机接口创造videoOutput
avRecorder
.
getInputSurface
((
err
,
surfaceId
)
=>
{
if
(
err
==
null
)
{
console
.
log
(
'
getInputSurface success
'
);
videoSurfaceId
=
surfaceId
;
}
else
{
console
.
log
(
'
getInputSurface failed and error is
'
+
err
.
message
);
}
let
videoSurfaceId
:
string
=
null
;
// 该surfaceID用于传递给相机接口创造videoOutput
avRecorder
.
getInputSurface
((
err
:
BusinessError
,
surfaceId
:
string
)
=>
{
if
(
err
==
null
)
{
console
.
log
(
'
getInputSurface success
'
);
videoSurfaceId
=
surfaceId
;
}
else
{
console
.
log
(
'
getInputSurface failed and error is
'
+
err
.
message
);
}
});
// 创建VideoOutput对象
let
videoOutput
let
videoOutput
:
camera
.
VideoOutput
;
try
{
videoOutput
=
cameraManager
.
createVideoOutput
(
videoProfilesArray
[
0
],
videoSurfaceId
)
videoOutput
=
cameraManager
.
createVideoOutput
(
videoProfilesArray
[
0
],
videoSurfaceId
)
}
catch
(
error
)
{
console
.
error
(
'
Failed to create the videoOutput instance. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to create the videoOutput instance. errorCode =
'
+
error
.
code
);
}
// 监听视频输出错误信息
videoOutput
.
on
(
'
error
'
,
(
error
)
=>
{
console
.
log
(
`Preview output error code:
${
error
.
code
}
`
);
})
videoOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
log
(
`Preview output error code:
${
error
.
code
}
`
);
})
;
//创建会话
let
captureSession
let
captureSession
:
camera
.
CaptureSession
;
try
{
captureSession
=
cameraManager
.
createCaptureSession
()
captureSession
=
cameraManager
.
createCaptureSession
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
error
.
code
);
}
// 监听session错误信息
captureSession
.
on
(
'
error
'
,
(
error
)
=>
{
console
.
log
(
`Capture session error code:
${
error
.
code
}
`
);
})
captureSession
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
log
(
`Capture session error code:
${
error
.
code
}
`
);
})
;
// 开始配置会话
try
{
captureSession
.
beginConfig
()
captureSession
.
beginConfig
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
}
// 获取相机列表
let
cameraArray
=
cameraManager
.
getSupportedCameras
();
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
if
(
cameraArray
.
length
<=
0
)
{
console
.
error
(
"
cameraManager.getSupportedCameras error
"
)
return
;
console
.
error
(
"
cameraManager.getSupportedCameras error
"
)
return
;
}
// 创建相机输入流
let
cameraInput
let
cameraInput
:
camera
.
CameraInput
;
try
{
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
error
.
code
);
}
// 监听cameraInput错误信息
let
cameraDevice
=
cameraArray
[
0
];
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
)
=>
{
console
.
log
(
`Camera input error code:
${
error
.
code
}
`
);
})
let
cameraDevice
:
camera
.
CameraDevice
=
cameraArray
[
0
];
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
console
.
log
(
`Camera input error code:
${
error
.
code
}
`
);
})
;
// 打开相机
await
cameraInput
.
open
();
// 向会话中添加相机输入流
try
{
captureSession
.
addInput
(
cameraInput
)
captureSession
.
addInput
(
cameraInput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addInput. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addInput. errorCode =
'
+
error
.
code
);
}
// 创建预览输出流,其中参数 surfaceId 参考下面 XComponent 组件,预览流为XComponent组件提供的surface
let
previewOutput
let
previewOutput
:
camera
.
PreviewOutput
;
try
{
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
)
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
}
catch
(
error
)
{
console
.
error
(
"
Failed to create the PreviewOutput instance.
"
)
console
.
error
(
"
Failed to create the PreviewOutput instance.
"
)
}
// 向会话中添加预览输入流
try
{
captureSession
.
addOutput
(
previewOutput
)
captureSession
.
addOutput
(
previewOutput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
error
.
code
);
}
// 向会话中添加录像输出流
try
{
captureSession
.
addOutput
(
videoOutput
)
captureSession
.
addOutput
(
videoOutput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(videoOutput). errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addOutput(videoOutput). errorCode =
'
+
error
.
code
);
}
// 提交会话配置
await
captureSession
.
commitConfig
()
await
captureSession
.
commitConfig
()
;
// 启动会话
await
captureSession
.
start
().
then
(()
=>
{
console
.
log
(
'
Promise returned to indicate the session start success.
'
);
})
console
.
log
(
'
Promise returned to indicate the session start success.
'
);
})
;
// 启动录像输出流
videoOutput
.
start
(
async
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to start the video output ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate the video output start success.
'
);
videoOutput
.
start
(
async
(
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to start the video output ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate the video output start success.
'
);
});
// 开始录像
avRecorder
.
start
().
then
(()
=>
{
console
.
log
(
'
videoRecorder start success
'
);
})
console
.
log
(
'
videoRecorder start success
'
);
})
;
// 停止录像输出流
videoOutput
.
stop
((
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to stop the video output ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate the video output stop success.
'
);
videoOutput
.
stop
((
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to stop the video output ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate the video output stop success.
'
);
});
// 停止录像
avRecorder
.
stop
().
then
(()
=>
{
console
.
log
(
'
stop success
'
);
})
console
.
log
(
'
stop success
'
);
})
;
// 停止当前会话
captureSession
.
stop
()
captureSession
.
stop
()
;
// 释放相机输入流
cameraInput
.
close
()
cameraInput
.
close
()
;
// 释放预览输出流
previewOutput
.
release
()
previewOutput
.
release
()
;
// 释放录像输出流
videoOutput
.
release
()
videoOutput
.
release
()
;
// 释放会话
captureSession
.
release
()
captureSession
.
release
()
;
// 会话置空
captureSession
=
null
captureSession
=
null
;
```
zh-cn/application-dev/media/camera-recording.md
浏览文件 @
d082df7d
...
...
@@ -17,30 +17,30 @@
系统提供的media接口可以创建一个录像AVRecorder实例,通过该实例的getInputSurface方法获取SurfaceId,与录像输出流做关联,处理录像输出流输出的数据。
```
ts
let
AVRecorder
;
media
.
createAVRecorder
((
error
,
r
ecorder
)
=>
{
if
(
recorder
!=
null
)
{
AV
Recorder
=
recorder
;
console
.
info
(
'
createAVRecorder success
'
);
}
else
{
console
.
info
(
`createAVRecorder fail, error:
${
error
}
`
);
}
let
avRecorder
:
media
.
AVRecorder
;
media
.
createAVRecorder
((
error
:
BusinessError
,
recorder
:
media
.
AVR
ecorder
)
=>
{
if
(
recorder
!=
null
)
{
av
Recorder
=
recorder
;
console
.
info
(
'
createAVRecorder success
'
);
}
else
{
console
.
info
(
`createAVRecorder fail, error:
${
error
}
`
);
}
});
// AVRecorderConfig可参考下一章节
AVRecorder
.
prepare
(
AVRecorderConfig
,
(
er
r
)
=>
{
if
(
err
==
null
)
{
console
.
log
(
'
prepare success
'
);
}
else
{
console
.
log
(
'
prepare failed and error is
'
+
err
.
message
);
}
})
avRecorder
.
prepare
(
AVRecorderConfig
:
media
.
AVRecorderConfig
,
(
err
:
BusinessErro
r
)
=>
{
if
(
err
==
null
)
{
console
.
log
(
'
prepare success
'
);
}
else
{
console
.
log
(
'
prepare failed and error is
'
+
err
.
message
);
}
})
;
let
videoSurfaceId
=
null
;
AVRecorder
.
getInputSurface
().
then
((
surfaceId
)
=>
{
console
.
info
(
'
getInputSurface success
'
);
videoSurfaceId
=
surfaceId
;
let
videoSurfaceId
:
string
=
null
;
avRecorder
.
getInputSurface
().
then
((
surfaceId
:
string
)
=>
{
console
.
info
(
'
getInputSurface success
'
);
videoSurfaceId
=
surfaceId
;
}).
catch
((
err
)
=>
{
console
.
info
(
'
getInputSurface failed and catch error is
'
+
err
.
message
);
console
.
info
(
'
getInputSurface failed and catch error is
'
+
err
.
message
);
});
```
...
...
@@ -49,43 +49,43 @@
通过CameraOutputCapability类中的videoProfiles,可获取当前设备支持的录像输出流。然后,定义创建录像的参数,通过createVideoOutput方法创建录像输出流。
```
ts
let
videoProfilesArray
=
cameraOutputCapability
.
videoProfiles
;
let
videoProfilesArray
:
Array
<
camera
.
VideoProfile
>
=
cameraOutputCapability
.
videoProfiles
;
if
(
!
videoProfilesArray
)
{
console
.
error
(
"
createOutput videoProfilesArray == null || undefined
"
);
console
.
error
(
"
createOutput videoProfilesArray == null || undefined
"
);
}
// 创建视频录制的参数
let
videoConfig
=
{
videoSourceType
:
media
.
VideoSourceType
.
VIDEO_SOURCE_TYPE_SURFACE_YUV
,
profile
:
{
fileFormat
:
media
.
ContainerFormatType
.
CFT_MPEG_4
,
// 视频文件封装格式,只支持MP4
videoBitrate
:
100000
,
// 视频比特率
videoCodec
:
media
.
CodecMimeType
.
VIDEO_MPEG4
,
// 视频文件编码格式,支持mpeg4和avc两种格式
videoFrameWidth
:
640
,
// 视频分辨率的宽
videoFrameHeight
:
480
,
// 视频分辨率的高
videoFrameRate
:
30
// 视频帧率
},
url
:
'
fd://35
'
,
rotation
:
90
// 90°为默认竖屏显示角度,如果由于设备原因或应用期望以其他方式显示等原因,请根据实际情况调整该参数
videoSourceType
:
media
.
VideoSourceType
.
VIDEO_SOURCE_TYPE_SURFACE_YUV
,
profile
:
{
fileFormat
:
media
.
ContainerFormatType
.
CFT_MPEG_4
,
// 视频文件封装格式,只支持MP4
videoBitrate
:
100000
,
// 视频比特率
videoCodec
:
media
.
CodecMimeType
.
VIDEO_MPEG4
,
// 视频文件编码格式,支持mpeg4和avc两种格式
videoFrameWidth
:
640
,
// 视频分辨率的宽
videoFrameHeight
:
480
,
// 视频分辨率的高
videoFrameRate
:
30
// 视频帧率
},
url
:
'
fd://35
'
,
rotation
:
90
// 90°为默认竖屏显示角度,如果由于设备原因或应用期望以其他方式显示等原因,请根据实际情况调整该参数
}
// 创建avRecorder
let
avRecorder
;
media
.
createAVRecorder
((
error
,
r
ecorder
)
=>
{
let
avRecorder
:
media
.
AVRecorder
;
media
.
createAVRecorder
((
error
:
BusinessError
,
recorder
:
media
.
AVR
ecorder
)
=>
{
if
(
recorder
!=
null
)
{
avRecorder
=
recorder
;
console
.
info
(
'
createAVRecorder success
'
);
avRecorder
=
recorder
;
console
.
info
(
'
createAVRecorder success
'
);
}
else
{
console
.
info
(
`createAVRecorder fail, error:
${
error
}
`
);
console
.
info
(
`createAVRecorder fail, error:
${
error
}
`
);
}
});
// 设置视频录制的参数
avRecorder
.
prepare
(
videoConfig
);
// 创建VideoOutput对象
let
videoOutput
;
let
videoOutput
:
camera
.
VideoOutput
;
try
{
videoOutput
=
cameraManager
.
createVideoOutput
(
videoProfilesArray
[
0
],
videoSurfaceId
);
videoOutput
=
cameraManager
.
createVideoOutput
(
videoProfilesArray
[
0
],
videoSurfaceId
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to create the videoOutput instance. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to create the videoOutput instance. errorCode =
'
+
error
.
code
);
}
```
...
...
@@ -93,17 +93,17 @@
先通过videoOutput的start方法启动录像输出流,再通过avRecorder的start方法开始录像。
```
videoOutput.start(async (err) => {
if (err) {
console.error('Failed to start the video output ${err.message}');
return;
}
console.info('Callback invoked to indicate the video output start success.');
```
ts
videoOutput
.
start
(
async
(
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to start the video output ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the video output start success.
'
);
});
avRecorder
.
start
().
then
(()
=>
{
console.info('avRecorder start success');
console
.
info
(
'
avRecorder start success
'
);
});
```
...
...
@@ -113,15 +113,15 @@
```
ts
videoRecorder
.
stop
().
then
(()
=>
{
console
.
info
(
'
stop success
'
);
console
.
info
(
'
stop success
'
);
});
videoOutput
.
stop
((
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to stop the video output ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the video output stop success.
'
);
videoOutput
.
stop
((
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to stop the video output ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the video output stop success.
'
);
});
```
...
...
@@ -134,22 +134,22 @@
```
ts
videoOutput
.
on
(
'
frameStart
'
,
()
=>
{
console
.
info
(
'
Video frame started
'
);
})
console
.
info
(
'
Video frame started
'
);
})
;
```
-
通过注册固定的frameEnd回调函数获取监听录像结束结果,videoOutput创建成功时即可监听,录像完成最后一帧时触发,有该事件返回结果则认为录像流已结束。
```
ts
videoOutput
.
on
(
'
frameEnd
'
,
()
=>
{
console
.
info
(
'
Video frame ended
'
);
})
console
.
info
(
'
Video frame ended
'
);
})
;
```
-
通过注册固定的error回调函数获取监听录像输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
```
ts
videoOutput
.
on
(
'
error
'
,
(
error
)
=>
{
console
.
info
(
`Video output error code:
${
error
.
code
}
`
);
})
videoOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Video output error code:
${
error
.
code
}
`
);
})
;
```
zh-cn/application-dev/media/camera-session-management.md
浏览文件 @
d082df7d
...
...
@@ -18,11 +18,11 @@
1.
调用cameraManager类中的createCaptureSession()方法创建一个会话。
```
ts
let
captureSession
;
let
captureSession
:
camera
.
CaptureSession
;
try
{
captureSession
=
cameraManager
.
createCaptureSession
();
captureSession
=
cameraManager
.
createCaptureSession
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
error
.
code
);
}
```
...
...
@@ -30,9 +30,9 @@
```
ts
try
{
captureSession
.
beginConfig
();
captureSession
.
beginConfig
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
}
```
...
...
@@ -42,24 +42,24 @@
```
ts
try
{
captureSession
.
addInput
(
cameraInput
);
captureSession
.
addInput
(
cameraInput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addInput. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addInput. errorCode =
'
+
error
.
code
);
}
try
{
captureSession
.
addOutput
(
previewOutput
);
captureSession
.
addOutput
(
previewOutput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
error
.
code
);
}
try
{
captureSession
.
addOutput
(
photoOutput
);
captureSession
.
addOutput
(
photoOutput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(photoOutput). errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addOutput(photoOutput). errorCode =
'
+
error
.
code
);
}
await
captureSession
.
commitConfig
()
;
await
captureSession
.
commitConfig
();
await
captureSession
.
start
().
then
(()
=>
{
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
})
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
})
;
```
4.
会话控制。调用captureSession类中的stop()方法可以停止当前会话。调用removeOutput()和addOutput()方法可以完成会话切换控制。以下示例代码以移除拍照流photoOutput,添加视频流videoOutput为例,完成了拍照到录像的切换。
...
...
@@ -67,20 +67,20 @@
```
ts
await
captureSession
.
stop
();
try
{
captureSession
.
beginConfig
();
captureSession
.
beginConfig
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
}
// 从会话中移除拍照输出流
try
{
captureSession
.
removeOutput
(
photoOutput
);
captureSession
.
removeOutput
(
photoOutput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to removeOutput(photoOutput). errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to removeOutput(photoOutput). errorCode =
'
+
error
.
code
);
}
// 向会话中添加视频输出流
try
{
captureSession
.
addOutput
(
videoOutput
);
captureSession
.
addOutput
(
videoOutput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(videoOutput). errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addOutput(videoOutput). errorCode =
'
+
error
.
code
);
}
```
zh-cn/application-dev/media/camera-shooting-case.md
浏览文件 @
d082df7d
...
...
@@ -9,232 +9,232 @@
## 完整示例
```
ts
import
camera
from
'
@ohos.multimedia.camera
'
import
image
from
'
@ohos.multimedia.image
'
import
media
from
'
@ohos.multimedia.media
'
import
camera
from
'
@ohos.multimedia.camera
'
;
import
image
from
'
@ohos.multimedia.image
'
;
import
media
from
'
@ohos.multimedia.media
'
;
// 创建CameraManager对象
context
:
any
=
getContext
(
this
)
let
cameraManager
=
camera
.
getCameraManager
(
this
.
context
)
let
context
:
Context
=
getContext
(
this
);
// [各类Context的获取方式](../application-models/application-context-stage.md
)
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
if
(
!
cameraManager
)
{
console
.
error
(
"
camera.getCameraManager error
"
)
return
;
console
.
error
(
"
camera.getCameraManager error
"
);
return
;
}
// 监听相机状态变化
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
,
c
ameraStatusInfo
)
=>
{
console
.
info
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
})
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
C
ameraStatusInfo
)
=>
{
console
.
info
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
})
;
// 获取相机列表
let
cameraArray
=
cameraManager
.
getSupportedCameras
();
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
if
(
cameraArray
.
length
<=
0
)
{
console
.
error
(
"
cameraManager.getSupportedCameras error
"
)
return
;
console
.
error
(
"
cameraManager.getSupportedCameras error
"
);
return
;
}
for
(
let
index
=
0
;
index
<
cameraArray
.
length
;
index
++
)
{
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
}
// 创建相机输入流
let
cameraInput
let
cameraInput
:
camera
.
CameraInput
;
try
{
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
error
.
code
);
}
// 监听cameraInput错误信息
let
cameraDevice
=
cameraArray
[
0
];
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
)
=>
{
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
let
cameraDevice
:
camera
.
CameraDevice
=
cameraArray
[
0
];
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
})
// 打开相机
await
cameraInput
.
open
();
// 获取相机设备支持的输出流能力
let
cameraOutputCap
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
let
cameraOutputCap
:
camera
.
CameraOutputCapability
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
if
(
!
cameraOutputCap
)
{
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
)
return
;
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
);
return
;
}
console
.
info
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCap
));
let
previewProfilesArray
=
cameraOutputCap
.
previewProfiles
;
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
previewProfiles
;
if
(
!
previewProfilesArray
)
{
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
)
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
);
}
let
photoProfilesArray
=
cameraOutputCap
.
photoProfiles
;
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
photoProfiles
;
if
(
!
photoProfilesArray
)
{
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
)
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
}
// 创建预览输出流,其中参数 surfaceId 参考上文 XComponent 组件,预览流为XComponent组件提供的surface
let
previewOutput
let
previewOutput
:
camera
.
PreviewOutput
;
try
{
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
)
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
}
catch
(
error
)
{
console
.
error
(
"
Failed to create the PreviewOutput instance.
"
)
console
.
error
(
"
Failed to create the PreviewOutput instance.
"
);
}
// 监听预览输出错误信息
previewOutput
.
on
(
'
error
'
,
(
error
)
=>
{
console
.
info
(
`Preview output error code:
${
error
.
code
}
`
);
})
previewOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Preview output error code:
${
error
.
code
}
`
);
})
;
// 创建ImageReceiver对象,并设置照片参数:分辨率大小是根据前面 photoProfilesArray 获取的当前设备所支持的拍照分辨率大小去设置
let
imageReceiver
=
await
image
.
createImageReceiver
(
1920
,
1080
,
4
,
8
)
let
imageReceiver
:
image
.
ImageReceiver
=
await
image
.
createImageReceiver
(
1920
,
1080
,
4
,
8
);
// 获取照片显示SurfaceId
let
photoSurfaceId
=
await
imageReceiver
.
getReceivingSurfaceId
()
let
photoSurfaceId
:
string
=
await
imageReceiver
.
getReceivingSurfaceId
();
// 创建拍照输出流
let
photoOutput
let
photoOutput
:
camera
.
PhotoOutput
;
try
{
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
)
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
error
.
code
);
}
//创建会话
let
captureSession
let
captureSession
:
camera
.
CaptureSession
;
try
{
captureSession
=
cameraManager
.
createCaptureSession
()
captureSession
=
cameraManager
.
createCaptureSession
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
error
.
code
);
}
// 监听session错误信息
captureSession
.
on
(
'
error
'
,
(
error
)
=>
{
console
.
info
(
`Capture session error code:
${
error
.
code
}
`
);
})
captureSession
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Capture session error code:
${
error
.
code
}
`
);
})
;
// 开始配置会话
try
{
captureSession
.
beginConfig
()
captureSession
.
beginConfig
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
}
// 向会话中添加相机输入流
try
{
captureSession
.
addInput
(
cameraInput
)
captureSession
.
addInput
(
cameraInput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addInput. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addInput. errorCode =
'
+
error
.
code
);
}
// 向会话中添加预览输出流
try
{
captureSession
.
addOutput
(
previewOutput
)
captureSession
.
addOutput
(
previewOutput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
error
.
code
);
}
// 向会话中添加拍照输出流
try
{
captureSession
.
addOutput
(
photoOutput
)
captureSession
.
addOutput
(
photoOutput
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(photoOutput). errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to addOutput(photoOutput). errorCode =
'
+
error
.
code
);
}
// 提交会话配置
await
captureSession
.
commitConfig
()
await
captureSession
.
commitConfig
()
;
// 启动会话
await
captureSession
.
start
().
then
(()
=>
{
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
})
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
})
;
// 判断设备是否支持闪光灯
let
flashStatus
let
flashStatus
:
boolean
;
try
{
flashStatus
=
captureSession
.
hasFlash
()
flashStatus
=
captureSession
.
hasFlash
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to hasFlash. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to hasFlash. errorCode =
'
+
error
.
code
);
}
console
.
info
(
'
Promise returned with the flash light support status:
'
+
flashStatus
);
if
(
flashStatus
)
{
// 判断是否支持自动闪光灯模式
let
flashModeStatus
// 判断是否支持自动闪光灯模式
let
flashModeStatus
:
boolean
;
try
{
let
status
:
boolean
=
captureSession
.
isFlashModeSupported
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
flashModeStatus
=
status
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to check whether the flash mode is supported. errorCode =
'
+
error
.
code
);
}
if
(
flashModeStatus
)
{
// 设置自动闪光灯模式
try
{
let
status
=
captureSession
.
isFlashModeSupported
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
)
flashModeStatus
=
status
captureSession
.
setFlashMode
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to check whether the flash mode is supported. errorCode =
'
+
error
.
code
);
}
if
(
flashModeStatus
)
{
// 设置自动闪光灯模式
try
{
captureSession
.
setFlashMode
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
)
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the flash mode. errorCode =
'
+
error
.
code
);
}
console
.
error
(
'
Failed to set the flash mode. errorCode =
'
+
error
.
code
);
}
}
}
// 判断是否支持连续自动变焦模式
let
focusModeStatus
let
focusModeStatus
:
boolean
;
try
{
let
status
=
captureSession
.
isFocusModeSupported
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
)
focusModeStatus
=
status
let
status
:
boolean
=
captureSession
.
isFocusModeSupported
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
focusModeStatus
=
status
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to check whether the focus mode is supported. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to check whether the focus mode is supported. errorCode =
'
+
error
.
code
);
}
if
(
focusModeStatus
)
{
// 设置连续自动变焦模式
try
{
captureSession
.
setFocusMode
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
)
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the focus mode. errorCode =
'
+
error
.
code
);
}
// 设置连续自动变焦模式
try
{
captureSession
.
setFocusMode
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the focus mode. errorCode =
'
+
error
.
code
);
}
}
// 获取相机支持的可变焦距比范围
let
zoomRatioRange
let
zoomRatioRange
;
try
{
zoomRatioRange
=
captureSession
.
getZoomRatioRange
()
zoomRatioRange
:
Array
<
number
>
=
captureSession
.
getZoomRatioRange
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to get the zoom ratio range. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to get the zoom ratio range. errorCode =
'
+
error
.
code
);
}
// 设置可变焦距比
try
{
captureSession
.
setZoomRatio
(
zoomRatioRange
[
0
])
captureSession
.
setZoomRatio
(
zoomRatioRange
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the zoom ratio value. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to set the zoom ratio value. errorCode =
'
+
error
.
code
);
}
let
settings
=
{
quality
:
camera
.
QualityLevel
.
QUALITY_LEVEL_HIGH
,
// 设置图片质量高
rotation
:
camera
.
ImageRotation
.
ROTATION_0
// 设置图片旋转角度0
let
settings
:
camera
.
PhotoCaptureSetting
=
{
quality
:
camera
.
QualityLevel
.
QUALITY_LEVEL_HIGH
,
// 设置图片质量高
rotation
:
camera
.
ImageRotation
.
ROTATION_0
// 设置图片旋转角度0
}
// 使用当前拍照设置进行拍照
photoOutput
.
capture
(
settings
,
async
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
photoOutput
.
capture
(
settings
,
async
(
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
});
// 停止当前会话
captureSession
.
stop
()
captureSession
.
stop
()
;
// 释放相机输入流
cameraInput
.
close
()
cameraInput
.
close
()
;
// 释放预览输出流
previewOutput
.
release
()
previewOutput
.
release
()
;
// 释放拍照输出流
photoOutput
.
release
()
photoOutput
.
release
()
;
// 释放会话
captureSession
.
release
()
captureSession
.
release
()
;
// 会话置空
captureSession
=
null
captureSession
=
null
;
```
zh-cn/application-dev/media/camera-shooting.md
浏览文件 @
d082df7d
...
...
@@ -18,15 +18,15 @@
```
ts
function
getImageReceiverSurfaceId
()
{
let
r
eceiver
=
image
.
createImageReceiver
(
640
,
480
,
4
,
8
);
console
.
info
(
'
before ImageReceiver check
'
);
if
(
receiver
!==
undefined
)
{
console
.
info
(
'
ImageReceiver is ok
'
);
let
photoSurfaceId
=
receiver
.
getReceivingSurfaceId
();
console
.
info
(
'
ImageReceived id:
'
+
JSON
.
stringify
(
photoSurfaceId
));
}
else
{
console
.
info
(
'
ImageReceiver is not ok
'
);
}
let
receiver
:
image
.
ImageR
eceiver
=
image
.
createImageReceiver
(
640
,
480
,
4
,
8
);
console
.
info
(
'
before ImageReceiver check
'
);
if
(
receiver
!==
undefined
)
{
console
.
info
(
'
ImageReceiver is ok
'
);
let
photoSurfaceId
:
string
=
receiver
.
getReceivingSurfaceId
();
console
.
info
(
'
ImageReceived id:
'
+
JSON
.
stringify
(
photoSurfaceId
));
}
else
{
console
.
info
(
'
ImageReceiver is not ok
'
);
}
}
```
...
...
@@ -35,15 +35,15 @@
通过CameraOutputCapability类中的photoProfiles()方法,可获取当前设备支持的拍照输出流,通过createPhotoOutput()方法传入支持的某一个输出流及步骤一获取的SurfaceId创建拍照输出流。
```
ts
let
photoProfilesArray
=
cameraOutputCapability
.
photoProfiles
;
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCapability
.
photoProfiles
;
if
(
!
photoProfilesArray
)
{
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
}
let
photoOutput
;
let
photoOutput
:
camera
.
PhotoOutput
;
try
{
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
);
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
error
.
code
);
}
```
...
...
@@ -53,59 +53,59 @@
```
ts
// 判断设备是否支持闪光灯
let
flashStatus
;
let
flashStatus
:
boolean
;
try
{
flashStatus
=
captureSession
.
hasFlash
();
flashStatus
=
captureSession
.
hasFlash
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to hasFlash. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to hasFlash. errorCode =
'
+
error
.
code
);
}
console
.
info
(
'
Promise returned with the flash light support status:
'
+
flashStatus
);
if
(
flashStatus
)
{
// 判断是否支持自动闪光灯模式
let
flashModeStatus
;
// 判断是否支持自动闪光灯模式
let
flashModeStatus
:
boolean
;
try
{
let
status
:
boolean
=
captureSession
.
isFlashModeSupported
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
flashModeStatus
=
status
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to check whether the flash mode is supported. errorCode =
'
+
error
.
code
);
}
if
(
flashModeStatus
)
{
// 设置自动闪光灯模式
try
{
let
status
=
captureSession
.
isFlashModeSupported
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
flashModeStatus
=
status
;
captureSession
.
setFlashMode
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to check whether the flash mode is supported. errorCode =
'
+
error
.
code
);
}
if
(
flashModeStatus
)
{
// 设置自动闪光灯模式
try
{
captureSession
.
setFlashMode
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the flash mode. errorCode =
'
+
error
.
code
);
}
console
.
error
(
'
Failed to set the flash mode. errorCode =
'
+
error
.
code
);
}
}
}
// 判断是否支持连续自动变焦模式
let
focusModeStatus
;
let
focusModeStatus
:
boolean
;
try
{
let
status
=
captureSession
.
isFocusModeSupported
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
focusModeStatus
=
status
;
let
status
:
boolean
=
captureSession
.
isFocusModeSupported
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
focusModeStatus
=
status
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to check whether the focus mode is supported. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to check whether the focus mode is supported. errorCode =
'
+
error
.
code
);
}
if
(
focusModeStatus
)
{
// 设置连续自动变焦模式
try
{
captureSession
.
setFocusMode
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the focus mode. errorCode =
'
+
error
.
code
);
}
// 设置连续自动变焦模式
try
{
captureSession
.
setFocusMode
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the focus mode. errorCode =
'
+
error
.
code
);
}
}
// 获取相机支持的可变焦距比范围
let
zoomRatioRange
;
let
zoomRatioRange
:
Array
<
number
>
;
try
{
zoomRatioRange
=
captureSession
.
getZoomRatioRange
();
zoomRatioRange
=
captureSession
.
getZoomRatioRange
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to get the zoom ratio range. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to get the zoom ratio range. errorCode =
'
+
error
.
code
);
}
// 设置可变焦距比
try
{
captureSession
.
setZoomRatio
(
zoomRatioRange
[
0
]);
captureSession
.
setZoomRatio
(
zoomRatioRange
[
0
]);
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the zoom ratio value. errorCode =
'
+
error
.
code
);
console
.
error
(
'
Failed to set the zoom ratio value. errorCode =
'
+
error
.
code
);
}
```
...
...
@@ -114,18 +114,18 @@
通过photoOutput类的capture()方法,执行拍照任务。该方法有两个参数,第一个参数为拍照设置参数的setting,setting中可以设置照片的质量和旋转角度,第二参数为回调函数。
```
ts
let
settings
=
{
quality
:
camera
.
QualityLevel
.
QUALITY_LEVEL_HIGH
,
// 设置图片质量高
rotation
:
camera
.
ImageRotation
.
ROTATION_0
,
// 设置图片旋转角度0
location
:
captureLocation
,
// 设置图片地理位置
mirror
:
false
// 设置镜像使能开关(默认关)
let
settings
:
camera
.
PhotoCaptureSetting
=
{
quality
:
camera
.
QualityLevel
.
QUALITY_LEVEL_HIGH
,
// 设置图片质量高
rotation
:
camera
.
ImageRotation
.
ROTATION_0
,
// 设置图片旋转角度0
location
:
captureLocation
,
// 设置图片地理位置
mirror
:
false
// 设置镜像使能开关(默认关)
};
photoOutput
.
capture
(
settings
,
async
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
photoOutput
.
capture
(
settings
,
async
(
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
});
```
...
...
@@ -136,24 +136,24 @@
-
通过注册固定的captureStart回调函数获取监听拍照开始结果,photoOutput创建成功时即可监听,拍照第一次曝光时触发,该事件返回此次拍照的captureId。
```
ts
photoOutput
.
on
(
'
captureStart
'
,
(
err
,
captureId
)
=>
{
console
.
info
(
`photo capture stated, captureId :
${
captureId
}
`
);
})
photoOutput
.
on
(
'
captureStart
'
,
(
err
:
BusinessError
,
captureId
:
number
)
=>
{
console
.
info
(
`photo capture stated, captureId :
${
captureId
}
`
);
})
;
```
-
通过注册固定的captureEnd回调函数获取监听拍照结束结果,photoOutput创建成功时即可监听,该事件返回结果为拍照完全结束后的相关信息
[
CaptureEndInfo
](
../reference/apis/js-apis-camera.md#captureendinfo
)
。
```
ts
photoOutput
.
on
(
'
captureEnd
'
,
(
err
,
c
aptureEndInfo
)
=>
{
console
.
info
(
`photo capture end, captureId :
${
captureEndInfo
.
captureId
}
`
);
console
.
info
(
`frameCount :
${
captureEndInfo
.
frameCount
}
`
);
})
photoOutput
.
on
(
'
captureEnd
'
,
(
err
:
BusinessError
,
captureEndInfo
:
camera
.
C
aptureEndInfo
)
=>
{
console
.
info
(
`photo capture end, captureId :
${
captureEndInfo
.
captureId
}
`
);
console
.
info
(
`frameCount :
${
captureEndInfo
.
frameCount
}
`
);
})
;
```
-
通过注册固定的error回调函数获取监听拍照输出流的错误结果。callback返回拍照输出接口使用错误时的对应错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
```
ts
photoOutput
.
on
(
'
error
'
,
(
error
)
=>
{
console
.
info
(
`Photo output error code:
${
error
.
code
}
`
);
})
photoOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Photo output error code:
${
error
.
code
}
`
);
})
;
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录