Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
66e5ee93
D
Docs
项目概览
OpenHarmony
/
Docs
接近 2 年 前同步成功
通知
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看板
未验证
提交
66e5ee93
编写于
8月 31, 2023
作者:
O
openharmony_ci
提交者:
Gitee
8月 31, 2023
浏览文件
操作
浏览文件
下载
差异文件
!23593 cameraFramework ArkTs整改
Merge pull request !23593 from 章超123/cherry-pick-1693454209
上级
b02d18d2
624d61a5
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
1189 addition
and
1022 deletion
+1189
-1022
zh-cn/application-dev/media/camera-device-input.md
zh-cn/application-dev/media/camera-device-input.md
+49
-39
zh-cn/application-dev/media/camera-metadata.md
zh-cn/application-dev/media/camera-metadata.md
+43
-26
zh-cn/application-dev/media/camera-mode.md
zh-cn/application-dev/media/camera-mode.md
+237
-235
zh-cn/application-dev/media/camera-performance-improvement.md
...n/application-dev/media/camera-performance-improvement.md
+61
-43
zh-cn/application-dev/media/camera-preview.md
zh-cn/application-dev/media/camera-preview.md
+68
-43
zh-cn/application-dev/media/camera-recording-case.md
zh-cn/application-dev/media/camera-recording-case.md
+233
-203
zh-cn/application-dev/media/camera-recording.md
zh-cn/application-dev/media/camera-recording.md
+100
-85
zh-cn/application-dev/media/camera-session-management.md
zh-cn/application-dev/media/camera-session-management.md
+87
-56
zh-cn/application-dev/media/camera-shooting-case.md
zh-cn/application-dev/media/camera-shooting-case.md
+209
-207
zh-cn/application-dev/media/camera-shooting.md
zh-cn/application-dev/media/camera-shooting.md
+102
-85
未找到文件。
zh-cn/application-dev/media/camera-device-input.md
浏览文件 @
66e5ee93
...
@@ -10,14 +10,18 @@
...
@@ -10,14 +10,18 @@
```
ts
```
ts
import
camera
from
'
@ohos.multimedia.camera
'
;
import
camera
from
'
@ohos.multimedia.camera
'
;
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
import
type
featureAbility
from
'
@ohos.ability.featureAbility
'
;
```
```
2.
通过getCameraManager()方法,获取cameraManager对象。
2.
通过getCameraManager()方法,获取cameraManager对象。
[
各类Context的获取方式
](
../application-models/application-context-stage.md
)
。
[
各类Context的获取方式
](
../application-models/application-context-stage.md
)
。
```
ts
```
ts
let
cameraManager
:
camera
.
CameraManager
;
function
getCameraManager
(
context
:
featureAbility
.
Context
):
camera
.
CameraManager
{
let
context
:
Context
=
getContext
(
this
);
let
cameraManager
:
camera
.
CameraManager
;
cameraManager
=
camera
.
getCameraManager
(
context
);
cameraManager
=
camera
.
getCameraManager
(
context
);
return
cameraManager
;
}
```
```
> **说明:**
> **说明:**
...
@@ -27,45 +31,49 @@
...
@@ -27,45 +31,49 @@
3.
通过cameraManager类中的getSupportedCameras()方法,获取当前设备支持的相机列表,列表中存储了设备支持的所有相机ID。若列表不为空,则说明列表中的每个ID都支持独立创建相机对象;否则,说明当前设备无可用相机,不可继续后续操作。
3.
通过cameraManager类中的getSupportedCameras()方法,获取当前设备支持的相机列表,列表中存储了设备支持的所有相机ID。若列表不为空,则说明列表中的每个ID都支持独立创建相机对象;否则,说明当前设备无可用相机,不可继续后续操作。
```
ts
```
ts
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
function
getCameraDevices
(
cameraManager
:
camera
.
CameraManager
):
Array
<
camera
.
CameraDevice
>
{
if
(
cameraArray
!=
undefined
&&
cameraArray
.
length
<=
0
)
{
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
console
.
error
(
"
cameraManager.getSupportedCameras error
"
);
if
(
cameraArray
!=
undefined
&&
cameraArray
.
length
<=
0
)
{
return
;
console
.
error
(
"
cameraManager.getSupportedCameras error
"
);
}
return
null
;
}
for
(
let
index
=
0
;
index
<
cameraArray
.
length
;
index
++
)
{
for
(
let
index
=
0
;
index
<
cameraArray
.
length
;
index
++
)
{
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
}
return
cameraArray
;
}
}
```
```
4.
通过getSupportedOutputCapability()方法,获取当前设备支持的所有输出流,如预览流、拍照流等。输出流在CameraOutputCapability中的各个profile字段中。
4.
通过getSupportedOutputCapability()方法,获取当前设备支持的所有输出流,如预览流、拍照流等。输出流在CameraOutputCapability中的各个profile字段中。
```
ts
```
ts
// 创建相机输入流
async
function
getSupportedOutputCapability
(
cameraDevice
:
camera
.
CameraDevice
,
cameraManager
:
camera
.
CameraManager
):
Promise
<
camera
.
CameraOutputCapability
>
{
let
cameraInput
:
camera
.
CameraInput
;
// 创建相机输入流
try
{
let
cameraInput
:
camera
.
CameraInput
;
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
try
{
}
catch
(
error
)
{
cameraInput
=
cameraManager
.
createCameraInput
(
cameraDevice
);
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
err
.
code
);
let
err
=
error
as
BusinessError
;
}
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
err
.
code
);
// 监听cameraInput错误信息
}
let
cameraDevice
:
camera
.
CameraDevice
=
cameraArray
[
0
];
// 监听cameraInput错误信息
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
});
});
// 打开相机
// 打开相机
await
cameraInput
.
open
();
await
cameraInput
.
open
();
// 获取相机设备支持的输出流能力
// 获取相机设备支持的输出流能力
let
cameraOutputCapability
:
camera
.
CameraOutputCapability
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
let
cameraOutputCapability
:
camera
.
CameraOutputCapability
=
cameraManager
.
getSupportedOutputCapability
(
cameraDevice
);
if
(
!
cameraOutputCapability
)
{
if
(
!
cameraOutputCapability
)
{
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
);
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
);
return
;
return
null
;
}
console
.
info
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCapability
));
return
cameraOutputCapability
;
}
}
console
.
info
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCapability
));
```
```
...
@@ -76,8 +84,10 @@
...
@@ -76,8 +84,10 @@
通过注册cameraStatus事件,通过回调返回监听结果,callback返回CameraStatusInfo参数,参数的具体内容可参考相机管理器回调接口实例
[
CameraStatusInfo
](
../reference/apis/js-apis-camera.md#camerastatusinfo
)
。
通过注册cameraStatus事件,通过回调返回监听结果,callback返回CameraStatusInfo参数,参数的具体内容可参考相机管理器回调接口实例
[
CameraStatusInfo
](
../reference/apis/js-apis-camera.md#camerastatusinfo
)
。
```
ts
```
ts
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
CameraStatusInfo
)
=>
{
function
onCameraStatus
(
cameraManager
:
camera
.
CameraManager
):
void
{
console
.
info
(
`camera:
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
CameraStatusInfo
)
=>
{
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
console
.
info
(
`camera:
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
});
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
});
}
```
```
zh-cn/application-dev/media/camera-metadata.md
浏览文件 @
66e5ee93
...
@@ -8,37 +8,50 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
...
@@ -8,37 +8,50 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
详细的API说明请参考
[
Camera API参考
](
../reference/apis/js-apis-camera.md
)
。
详细的API说明请参考
[
Camera API参考
](
../reference/apis/js-apis-camera.md
)
。
1.
调用CameraOutputCapability类中的supportedMetadataObjectTypes()方法,获取当前设备支持的元数据类型,并通过createMetadataOutput()方法创建元数据输出流。
1.
导入相关接口,导入方法如下。
```
ts
import
camera
from
'
@ohos.multimedia.camera
'
;
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
```
2.
调用CameraOutputCapability类中的supportedMetadataObjectTypes()方法,获取当前设备支持的元数据类型,并通过createMetadataOutput()方法创建元数据输出流。
```
ts
```
ts
let
metadataObjectTypes
:
Array
<
camera
.
MetadataObjectType
>
=
cameraOutputCapability
.
supportedMetadataObjectTypes
;
function
getMetadataOutput
(
cameraManager
:
camera
.
CameraManager
,
cameraOutputCapability
:
camera
.
CameraOutputCapability
):
camera
.
MetadataOutput
{
let
metadataOutput
:
camera
.
MetadataOutput
;
let
metadataObjectTypes
:
Array
<
camera
.
MetadataObjectType
>
=
cameraOutputCapability
.
supportedMetadataObjectTypes
;
try
{
let
metadataOutput
:
camera
.
MetadataOutput
;
metadataOutput
=
cameraManager
.
createMetadataOutput
(
metadataObjectTypes
);
try
{
}
catch
(
error
)
{
metadataOutput
=
cameraManager
.
createMetadataOutput
(
metadataObjectTypes
);
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
info
(
'
Failed to createMetadataOutput, error code:
'
+
err
.
code
);
let
err
=
error
as
BusinessError
;
console
.
info
(
'
Failed to createMetadataOutput, error code:
'
+
err
.
code
);
}
return
metadataOutput
;
}
}
```
```
2
.
调用start()方法输出metadata数据,接口调用失败时,会返回相应错误码,错误码类型参见CameraErrorCode。
3
.
调用start()方法输出metadata数据,接口调用失败时,会返回相应错误码,错误码类型参见CameraErrorCode。
```
ts
```
ts
metadataOutput
.
start
().
then
(()
=>
{
function
startMetadataOutput
(
metadataOutput
:
camera
.
MetadataOutput
):
void
{
console
.
info
(
'
Callback returned with metadataOutput started.
'
);
metadataOutput
.
start
().
then
(()
=>
{
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
info
(
'
Callback returned with metadataOutput started.
'
);
console
.
info
(
'
Failed to metadataOutput start, error code:
'
+
err
.
code
);
}).
catch
((
err
:
BusinessError
)
=>
{
});
console
.
info
(
'
Failed to metadataOutput start, error code:
'
+
err
.
code
);
});
}
```
```
3
.
调用stop方法停止输出metadata数据,接口调用失败会返回相应错误码,错误码类型参见CameraErrorCode。
4
.
调用stop方法停止输出metadata数据,接口调用失败会返回相应错误码,错误码类型参见CameraErrorCode。
```
ts
```
ts
metadataOutput
.
stop
().
then
(()
=>
{
function
stopMetadataOutput
(
metadataOutput
:
camera
.
MetadataOutput
):
void
{
console
.
info
(
'
Callback returned with metadataOutput stopped.
'
);
metadataOutput
.
stop
().
then
(()
=>
{
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
info
(
'
Callback returned with metadataOutput stopped.
'
);
console
.
info
(
'
Failed to metadataOutput stop
'
+
err
.
code
);
}).
catch
((
err
:
BusinessError
)
=>
{
});
console
.
info
(
'
Failed to metadataOutput stop
'
+
err
.
code
);
});
}
```
```
## 状态监听
## 状态监听
...
@@ -48,9 +61,11 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
...
@@ -48,9 +61,11 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
-
通过注册监听获取metadata对象,监听事件固定为metadataObjectsAvailable。检测到有效metadata数据时,callback返回相应的metadata数据信息,metadataOutput创建成功时可监听。
-
通过注册监听获取metadata对象,监听事件固定为metadataObjectsAvailable。检测到有效metadata数据时,callback返回相应的metadata数据信息,metadataOutput创建成功时可监听。
```
ts
```
ts
metadataOutput
.
on
(
'
metadataObjectsAvailable
'
,
(
err
:
BusinessError
,
metadataObjectArr
:
Array
<
camera
.
MetadataObject
>
)
=>
{
function
onMetadataObjectsAvailable
(
metadataOutput
:
camera
.
MetadataOutput
):
void
{
console
.
info
(
`metadata output metadataObjectsAvailable`
);
metadataOutput
.
on
(
'
metadataObjectsAvailable
'
,
(
err
:
BusinessError
,
metadataObjectArr
:
Array
<
camera
.
MetadataObject
>
)
=>
{
});
console
.
info
(
`metadata output metadataObjectsAvailable`
);
});
}
```
```
> **说明:**
> **说明:**
...
@@ -60,7 +75,9 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
...
@@ -60,7 +75,9 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
-
通过注册回调函数,获取监听metadata流的错误结果,callback返回metadata输出接口使用错误时返回的错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
-
通过注册回调函数,获取监听metadata流的错误结果,callback返回metadata输出接口使用错误时返回的错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
```
ts
```
ts
metadataOutput
.
on
(
'
error
'
,
(
metadataOutputError
:
BusinessError
)
=>
{
function
onMetadataError
(
metadataOutput
:
camera
.
MetadataOutput
):
void
{
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
浏览文件 @
66e5ee93
...
@@ -12,265 +12,267 @@
...
@@ -12,265 +12,267 @@
[
各类Context的获取方式
](
../application-models/application-context-stage.md
)
[
各类Context的获取方式
](
../application-models/application-context-stage.md
)
```
ts
```
ts
import
camera
from
'
@ohos.multimedia.camera
'
;
import
camera
from
'
@ohos.multimedia.camera
'
;
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
import
image
from
'
@ohos.multimedia.image
'
;
import
image
from
'
@ohos.multimedia.image
'
;
import
media
from
'
@ohos.multimedia.media
'
;
import
type
featureAbility
from
'
@ohos.ability.featureAbility
'
;
async
function
cameraModeCase
(
context
:
featureAbility
.
Context
,
surfaceId
:
string
):
Promise
<
void
>
{
// 创建CameraManager对象
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
if
(
!
cameraManager
)
{
console
.
error
(
"
camera.getCameraManager error
"
);
return
;
}
// 创建ModeManager对象
let
modeManager
:
camera
.
ModeManager
=
camera
.
getModeManager
(
context
);
if
(
!
cameraManager
)
{
console
.
error
(
"
camera.getModeManager error
"
);
return
;
}
// 监听相机状态变化
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
CameraStatusInfo
)
=>
{
console
.
info
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
});
// 获取相机列表
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
if
(
cameraArray
.
length
<=
0
)
{
console
.
error
(
"
cameraManager.getSupportedCameras error
"
);
return
;
}
// 创建CameraManager对象
for
(
let
index
=
0
;
index
<
cameraArray
.
length
;
index
++
)
{
let
context
:
Context
=
getContext
(
this
);
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
if
(
!
cameraManager
)
{
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
console
.
error
(
"
camera.getCameraManager error
"
);
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
return
;
}
}
// 创建ModeManager对象
let
modeManager
:
camera
.
ModeManager
=
camera
.
getModeManager
(
context
);
if
(
!
cameraManager
)
{
console
.
error
(
"
camera.getModeManager error
"
);
return
;
}
// 监听相机状态变化
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
CameraStatusInfo
)
=>
{
console
.
info
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
});
// 获取相机列表
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
if
(
cameraArray
.
length
<=
0
)
{
console
.
error
(
"
cameraManager.getSupportedCameras error
"
);
return
;
}
for
(
let
index
=
0
;
index
<
cameraArray
.
length
;
index
++
)
{
// 获取模式列表
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
let
cameraModeArray
:
Array
<
camera
.
CameraMode
>
=
modeManager
.
getSupportedModes
(
cameraArray
[
0
]);
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
if
(
cameraModeArray
.
length
<=
0
)
{
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
console
.
error
(
"
modeManager.getSupportedModes error
"
);
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
return
;
}
}
// 创建相机输入流
let
cameraInput
:
camera
.
CameraInput
;
try
{
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
err
.
code
);
}
// 监听cameraInput错误信息
let
cameraDevice
:
camera
.
CameraDevice
=
cameraArray
[
0
];
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
})
// 获取模式列表
// 打开相机
let
cameraModeArray
:
Array
<
camera
.
CameraMode
>
=
modeManager
.
getSupportedModes
(
cameraArray
[
0
]);
await
cameraInput
.
open
();
if
(
cameraModeArray
.
length
<=
0
)
{
console
.
error
(
"
modeManager.getSupportedModes error
"
);
return
;
}
// 创建相机输入流
let
cameraInput
:
camera
.
CameraInput
;
try
{
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
err
.
code
);
}
// 监听cameraInput错误信息
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
:
camera
.
CameraOutputCapability
=
modeManager
.
getSupportedOutputCapability
(
cameraArray
[
0
],
cameraModeArray
[
0
]);
if
(
!
cameraOutputCap
)
{
console
.
error
(
"
modeManager.getSupportedOutputCapability error
"
);
return
;
}
console
.
info
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCap
));
// 获取当前模式相机设备支持的输出流能力
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
previewProfiles
;
let
cameraOutputCap
:
camera
.
CameraOutputCapability
=
modeManager
.
getSupportedOutputCapability
(
cameraArray
[
0
],
cameraModeArray
[
0
]);
if
(
!
previewProfilesArray
)
{
if
(
!
cameraOutputCap
)
{
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
);
console
.
error
(
"
modeManager.getSupportedOutputCapability error
"
);
}
return
;
}
console
.
info
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCap
));
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
preview
Profiles
;
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
photo
Profiles
;
if
(
!
preview
ProfilesArray
)
{
if
(
!
photo
ProfilesArray
)
{
console
.
error
(
"
createOutput preview
ProfilesArray == null || undefined
"
);
console
.
error
(
"
createOutput photo
ProfilesArray == null || undefined
"
);
}
}
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
photoProfiles
;
// 创建预览输出流,其中参数 surfaceId 参考上文 XComponent 组件,预览流为XComponent组件提供的surface
if
(
!
photoProfilesArray
)
{
let
previewOutput
:
camera
.
PreviewOutput
;
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
try
{
}
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
"
Failed to create the PreviewOutput instance. error code:
"
+
err
.
code
);
}
// 监听预览输出错误信息
previewOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Preview output error code:
${
error
.
code
}
`
);
})
// 创建ImageReceiver对象,并设置照片参数:分辨率大小是根据前面 photoProfilesArray 获取的当前设备所支持的拍照分辨率大小去设置
let
imageReceiver
:
image
.
ImageReceiver
=
image
.
createImageReceiver
(
1920
,
1080
,
4
,
8
);
// 获取照片显示SurfaceId
let
photoSurfaceId
:
string
=
await
imageReceiver
.
getReceivingSurfaceId
();
// 创建拍照输出流
let
photoOutput
:
camera
.
PhotoOutput
;
try
{
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
err
.
code
);
}
//创建portrait会话
let
portraitSession
:
camera
.
CaptureSession
;
try
{
portraitSession
=
modeManager
.
createCaptureSession
(
cameraModeArray
[
0
]);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
err
.
code
);
}
// 创建预览输出流,其中参数 surfaceId 参考上文 XComponent 组件,预览流为XComponent组件提供的surface
// 监听portraitSession错误信息
let
previewOutput
:
camera
.
PreviewOutput
;
portraitSession
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
try
{
console
.
info
(
`Capture session error code:
${
error
.
code
}
`
);
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
});
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
"
Failed to create the PreviewOutput instance. error code:
"
+
err
.
code
);
}
// 监听预览输出错误信息
previewOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Preview output error code:
${
error
.
code
}
`
);
})
// 创建ImageReceiver对象,并设置照片参数:分辨率大小是根据前面 photoProfilesArray 获取的当前设备所支持的拍照分辨率大小去设置
let
imageReceiver
:
image
.
ImageReceiver
=
image
.
createImageReceiver
(
1920
,
1080
,
4
,
8
);
// 获取照片显示SurfaceId
let
photoSurfaceId
:
string
=
await
imageReceiver
.
getReceivingSurfaceId
();
// 创建拍照输出流
let
photoOutput
:
camera
.
PhotoOutput
;
try
{
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
err
.
code
);
}
//创建portrait会话
let
portraitSession
:
camera
.
CaptureSession
;
try
{
portraitSession
=
modeManager
.
createCaptureSession
(
cameraModeArray
[
0
]);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
err
.
code
);
}
// 监听portraitSession错误信息
// 开始配置会话
portraitSession
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
try
{
console
.
info
(
`Capture session error code:
${
error
.
code
}
`
);
portraitSession
.
beginConfig
();
});
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
err
.
code
);
}
// 开始配置会话
// 向会话中添加相机输入流
try
{
try
{
portraitSession
.
beginConfig
(
);
portraitSession
.
addInput
(
cameraInput
);
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to beginConfig
. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to addInput
. errorCode =
'
+
err
.
code
);
}
}
// 向会话中添加相机输入
流
// 向会话中添加预览输出
流
try
{
try
{
portraitSession
.
addInput
(
cameraIn
put
);
portraitSession
.
addOutput
(
previewOut
put
);
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to addInput
. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to addOutput(previewOutput)
. errorCode =
'
+
err
.
code
);
}
}
// 向会话中添加预览
输出流
// 向会话中添加拍照
输出流
try
{
try
{
portraitSession
.
addOutput
(
preview
Output
);
portraitSession
.
addOutput
(
photo
Output
);
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to addOutput(preview
Output). errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to addOutput(photo
Output). errorCode =
'
+
err
.
code
);
}
}
// 向会话中添加拍照输出流
// 提交会话配置
try
{
await
portraitSession
.
commitConfig
();
portraitSession
.
addOutput
(
photoOutput
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to addOutput(photoOutput). errorCode =
'
+
err
.
code
);
}
// 提交会话配置
// 启动会话
await
portraitSession
.
commitConfig
();
await
portraitSession
.
start
().
then
(()
=>
{
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
})
// 启动会话
// 获取支持的美颜类型
await
portraitSession
.
start
().
then
(()
=>
{
let
beautyTypes
:
Array
<
camera
.
BeautyType
>
;
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
try
{
})
beautyTypes
=
portraitSession
.
getSupportedBeautyTypes
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to get the beauty types. errorCode =
'
+
err
.
code
);
}
// 获取支持的美颜类型对应的美颜强度范围
let
beautyRanges
:
Array
<
number
>
;
try
{
beautyRanges
=
portraitSession
.
getSupportedBeautyRanges
(
beautyTypes
[
0
]);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to get the beauty types ranges. errorCode =
'
+
err
.
code
);
}
// 设置美颜类型及对应的美颜强度
try
{
portraitSession
.
setBeauty
(
beautyTypes
[
0
],
beautyRanges
[
0
]);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to set the beauty type value. errorCode =
'
+
err
.
code
);
}
// 获取已经设置的美颜类型对应的美颜强度
let
beautyLevel
:
number
;
try
{
beautyLevel
=
portraitSession
.
getBeauty
(
beautyTypes
[
0
]);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to get the beauty type value. errorCode =
'
+
err
.
code
);
}
// 获取支持的美颜类型
// 获取支持的滤镜类型
let
beautyTypes
:
Array
<
camera
.
BeautyType
>
;
let
filterTypes
:
Array
<
camera
.
FilterType
>
;
try
{
try
{
beautyTypes
=
portraitSession
.
getSupportedBeautyTypes
();
filterTypes
=
portraitSession
.
getSupportedFilters
();
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to get the beauty types. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to get the filter types. errorCode =
'
+
err
.
code
);
}
}
// 获取支持的美颜类型对应的美颜强度范围
// 设置滤镜类型
let
beautyRanges
:
Array
<
number
>
;
try
{
try
{
portraitSession
.
setFilter
(
filterTypes
[
0
]);
beautyRanges
=
portraitSession
.
getSupportedBeautyRanges
(
beautyTypes
[
0
]);
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to set the filter type value. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to get the beauty types ranges. errorCode =
'
+
err
.
code
);
}
}
// 获取已经设置的滤镜类型
// 设置美颜类型及对应的美颜强度
let
filter
:
number
;
try
{
try
{
portraitSession
.
setBeauty
(
beautyTypes
[
0
],
beautyRanges
[
0
]);
filter
=
portraitSession
.
getFilter
();
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to set the beauty type value. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to get the filter type value. errorCode =
'
+
err
.
code
);
}
}
// 获取已经设置的美颜类型对应的美颜强度
let
beautyLevel
:
number
;
try
{
beautyLevel
=
portraitSession
.
getBeauty
(
beautyTypes
[
0
]);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to get the beauty type value. errorCode =
'
+
err
.
code
);
}
// 获取支持的滤镜
类型
// 获取支持的虚化
类型
let
filterTypes
:
Array
<
camera
.
FilterType
>
;
let
portraitTypes
:
Array
<
camera
.
PortraitEffect
>
;
try
{
try
{
filterTypes
=
portraitSession
.
getSupportedFilter
s
();
portraitTypes
=
portraitSession
.
getSupportedPortraitEffect
s
();
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to get the filter
types. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to get the portrait effects
types. errorCode =
'
+
err
.
code
);
}
}
// 设置滤镜
类型
// 设置虚化
类型
try
{
try
{
portraitSession
.
setFilter
(
filter
Types
[
0
]);
portraitSession
.
setPortraitEffect
(
portrait
Types
[
0
]);
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to set the filter type
value. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to set the portrait effects
value. errorCode =
'
+
err
.
code
);
}
}
// 获取已经设置的滤镜
类型
// 获取已经设置的虚化
类型
let
filter
:
number
;
let
effect
:
camera
.
PortraitEffect
;
try
{
try
{
filter
=
portraitSession
.
getFilter
();
effect
=
portraitSession
.
getPortraitEffect
();
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to get the filter type
value. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to get the portrait effects
value. errorCode =
'
+
err
.
code
);
}
}
// 获取支持的虚化类型
let
captureSettings
:
camera
.
PhotoCaptureSetting
;
let
portraitTypes
:
Array
<
camera
.
PortraitEffect
>
;
// 使用当前拍照设置进行拍照
try
{
photoOutput
.
capture
(
captureSettings
,
async
(
err
:
BusinessError
)
=>
{
portraitTypes
=
portraitSession
.
getSupportedPortraitEffects
();
if
(
err
)
{
}
catch
(
error
)
{
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
let
err
=
error
as
BusinessError
;
return
;
console
.
error
(
'
Failed to get the portrait effects types. errorCode =
'
+
err
.
code
);
}
}
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
// 设置虚化类型
});
try
{
// 停止当前会话
portraitSession
.
setPortraitEffect
(
portraitTypes
[
0
]);
portraitSession
.
stop
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to set the portrait effects value. errorCode =
'
+
err
.
code
);
}
// 获取已经设置的虚化类型
let
effect
:
camera
.
PortraitEffect
;
try
{
effect
=
portraitSession
.
getPortraitEffect
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to get the portrait effects value. errorCode =
'
+
err
.
code
);
}
let
captureSettings
:
camera
.
PhotoCaptureSetting
;
// 释放相机输入流
// 使用当前拍照设置进行拍照
cameraInput
.
close
();
photoOutput
.
capture
(
captureSettings
,
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
();
// 释放相机输入
流
// 释放预览输出
流
cameraInput
.
clo
se
();
previewOutput
.
relea
se
();
// 释放预览
输出流
// 释放拍照
输出流
preview
Output
.
release
();
photo
Output
.
release
();
// 释放拍照输出流
// 释放会话
photoOutput
.
release
();
portraitSession
.
release
();
// 释放会话
// 会话置空
portraitSession
.
release
();
portraitSession
=
null
;
}
// 会话置空
portraitSession
=
null
;
```
```
zh-cn/application-dev/media/camera-performance-improvement.md
浏览文件 @
66e5ee93
...
@@ -35,8 +35,9 @@
...
@@ -35,8 +35,9 @@
```
js
```
js
import
camera
from
'
@ohos.multimedia.camera
'
;
import
camera
from
'
@ohos.multimedia.camera
'
;
import
type
featureAbility
from
'
@ohos.ability.featureAbility
'
;
async
function
Preview
(
context
:
Context
,
cameraInfo
:
camera
.
CameraDevice
,
previewProfile
:
camera
.
Profile
,
photoProfile
:
camera
.
Profile
,
surfaceId
:
string
):
Promise
<
void
>
{
async
function
preview
(
context
:
featureAbility
.
Context
,
cameraInfo
:
camera
.
CameraDevice
,
previewProfile
:
camera
.
Profile
,
photoProfile
:
camera
.
Profile
,
surfaceId
:
string
):
Promise
<
void
>
{
const
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
const
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
const
cameraInput
:
camera
.
CameraInput
=
cameraManager
.
createCameraInput
(
cameraInfo
);
const
cameraInput
:
camera
.
CameraInput
=
cameraManager
.
createCameraInput
(
cameraInfo
);
const
previewOutput
:
camera
.
PreviewOutput
=
await
cameraManager
.
createDeferredPreviewOutput
(
previewProfile
);
const
previewOutput
:
camera
.
PreviewOutput
=
await
cameraManager
.
createDeferredPreviewOutput
(
previewProfile
);
...
@@ -84,32 +85,41 @@ async function Preview(context: Context, cameraInfo: camera.CameraDevice, previe
...
@@ -84,32 +85,41 @@ async function Preview(context: Context, cameraInfo: camera.CameraDevice, previe
[
各类Context的获取方式
](
../application-models/application-context-stage.md
)
[
各类Context的获取方式
](
../application-models/application-context-stage.md
)
```
js
```
js
import
camera
from
'
@ohos.multimedia.camera
'
;
import
camera
from
'
@ohos.multimedia.camera
'
;
let
context
:
Context
=
getContext
(
this
);
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
import
image
from
'
@ohos.multimedia.image
'
;
let
cameras
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
import
type
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 创建CaptureSession实例
let
captureSession
:
camera
.
CaptureSession
=
cameraManager
.
createCaptureSession
();
async
function
enableQuickThumbnail
(
context
:
featureAbility
.
Context
,
surfaceId
:
string
,
photoProfile
:
camera
.
Profile
):
Promise
<
void
>
{
// 开始配置会话
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
captureSession
.
beginConfig
();
let
cameras
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
// 把CameraInput加入到会话
// 创建CaptureSession实例
let
cameraInput
:
camera
.
CameraInput
=
cameraManager
.
createCameraInput
(
cameras
[
0
]);
let
captureSession
:
camera
.
CaptureSession
=
cameraManager
.
createCaptureSession
();
cameraInput
.
open
();
// 开始配置会话
captureSession
.
addInput
(
cameraInput
);
captureSession
.
beginConfig
();
// 把PhotoOutPut加入到会话
// 把CameraInput加入到会话
let
photoOutPut
:
camera
.
PhotoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfile
,
surfaceId
);
let
cameraInput
:
camera
.
CameraInput
=
cameraManager
.
createCameraInput
(
cameras
[
0
]);
captureSession
.
addOutput
(
photoOutPut
);
cameraInput
.
open
();
let
isSupported
:
boolean
=
photoOutPut
.
isQuickThumbnailSupported
();
captureSession
.
addInput
(
cameraInput
);
if
(
isSupported
)
{
// 把PhotoOutPut加入到会话
// 使能快速缩略图
let
photoOutPut
:
camera
.
PhotoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfile
,
surfaceId
);
photoOutPut
.
enableQuickThumbnail
(
true
);
captureSession
.
addOutput
(
photoOutPut
);
photoOutPut
.
on
(
'
quickThumbnail
'
,
(
err
:
BusinessError
,
pixelmap
:
image
.
PixelMap
)
=>
{
let
isSupported
:
boolean
=
photoOutPut
.
isQuickThumbnailSupported
();
if
(
err
||
pixelmap
===
undefined
)
{
if
(
isSupported
)
{
console
.
error
(
'
photoOutPut on thumbnail failed
'
);
// 使能快速缩略图
return
;
photoOutPut
.
enableQuickThumbnail
(
true
);
}
photoOutPut
.
on
(
'
quickThumbnail
'
,
(
err
:
BusinessError
,
pixelMap
:
image
.
PixelMap
)
=>
{
// 显示或保存pixelmap
if
(
err
||
pixelMap
===
undefined
)
{
showOrSavePicture
(
pixelmap
);
console
.
error
(
'
photoOutPut on thumbnail failed
'
);
})
return
;
}
// 显示或保存pixelmap
showOrSavePicture
(
pixelMap
);
})
}
}
function
showOrSavePicture
(
pixelMap
:
image
.
PixelMap
):
void
{
//do something
}
}
```
```
...
@@ -144,13 +154,17 @@ if (isSupported) {
...
@@ -144,13 +154,17 @@ if (isSupported) {
```
js
```
js
import
camera
from
'
@ohos.multimedia.camera
'
;
import
camera
from
'
@ohos.multimedia.camera
'
;
let
context
:
Context
=
getContext
(
this
);
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
import
type
featureAbility
from
'
@ohos.ability.featureAbility
'
;
try
{
cameraManager
.
prelaunch
();
function
preLaunch
(
context
:
featureAbility
.
Context
):
void
{
}
catch
(
error
)
{
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
let
err
=
error
as
BusinessError
;
try
{
console
.
error
(
`catch error: Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
cameraManager
.
prelaunch
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`catch error: Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
}
}
}
```
```
...
@@ -162,15 +176,19 @@ if (isSupported) {
...
@@ -162,15 +176,19 @@ if (isSupported) {
```
js
```
js
import
camera
from
'
@ohos.multimedia.camera
'
;
import
camera
from
'
@ohos.multimedia.camera
'
;
let
context
:
Context
=
getContext
(
this
);
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
import
type
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
cameras
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
if
(
cameraManager
.
isPrelaunchSupported
(
cameras
[
0
]))
{
function
setPreLaunchConfig
(
context
:
featureAbility
.
Context
):
void
{
try
{
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
cameraManager
.
setPrelaunchConfig
({
cameraDevice
:
cameras
[
0
]});
let
cameras
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
}
catch
(
error
)
{
if
(
cameraManager
.
isPrelaunchSupported
(
cameras
[
0
]))
{
let
err
=
error
as
BusinessError
;
try
{
console
.
error
(
`catch error: Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
cameraManager
.
setPrelaunchConfig
({
cameraDevice
:
cameras
[
0
]});
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`catch error: Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
}
}
}
}
}
```
```
zh-cn/application-dev/media/camera-preview.md
浏览文件 @
66e5ee93
...
@@ -6,55 +6,74 @@
...
@@ -6,55 +6,74 @@
详细的API说明请参考
[
Camera API参考
](
../reference/apis/js-apis-camera.md
)
。
详细的API说明请参考
[
Camera API参考
](
../reference/apis/js-apis-camera.md
)
。
1.
创建Surface。
1.
导入camera接口,接口中提供了相机相关的属性和方法,导入方法如下。
```
ts
import
camera
from
'
@ohos.multimedia.camera
'
;
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
```
2.
创建Surface。
XComponent组件为预览流提供的Surface,而XComponent的能力由UI提供,相关介绍可参考
[
XComponent组件参考
](
../reference/arkui-ts/ts-basic-components-xcomponent.md
)
。
XComponent组件为预览流提供的Surface,而XComponent的能力由UI提供,相关介绍可参考
[
XComponent组件参考
](
../reference/arkui-ts/ts-basic-components-xcomponent.md
)
。
```
ts
```
ets
// xxx.ets
// 创建XComponentController
// 创建XComponentController
mXComponentController
:
XComponentController
=
new
XComponentController
;
@Component
build
()
{
struct XComponentPage {
Flex
()
{
// 创建XComponentController
// 创建XComponent
mXComponentController: XComponentController = new XComponentController;
XComponent
({
surfaceId: string;
id
:
''
,
type
:
'
surface
'
,
build() {
libraryname
:
''
,
Flex() {
controller
:
this
.
mXComponentController
// 创建XComponent
})
XComponent({
.
onLoad
(()
=>
{
id: '',
// 设置Surface宽高(1920*1080),预览尺寸设置参考前面 previewProfilesArray 获取的当前设备所支持的预览分辨率大小去设置
type: 'surface',
this
.
mXComponentController
.
setXComponentSurfaceSize
({
surfaceWidth
:
1920
,
surfaceHeight
:
1080
});
libraryname: '',
// 获取Surface ID
controller: this.mXComponentController
globalThis
.
surfaceId
=
this
.
mXComponentController
.
getXComponentSurfaceId
();
})
})
.onLoad(() => {
.
width
(
'
1920px
'
)
// 设置Surface宽高(1920*1080),预览尺寸设置参考前面 previewProfilesArray 获取的当前设备所支持的预览分辨率大小去设置
.
height
(
'
1080px
'
)
this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:1920,surfaceHeight:1080});
// 获取Surface ID
this.surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
})
.width('1920px')
.height('1080px')
}
}
}
}
}
```
```
2
.
通过CameraOutputCapability类中的previewProfiles()方法获取当前设备支持的预览能力,返回previewProfilesArray数组 。通过createPreviewOutput()方法创建预览输出流,其中,createPreviewOutput()方法中的两个参数分别是previewProfilesArray数组中的第一项和步骤一中获取的surfaceId。
3
.
通过CameraOutputCapability类中的previewProfiles()方法获取当前设备支持的预览能力,返回previewProfilesArray数组 。通过createPreviewOutput()方法创建预览输出流,其中,createPreviewOutput()方法中的两个参数分别是previewProfilesArray数组中的第一项和步骤一中获取的surfaceId。
```
ts
```
ts
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCapability
.
previewProfiles
;
function
getPreviewOutput
(
cameraManager
:
camera
.
CameraManager
,
cameraOutputCapability
:
camera
.
CameraOutputCapability
,
surfaceId
:
string
):
camera
.
PreviewOutput
{
let
previewOutput
:
camera
.
PreviewOutput
;
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCapability
.
previewProfiles
;
try
{
let
previewOutput
:
camera
.
PreviewOutput
;
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
try
{
}
catch
(
error
)
{
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
error
(
"
Failed to create the PreviewOutput instance. error code:
"
+
err
.
code
);
let
err
=
error
as
BusinessError
;
console
.
error
(
"
Failed to create the PreviewOutput instance. error code:
"
+
err
.
code
);
}
return
previewOutput
;
}
}
```
```
3
.
使能。通过start()方法输出预览流,接口调用失败会返回相应错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
4
.
使能。通过start()方法输出预览流,接口调用失败会返回相应错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
```
ts
```
ts
previewOutput
.
start
().
then
(()
=>
{
function
startPreviewOutput
(
previewOutput
:
camera
.
PreviewOutput
):
void
{
console
.
info
(
'
Callback returned with previewOutput started.
'
);
previewOutput
.
start
().
then
(()
=>
{
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
info
(
'
Callback returned with previewOutput started.
'
);
console
.
info
(
'
Failed to previewOutput start
'
+
err
.
code
);
}).
catch
((
err
:
BusinessError
)
=>
{
});
console
.
info
(
'
Failed to previewOutput start
'
+
err
.
code
);
});
}
```
```
...
@@ -65,23 +84,29 @@
...
@@ -65,23 +84,29 @@
-
通过注册固定的frameStart回调函数获取监听预览启动结果,previewOutput创建成功时即可监听,预览第一次曝光时触发,有该事件返回结果则认为预览流已启动。
-
通过注册固定的frameStart回调函数获取监听预览启动结果,previewOutput创建成功时即可监听,预览第一次曝光时触发,有该事件返回结果则认为预览流已启动。
```
ts
```
ts
previewOutput
.
on
(
'
frameStart
'
,
()
=>
{
function
onPreviewOutputFrameStart
(
previewOutput
:
camera
.
PreviewOutput
):
void
{
console
.
info
(
'
Preview frame started
'
);
previewOutput
.
on
(
'
frameStart
'
,
()
=>
{
});
console
.
info
(
'
Preview frame started
'
);
});
}
```
```
-
通过注册固定的frameEnd回调函数获取监听预览结束结果,previewOutput创建成功时即可监听,预览完成最后一帧时触发,有该事件返回结果则认为预览流已结束。
-
通过注册固定的frameEnd回调函数获取监听预览结束结果,previewOutput创建成功时即可监听,预览完成最后一帧时触发,有该事件返回结果则认为预览流已结束。
```
ts
```
ts
previewOutput
.
on
(
'
frameEnd
'
,
()
=>
{
function
onPreviewOutputFrameEnd
(
previewOutput
:
camera
.
PreviewOutput
):
void
{
console
.
info
(
'
Preview frame ended
'
);
previewOutput
.
on
(
'
frameEnd
'
,
()
=>
{
});
console
.
info
(
'
Preview frame ended
'
);
});
}
```
```
-
通过注册固定的error回调函数获取监听预览输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
-
通过注册固定的error回调函数获取监听预览输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
```
ts
```
ts
previewOutput
.
on
(
'
error
'
,
(
previewOutputError
:
BusinessError
)
=>
{
function
onPreviewOutputError
(
previewOutput
:
camera
.
PreviewOutput
):
void
{
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
浏览文件 @
66e5ee93
...
@@ -11,236 +11,266 @@
...
@@ -11,236 +11,266 @@
[
各类Context的获取方式
](
../application-models/application-context-stage.md
)
[
各类Context的获取方式
](
../application-models/application-context-stage.md
)
```
ts
```
ts
import
camera
from
'
@ohos.multimedia.camera
'
;
import
camera
from
'
@ohos.multimedia.camera
'
;
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
import
media
from
'
@ohos.multimedia.media
'
;
import
media
from
'
@ohos.multimedia.media
'
;
import
type
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 创建CameraManager对象
async
function
videoRecording
(
context
:
featureAbility
.
Context
,
surfaceId
:
string
):
Promise
<
void
>
{
let
context
:
Context
=
getContext
(
this
);
// 创建CameraManager对象
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
if
(
!
cameraManager
)
{
if
(
!
cameraManager
)
{
console
.
error
(
"
camera.getCameraManager error
"
);
console
.
error
(
"
camera.getCameraManager error
"
);
return
;
return
;
}
}
// 监听相机状态变化
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
CameraStatusInfo
)
=>
{
console
.
log
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
console
.
log
(
`status:
${
cameraStatusInfo
.
status
}
`
);
});
// 获取相机列表
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
if
(
cameraArray
.
length
<=
0
)
{
console
.
error
(
"
cameraManager.getSupportedCameras error
"
)
return
;
}
// 获取相机设备支持的输出流能力
let
cameraOutputCap
:
camera
.
CameraOutputCapability
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
if
(
!
cameraOutputCap
)
{
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
)
return
;
}
console
.
log
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCap
));
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
previewProfiles
;
if
(
!
previewProfilesArray
)
{
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
);
}
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
photoProfiles
;
if
(
!
photoProfilesArray
)
{
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
}
let
videoProfilesArray
:
Array
<
camera
.
VideoProfile
>
=
cameraOutputCap
.
videoProfiles
;
// 监听相机状态变化
if
(
!
videoProfilesArray
)
{
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
CameraStatusInfo
)
=>
{
console
.
error
(
"
createOutput videoProfilesArray == null || undefined
"
);
console
.
log
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
}
console
.
log
(
`status:
${
cameraStatusInfo
.
status
}
`
);
});
let
metadataObjectTypesArray
:
Array
<
camera
.
MetadataObjectType
>
=
cameraOutputCap
.
supportedMetadataObjectTypes
;
// 获取相机列表
if
(
!
metadataObjectTypesArray
)
{
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
console
.
error
(
"
createOutput metadataObjectTypesArray == null || undefined
"
);
if
(
cameraArray
.
length
<=
0
)
{
}
console
.
error
(
"
cameraManager.getSupportedCameras error
"
)
return
;
}
// 获取相机设备支持的输出流能力
let
cameraOutputCap
:
camera
.
CameraOutputCapability
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
if
(
!
cameraOutputCap
)
{
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
)
return
;
}
console
.
log
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCap
));
// 配置参数以实际硬件设备支持的范围为准
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
previewProfiles
;
let
AVRecorderProfile
=
{
if
(
!
previewProfilesArray
)
{
audioBitrate
:
48000
,
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
);
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
.
AVRecorder
;
media
.
createAVRecorder
((
error
:
BusinessError
,
recorder
:
media
.
AVRecorder
)
=>
{
if
(
recorder
!=
null
)
{
avRecorder
=
recorder
;
console
.
log
(
'
createAVRecorder success
'
);
}
else
{
console
.
log
(
`createAVRecorder fail, error:
${
error
}
`
);
}
});
avRecorder
.
prepare
(
aVRecorderConfig
,
(
err
:
BusinessError
)
=>
{
if
(
err
==
null
)
{
console
.
log
(
'
prepare success
'
);
}
else
{
console
.
log
(
'
prepare 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
:
camera
.
VideoOutput
;
try
{
videoOutput
=
cameraManager
.
createVideoOutput
(
videoProfilesArray
[
0
],
videoSurfaceId
)
}
catch
(
error
)
{
console
.
error
(
'
Failed to create the videoOutput instance. errorCode =
'
+
error
.
code
);
}
// 监听视频输出错误信息
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
photoProfiles
;
videoOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
if
(
!
photoProfilesArray
)
{
console
.
log
(
`Preview output error code:
${
error
.
code
}
`
);
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
});
}
//创建会话
let
captureSession
:
camera
.
CaptureSession
;
try
{
captureSession
=
cameraManager
.
createCaptureSession
();
}
catch
(
error
)
{
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
error
.
code
);
}
// 监听session错误信息
let
videoProfilesArray
:
Array
<
camera
.
VideoProfile
>
=
cameraOutputCap
.
videoProfiles
;
captureSession
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
if
(
!
videoProfilesArray
)
{
console
.
log
(
`Capture session error code:
${
error
.
code
}
`
);
console
.
error
(
"
createOutput videoProfilesArray == null || undefined
"
);
});
}
// 开始配置会话
let
metadataObjectTypesArray
:
Array
<
camera
.
MetadataObjectType
>
=
cameraOutputCap
.
supportedMetadataObjectTypes
;
try
{
if
(
!
metadataObjectTypesArray
)
{
captureSession
.
beginConfig
();
console
.
error
(
"
createOutput metadataObjectTypesArray == null || undefined
"
);
}
catch
(
error
)
{
}
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
error
.
code
);
}
// 创建相机输入流
// 配置参数以实际硬件设备支持的范围为准
let
cameraInput
:
camera
.
CameraInput
;
let
aVRecorderProfile
:
media
.
AVRecorderProfile
=
{
try
{
audioBitrate
:
48000
,
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
audioChannels
:
2
,
}
catch
(
error
)
{
audioCodec
:
media
.
CodecMimeType
.
AUDIO_AAC
,
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
error
.
code
);
audioSampleRate
:
48000
,
}
fileFormat
:
media
.
ContainerFormatType
.
CFT_MPEG_4
,
videoBitrate
:
2000000
,
videoCodec
:
media
.
CodecMimeType
.
VIDEO_MPEG4
,
videoFrameWidth
:
640
,
videoFrameHeight
:
480
,
videoFrameRate
:
30
};
let
aVRecorderConfig
:
media
.
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
.
AVRecorder
;
media
.
createAVRecorder
((
error
:
BusinessError
,
recorder
:
media
.
AVRecorder
)
=>
{
if
(
recorder
!=
null
)
{
avRecorder
=
recorder
;
console
.
log
(
'
createAVRecorder success
'
);
}
else
{
console
.
log
(
`createAVRecorder fail, error:
${
error
}
`
);
}
});
avRecorder
.
prepare
(
aVRecorderConfig
,
(
err
:
BusinessError
)
=>
{
if
(
err
==
null
)
{
console
.
log
(
'
prepare success
'
);
}
else
{
console
.
log
(
`prepare failed. error:
${
JSON
.
stringify
(
err
)}
`
);
}
})
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. error:
${
JSON
.
stringify
(
err
)}
`
);
}
});
// 创建VideoOutput对象
let
videoOutput
:
camera
.
VideoOutput
;
try
{
videoOutput
=
cameraManager
.
createVideoOutput
(
videoProfilesArray
[
0
],
videoSurfaceId
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to create the videoOutput instance. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 监听cameraInput错误信息
// 监听视频输出错误信息
let
cameraDevice
:
camera
.
CameraDevice
=
cameraArray
[
0
];
videoOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
console
.
log
(
`Preview output error code:
${
error
.
code
}
`
);
console
.
log
(
`Camera input error code:
${
error
.
code
}
`
);
});
});
//创建会话
let
captureSession
:
camera
.
CaptureSession
;
try
{
captureSession
=
cameraManager
.
createCaptureSession
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to create the CaptureSession instance. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 打开相机
// 监听session错误信息
await
cameraInput
.
open
();
captureSession
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
log
(
`Capture session error code:
${
error
.
code
}
`
);
});
// 开始配置会话
try
{
captureSession
.
beginConfig
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to beginConfig. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 向会话中添加相机输入流
// 创建相机输入流
try
{
let
cameraInput
:
camera
.
CameraInput
;
captureSession
.
addInput
(
cameraInput
);
try
{
}
catch
(
error
)
{
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
console
.
error
(
'
Failed to addInput. errorCode =
'
+
error
.
code
);
}
catch
(
error
)
{
}
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to createCameraInput. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 创建预览输出流,其中参数 surfaceId 参考下面 XComponent 组件,预览流为XComponent组件提供的surface
// 监听cameraInput错误信息
let
previewOutput
:
camera
.
PreviewOutput
;
let
cameraDevice
:
camera
.
CameraDevice
=
cameraArray
[
0
];
try
{
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
console
.
log
(
`Camera input error code:
${
error
.
code
}
`
);
}
catch
(
error
)
{
});
console
.
error
(
"
Failed to create the PreviewOutput instance.
"
)
}
// 打开相机
try
{
await
cameraInput
.
open
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to open cameraInput. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 向会话中添加预览输入流
// 向会话中添加相机输入流
try
{
try
{
captureSession
.
addOutput
(
previewOutput
);
captureSession
.
addInput
(
cameraInput
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
error
.
code
);
let
err
=
error
as
BusinessError
;
}
console
.
error
(
`Failed to add cameraInput. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 向会话中添加录像输出流
// 创建预览输出流,其中参数 surfaceId 参考下面 XComponent 组件,预览流为XComponent组件提供的surface
try
{
let
previewOutput
:
camera
.
PreviewOutput
;
captureSession
.
addOutput
(
videoOutput
);
try
{
}
catch
(
error
)
{
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
console
.
error
(
'
Failed to addOutput(videoOutput). errorCode =
'
+
error
.
code
);
}
catch
(
error
)
{
}
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to create the PreviewOutput instance. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 提交会话配置
// 向会话中添加预览输入流
await
captureSession
.
commitConfig
();
try
{
captureSession
.
addOutput
(
previewOutput
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to add previewOutput. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 启动会话
// 向会话中添加录像输出流
await
captureSession
.
start
().
then
(()
=>
{
try
{
console
.
log
(
'
Promise returned to indicate the session start success.
'
);
captureSession
.
addOutput
(
videoOutput
);
});
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to add videoOutput. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 启动录像输出流
// 提交会话配置
videoOutput
.
start
(
async
(
err
:
BusinessError
)
=>
{
try
{
if
(
err
)
{
await
captureSession
.
commitConfig
();
console
.
error
(
'
Failed to start the video output ${err.message}
'
);
}
catch
(
error
)
{
return
;
let
err
=
error
as
BusinessError
;
console
.
error
(
`captureSession commitConfig error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
console
.
log
(
'
Callback invoked to indicate the video output start success.
'
);
});
// 开始录像
// 启动会话
avRecorder
.
start
().
then
(()
=>
{
try
{
console
.
log
(
'
videoRecorder start success
'
);
await
captureSession
.
start
();
});
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`captureSession start error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 停止录像输出流
// 启动录像输出流
videoOutput
.
stop
((
err
:
BusinessError
)
=>
{
videoOutput
.
start
((
err
:
BusinessError
)
=>
{
if
(
err
)
{
if
(
err
)
{
console
.
error
(
'
Failed to stop the video output ${err.message}
'
);
console
.
error
(
`Failed to start the video output. error:
${
JSON
.
stringify
(
err
)}
`
);
return
;
return
;
}
console
.
log
(
'
Callback invoked to indicate the video output start success.
'
);
});
// 开始录像
try
{
await
avRecorder
.
start
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`avRecorder start error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
console
.
log
(
'
Callback invoked to indicate the video output stop success.
'
);
});
// 停止录像
// 停止录像输出流
avRecorder
.
stop
().
then
(()
=>
{
videoOutput
.
stop
((
err
:
BusinessError
)
=>
{
console
.
log
(
'
stop success
'
);
if
(
err
)
{
});
console
.
error
(
`Failed to stop the video output. error:
${
JSON
.
stringify
(
err
)}
`
);
return
;
}
console
.
log
(
'
Callback invoked to indicate the video output stop success.
'
);
});
// 停止录像
try
{
await
avRecorder
.
stop
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`avRecorder stop error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 停止当前会话
// 停止当前会话
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
浏览文件 @
66e5ee93
...
@@ -9,6 +9,7 @@
...
@@ -9,6 +9,7 @@
1.
导入media模块。创建拍照输出流的SurfaceId以及拍照输出的数据,都需要用到系统提供的
[
media接口
](
../reference/apis/js-apis-media.md
)
能力,导入media接口的方法如下。
1.
导入media模块。创建拍照输出流的SurfaceId以及拍照输出的数据,都需要用到系统提供的
[
media接口
](
../reference/apis/js-apis-media.md
)
能力,导入media接口的方法如下。
```
ts
```
ts
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
import
media
from
'
@ohos.multimedia.media
'
;
import
media
from
'
@ohos.multimedia.media
'
;
```
```
...
@@ -17,32 +18,27 @@
...
@@ -17,32 +18,27 @@
系统提供的media接口可以创建一个录像AVRecorder实例,通过该实例的getInputSurface方法获取SurfaceId,与录像输出流做关联,处理录像输出流输出的数据。
系统提供的media接口可以创建一个录像AVRecorder实例,通过该实例的getInputSurface方法获取SurfaceId,与录像输出流做关联,处理录像输出流输出的数据。
```
ts
```
ts
let
avRecorder
:
media
.
AVRecorder
;
async
function
getVideoSurfaceId
(
aVRecorderConfig
:
media
.
AVRecorderConfig
):
Promise
<
string
>
{
// aVRecorderConfig可参考下一章节
media
.
createAVRecorder
((
error
:
BusinessError
,
recorder
:
media
.
AVRecorder
)
=>
{
let
avRecorder
:
media
.
AVRecorder
;
if
(
recorder
!=
null
)
{
media
.
createAVRecorder
((
error
:
BusinessError
,
recorder
:
media
.
AVRecorder
)
=>
{
avRecorder
=
recorder
;
if
(
recorder
!=
null
)
{
console
.
info
(
'
createAVRecorder success
'
);
avRecorder
=
recorder
;
}
else
{
console
.
info
(
'
createAVRecorder success
'
);
console
.
info
(
`createAVRecorder fail, error:
${
error
}
`
);
}
else
{
}
console
.
info
(
`createAVRecorder fail, error:
${
error
}
`
);
});
}
// aVRecorderConfig可参考下一章节
});
let
aVRecorderConfig
:
media
.
AVRecorderConfig
;
avRecorder
.
prepare
(
aVRecorderConfig
,
(
err
:
BusinessError
)
=>
{
if
(
err
==
null
)
{
console
.
log
(
'
prepare success
'
);
}
else
{
console
.
log
(
'
prepare failed and error is
'
+
err
.
message
);
}
});
let
videoSurfaceId
:
string
=
null
;
avRecorder
.
prepare
(
aVRecorderConfig
,
(
err
:
BusinessError
)
=>
{
avRecorder
.
getInputSurface
().
then
((
surfaceId
:
string
)
=>
{
if
(
err
==
null
)
{
console
.
info
(
'
getInputSurface success
'
);
console
.
log
(
'
prepare success
'
);
videoSurfaceId
=
surfaceId
;
}
else
{
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
log
(
'
prepare failed and error is
'
+
err
.
message
);
console
.
info
(
'
getInputSurface failed and catch error is
'
+
err
.
message
);
}
});
});
let
videoSurfaceId
=
await
avRecorder
.
getInputSurface
();
return
videoSurfaceId
;
}
```
```
3.
创建录像输出流。
3.
创建录像输出流。
...
@@ -50,44 +46,49 @@
...
@@ -50,44 +46,49 @@
通过CameraOutputCapability类中的videoProfiles,可获取当前设备支持的录像输出流。然后,定义创建录像的参数,通过createVideoOutput方法创建录像输出流。
通过CameraOutputCapability类中的videoProfiles,可获取当前设备支持的录像输出流。然后,定义创建录像的参数,通过createVideoOutput方法创建录像输出流。
```
ts
```
ts
let
videoProfilesArray
:
Array
<
camera
.
VideoProfile
>
=
cameraOutputCapability
.
videoProfiles
;
function
getVideoOutput
(
cameraManager
:
camera
.
CameraManager
,
videoSurfaceId
:
string
,
cameraOutputCapability
:
camera
.
CameraOutputCapability
):
camera
.
VideoOutput
{
if
(
!
videoProfilesArray
)
{
let
videoProfilesArray
:
Array
<
camera
.
VideoProfile
>
=
cameraOutputCapability
.
videoProfiles
;
console
.
error
(
"
createOutput videoProfilesArray == null || undefined
"
);
if
(
!
videoProfilesArray
)
{
}
console
.
error
(
"
createOutput videoProfilesArray == null || undefined
"
);
return
null
;
// 创建视频录制的参数
}
let
videoConfig
=
{
// AVRecorderProfile
videoSourceType
:
media
.
VideoSourceType
.
VIDEO_SOURCE_TYPE_SURFACE_YUV
,
let
aVRecorderProfile
:
media
.
AVRecorderProfile
=
{
profile
:
{
fileFormat
:
media
.
ContainerFormatType
.
CFT_MPEG_4
,
// 视频文件封装格式,只支持MP4
fileFormat
:
media
.
ContainerFormatType
.
CFT_MPEG_4
,
// 视频文件封装格式,只支持MP4
videoBitrate
:
100000
,
// 视频比特率
videoBitrate
:
100000
,
// 视频比特率
videoCodec
:
media
.
CodecMimeType
.
VIDEO_MPEG4
,
// 视频文件编码格式,支持mpeg4和avc两种格式
videoCodec
:
media
.
CodecMimeType
.
VIDEO_MPEG4
,
// 视频文件编码格式,支持mpeg4和avc两种格式
videoFrameWidth
:
640
,
// 视频分辨率的宽
videoFrameWidth
:
640
,
// 视频分辨率的宽
videoFrameHeight
:
480
,
// 视频分辨率的高
videoFrameHeight
:
480
,
// 视频分辨率的高
videoFrameRate
:
30
// 视频帧率
videoFrameRate
:
30
// 视频帧率
},
url
:
'
fd://35
'
,
rotation
:
90
// 90°为默认竖屏显示角度,如果由于设备原因或应用期望以其他方式显示等原因,请根据实际情况调整该参数
}
// 创建avRecorder
let
avRecorder
:
media
.
AVRecorder
;
media
.
createAVRecorder
((
error
:
BusinessError
,
recorder
:
media
.
AVRecorder
)
=>
{
if
(
recorder
!=
null
)
{
avRecorder
=
recorder
;
console
.
info
(
'
createAVRecorder success
'
);
}
else
{
console
.
info
(
`createAVRecorder fail, error:
${
error
}
`
);
}
}
});
// 创建视频录制的参数
// 设置视频录制的参数
let
aVRecorderConfig
:
media
.
AVRecorderConfig
=
{
avRecorder
.
prepare
(
videoConfig
);
videoSourceType
:
media
.
VideoSourceType
.
VIDEO_SOURCE_TYPE_SURFACE_YUV
,
// 创建VideoOutput对象
profile
:
aVRecorderProfile
,
let
videoOutput
:
camera
.
VideoOutput
;
url
:
'
fd://35
'
,
try
{
rotation
:
90
// 90°为默认竖屏显示角度,如果由于设备原因或应用期望以其他方式显示等原因,请根据实际情况调整该参数
videoOutput
=
cameraManager
.
createVideoOutput
(
videoProfilesArray
[
0
],
videoSurfaceId
);
}
}
catch
(
error
)
{
// 创建avRecorder
let
err
=
error
as
BusinessError
;
let
avRecorder
:
media
.
AVRecorder
;
console
.
error
(
'
Failed to create the videoOutput instance. errorCode =
'
+
err
.
code
);
media
.
createAVRecorder
((
error
:
BusinessError
,
recorder
:
media
.
AVRecorder
)
=>
{
if
(
recorder
!=
null
)
{
avRecorder
=
recorder
;
console
.
info
(
'
createAVRecorder success
'
);
}
else
{
console
.
info
(
`createAVRecorder fail, error:
${
error
}
`
);
}
});
// 设置视频录制的参数
avRecorder
.
prepare
(
aVRecorderConfig
);
// 创建VideoOutput对象
let
videoOutput
:
camera
.
VideoOutput
;
try
{
videoOutput
=
cameraManager
.
createVideoOutput
(
videoProfilesArray
[
0
],
videoSurfaceId
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to create the videoOutput instance. errorCode =
'
+
err
.
code
);
}
return
videoOutput
;
}
}
```
```
...
@@ -96,17 +97,21 @@
...
@@ -96,17 +97,21 @@
先通过videoOutput的start方法启动录像输出流,再通过avRecorder的start方法开始录像。
先通过videoOutput的start方法启动录像输出流,再通过avRecorder的start方法开始录像。
```
ts
```
ts
videoOutput
.
start
(
async
(
err
:
BusinessError
)
=>
{
async
function
startVideo
(
videoOutput
:
camera
.
VideoOutput
,
avRecorder
:
media
.
AVRecorder
):
Promise
<
void
>
{
if
(
err
)
{
videoOutput
.
start
(
async
(
err
:
BusinessError
)
=>
{
console
.
error
(
'
Failed to start the video output ${err.message}
'
);
if
(
err
)
{
return
;
console
.
error
(
'
Failed to start the video output ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the video output start success.
'
);
});
try
{
await
avRecorder
.
start
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`avRecorder start error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
console
.
info
(
'
Callback invoked to indicate the video output start success.
'
);
}
});
avRecorder
.
start
().
then
(()
=>
{
console
.
info
(
'
avRecorder start success
'
);
});
```
```
5.
停止录像。
5.
停止录像。
...
@@ -114,17 +119,21 @@
...
@@ -114,17 +119,21 @@
先通过avRecorder的stop方法停止录像,再通过videoOutput的stop方法停止录像输出流。
先通过avRecorder的stop方法停止录像,再通过videoOutput的stop方法停止录像输出流。
```
ts
```
ts
videoRecorder
.
stop
().
then
(()
=>
{
async
function
stopVideo
(
videoOutput
:
camera
.
VideoOutput
,
avRecorder
:
media
.
AVRecorder
):
Promise
<
void
>
{
console
.
info
(
'
stop success
'
);
try
{
});
await
avRecorder
.
stop
();
}
catch
(
error
)
{
videoOutput
.
stop
((
err
:
BusinessError
)
=>
{
let
err
=
error
as
BusinessError
;
if
(
err
)
{
console
.
error
(
`avRecorder stop error:
${
JSON
.
stringify
(
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.
'
);
});
}
```
```
...
@@ -135,23 +144,29 @@
...
@@ -135,23 +144,29 @@
-
通过注册固定的frameStart回调函数获取监听录像开始结果,videoOutput创建成功时即可监听,录像第一次曝光时触发,有该事件返回结果则认为录像开始。
-
通过注册固定的frameStart回调函数获取监听录像开始结果,videoOutput创建成功时即可监听,录像第一次曝光时触发,有该事件返回结果则认为录像开始。
```
ts
```
ts
videoOutput
.
on
(
'
frameStart
'
,
()
=>
{
function
onVideoOutputFrameStart
(
videoOutput
:
camera
.
VideoOutput
):
void
{
console
.
info
(
'
Video frame started
'
);
videoOutput
.
on
(
'
frameStart
'
,
()
=>
{
});
console
.
info
(
'
Video frame started
'
);
});
}
```
```
-
通过注册固定的frameEnd回调函数获取监听录像结束结果,videoOutput创建成功时即可监听,录像完成最后一帧时触发,有该事件返回结果则认为录像流已结束。
-
通过注册固定的frameEnd回调函数获取监听录像结束结果,videoOutput创建成功时即可监听,录像完成最后一帧时触发,有该事件返回结果则认为录像流已结束。
```
ts
```
ts
videoOutput
.
on
(
'
frameEnd
'
,
()
=>
{
function
onVideoOutputFrameEnd
(
videoOutput
:
camera
.
VideoOutput
):
void
{
console
.
info
(
'
Video frame ended
'
);
videoOutput
.
on
(
'
frameEnd
'
,
()
=>
{
});
console
.
info
(
'
Video frame ended
'
);
});
}
```
```
-
通过注册固定的error回调函数获取监听录像输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
-
通过注册固定的error回调函数获取监听录像输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
```
ts
```
ts
videoOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
function
onVideoOutputError
(
videoOutput
:
camera
.
VideoOutput
):
void
{
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
浏览文件 @
66e5ee93
...
@@ -14,81 +14,112 @@
...
@@ -14,81 +14,112 @@
完成会话配置后,应用提交和开启会话,可以开始调用相机相关功能。
完成会话配置后,应用提交和开启会话,可以开始调用相机相关功能。
## 开发步骤
## 开发步骤
1.
导入相关接口,导入方法如下。
```
ts
import
camera
from
'
@ohos.multimedia.camera
'
;
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
import
type
featureAbility
from
'
@ohos.ability.featureAbility
'
;
```
1
.
调用cameraManager类中的createCaptureSession()方法创建一个会话。
2
.
调用cameraManager类中的createCaptureSession()方法创建一个会话。
```
ts
```
ts
let
captureSession
:
camera
.
CaptureSession
;
function
getCaptureSession
(
cameraManager
:
camera
.
CameraManager
):
camera
.
CaptureSession
{
try
{
let
captureSession
:
camera
.
CaptureSession
;
captureSession
=
cameraManager
.
createCaptureSession
();
try
{
}
catch
(
error
)
{
captureSession
=
cameraManager
.
createCaptureSession
();
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
err
.
code
);
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to create the CaptureSession instance. error:
${
JSON
.
stringify
(
err
)}
`
);
}
return
captureSession
;
}
}
```
```
2
.
调用captureSession类中的beginConfig()方法配置会话。
3
.
调用captureSession类中的beginConfig()方法配置会话。
```
ts
```
ts
try
{
function
beginConfig
(
captureSession
:
camera
.
CaptureSession
):
void
{
captureSession
.
beginConfig
();
try
{
}
catch
(
error
)
{
captureSession
.
beginConfig
();
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
err
.
code
);
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to beginConfig. error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
}
```
```
3
.
使能。向会话中添加相机的输入流和输出流,调用captureSession.addInput()添加相机的输入流;调用captureSession.addOutput()添加相机的输出流。以下示例代码以添加预览流previewOutput和拍照流photoOutput为例,即当前模式支持拍照和预览。
4
.
使能。向会话中添加相机的输入流和输出流,调用captureSession.addInput()添加相机的输入流;调用captureSession.addOutput()添加相机的输出流。以下示例代码以添加预览流previewOutput和拍照流photoOutput为例,即当前模式支持拍照和预览。
调用captureSession类中的commitConfig()和start()方法提交相关配置,并启动会话。
调用captureSession类中的commitConfig()和start()方法提交相关配置,并启动会话。
```
ts
```
ts
try
{
async
function
startSession
(
captureSession
:
camera
.
CaptureSession
,
cameraInput
:
camera
.
CameraInput
,
previewOutput
:
camera
.
PreviewOutput
,
photoOutput
:
camera
.
PhotoOutput
):
Promise
<
void
>
{
captureSession
.
addInput
(
cameraInput
);
try
{
}
catch
(
error
)
{
captureSession
.
addInput
(
cameraInput
);
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to addInput. errorCode =
'
+
err
.
code
);
let
err
=
error
as
BusinessError
;
}
console
.
error
(
`Failed to addInput. error:
${
JSON
.
stringify
(
err
)}
`
);
try
{
}
captureSession
.
addOutput
(
previewOutput
);
try
{
}
catch
(
error
)
{
captureSession
.
addOutput
(
previewOutput
);
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
err
.
code
);
let
err
=
error
as
BusinessError
;
}
console
.
error
(
`Failed to add previewOutput. error:
${
JSON
.
stringify
(
err
)}
`
);
try
{
}
captureSession
.
addOutput
(
photoOutput
);
try
{
}
catch
(
error
)
{
captureSession
.
addOutput
(
photoOutput
);
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(photoOutput). errorCode =
'
+
err
.
code
);
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to add photoOutput. error:
${
JSON
.
stringify
(
err
)}
`
);
}
try
{
await
captureSession
.
commitConfig
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to commitConfig. error:
${
JSON
.
stringify
(
err
)}
`
);
}
try
{
await
captureSession
.
start
()
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to start. error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
}
await
captureSession
.
commitConfig
();
await
captureSession
.
start
().
then
(()
=>
{
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
});
```
```
4
.
会话控制。调用captureSession类中的stop()方法可以停止当前会话。调用removeOutput()和addOutput()方法可以完成会话切换控制。以下示例代码以移除拍照流photoOutput,添加视频流videoOutput为例,完成了拍照到录像的切换。
5
.
会话控制。调用captureSession类中的stop()方法可以停止当前会话。调用removeOutput()和addOutput()方法可以完成会话切换控制。以下示例代码以移除拍照流photoOutput,添加视频流videoOutput为例,完成了拍照到录像的切换。
```
ts
```
ts
await
captureSession
.
stop
();
async
function
switchOutput
(
captureSession
:
camera
.
CaptureSession
,
videoOutput
:
camera
.
VideoOutput
,
photoOutput
:
camera
.
PhotoOutput
):
Promise
<
void
>
{
try
{
try
{
captureSession
.
beginConfig
();
await
captureSession
.
stop
();
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
err
.
code
);
console
.
error
(
`Failed to stop. error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
// 从会话中移除拍照输出流
try
{
try
{
captureSession
.
removeOutput
(
photoOutput
);
captureSession
.
beginConfig
();
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to removeOutput(photoOutput). errorCode =
'
+
err
.
code
);
console
.
error
(
`Failed to beginConfig. error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
// 向会话中添加视频输出流
// 从会话中移除拍照输出流
try
{
try
{
captureSession
.
addOutput
(
videoOutput
);
captureSession
.
removeOutput
(
photoOutput
);
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to addOutput(videoOutput). errorCode =
'
+
err
.
code
);
console
.
error
(
`Failed to remove photoOutput. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 向会话中添加视频输出流
try
{
captureSession
.
addOutput
(
videoOutput
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to add videoOutput. error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
}
```
```
zh-cn/application-dev/media/camera-shooting-case.md
浏览文件 @
66e5ee93
...
@@ -11,245 +11,247 @@
...
@@ -11,245 +11,247 @@
```
ts
```
ts
import
camera
from
'
@ohos.multimedia.camera
'
;
import
camera
from
'
@ohos.multimedia.camera
'
;
import
image
from
'
@ohos.multimedia.image
'
;
import
image
from
'
@ohos.multimedia.image
'
;
import
media
from
'
@ohos.multimedia.media
'
;
import
{
BusinessError
}
from
'
@ohos.base
'
;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 创建CameraManager对象
let
context
:
Context
=
getContext
(
this
);
async
function
cameraShootingCase
(
context
:
featureAbility
.
Context
,
surfaceId
:
string
)
{
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
// 创建CameraManager对象
if
(
!
cameraManager
)
{
let
cameraManager
:
camera
.
CameraManager
=
camera
.
getCameraManager
(
context
);
console
.
error
(
"
camera.getCameraManager error
"
);
if
(
!
cameraManager
)
{
return
;
console
.
error
(
"
camera.getCameraManager error
"
);
}
return
;
// 监听相机状态变化
}
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
CameraStatusInfo
)
=>
{
// 监听相机状态变化
console
.
info
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
cameraManager
.
on
(
'
cameraStatus
'
,
(
err
:
BusinessError
,
cameraStatusInfo
:
camera
.
CameraStatusInfo
)
=>
{
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
console
.
info
(
`camera :
${
cameraStatusInfo
.
camera
.
cameraId
}
`
);
});
console
.
info
(
`status:
${
cameraStatusInfo
.
status
}
`
);
});
// 获取相机列表
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
// 获取相机列表
if
(
cameraArray
.
length
<=
0
)
{
let
cameraArray
:
Array
<
camera
.
CameraDevice
>
=
cameraManager
.
getSupportedCameras
();
console
.
error
(
"
cameraManager.getSupportedCameras error
"
);
if
(
cameraArray
.
length
<=
0
)
{
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
);
// 获取相机连接类型
}
// 创建相机输入流
for
(
let
index
=
0
;
index
<
cameraArray
.
length
;
index
++
)
{
let
cameraInput
:
camera
.
CameraInput
;
console
.
info
(
'
cameraId :
'
+
cameraArray
[
index
].
cameraId
);
// 获取相机ID
try
{
console
.
info
(
'
cameraPosition :
'
+
cameraArray
[
index
].
cameraPosition
);
// 获取相机位置
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
console
.
info
(
'
cameraType :
'
+
cameraArray
[
index
].
cameraType
);
// 获取相机类型
}
catch
(
error
)
{
console
.
info
(
'
connectionType :
'
+
cameraArray
[
index
].
connectionType
);
// 获取相机连接类型
let
err
=
error
as
BusinessError
;
}
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
err
.
code
);
}
// 监听cameraInput错误信息
// 创建相机输入流
let
cameraDevice
:
camera
.
CameraDevice
=
cameraArray
[
0
];
let
cameraInput
:
camera
.
CameraInput
;
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
try
{
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
cameraInput
=
cameraManager
.
createCameraInput
(
cameraArray
[
0
]);
})
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to createCameraInput errorCode =
'
+
err
.
code
);
}
// 打开相机
// 监听cameraInput错误信息
await
cameraInput
.
open
();
let
cameraDevice
:
camera
.
CameraDevice
=
cameraArray
[
0
];
cameraInput
.
on
(
'
error
'
,
cameraDevice
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Camera input error code:
${
error
.
code
}
`
);
})
// 获取相机设备支持的输出流能力
// 打开相机
let
cameraOutputCap
:
camera
.
CameraOutputCapability
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
await
cameraInput
.
open
();
if
(
!
cameraOutputCap
)
{
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
);
return
;
}
console
.
info
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCap
));
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
previewProfiles
;
if
(
!
previewProfilesArray
)
{
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
);
}
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
photoProfiles
;
if
(
!
photoProfilesArray
)
{
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
}
// 创建预览输出流,其中参数 surfaceId 参考上文 XComponent 组件,预览流为XComponent组件提供的surface
let
previewOutput
:
camera
.
PreviewOutput
;
try
{
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to create the PreviewOutput instance. error code:
${
err
.
code
}
`
);
}
// 监听预览输出错误信息
// 获取相机设备支持的输出流能力
previewOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
let
cameraOutputCap
:
camera
.
CameraOutputCapability
=
cameraManager
.
getSupportedOutputCapability
(
cameraArray
[
0
]);
console
.
info
(
`Preview output error code:
${
error
.
code
}
`
);
if
(
!
cameraOutputCap
)
{
});
console
.
error
(
"
cameraManager.getSupportedOutputCapability error
"
);
return
;
// 创建ImageReceiver对象,并设置照片参数:分辨率大小是根据前面 photoProfilesArray 获取的当前设备所支持的拍照分辨率大小去设置
}
let
imageReceiver
:
image
.
ImageReceiver
=
image
.
createImageReceiver
(
1920
,
1080
,
4
,
8
);
console
.
info
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCap
));
// 获取照片显示SurfaceId
let
photoSurfaceId
:
string
=
await
imageReceiver
.
getReceivingSurfaceId
();
// 创建拍照输出流
let
photoOutput
:
camera
.
PhotoOutput
;
try
{
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
err
.
code
);
}
//创建会话
let
captureSession
:
camera
.
CaptureSession
;
try
{
captureSession
=
cameraManager
.
createCaptureSession
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
err
.
code
);
}
// 监听session错误信息
let
previewProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
previewProfiles
;
captureSession
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
if
(
!
previewProfilesArray
)
{
console
.
info
(
`Capture session error code:
${
error
.
code
}
`
);
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
);
});
}
// 开始配置会话
try
{
captureSession
.
beginConfig
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
err
.
code
);
}
// 向会话中添加相机输入流
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCap
.
photoProfiles
;
try
{
if
(
!
photoProfilesArray
)
{
captureSession
.
addInput
(
cameraInput
);
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
}
catch
(
error
)
{
}
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to addInput. errorCode =
'
+
err
.
code
);
}
// 向会话中添加预览输出流
// 创建预览输出流,其中参数 surfaceId 参考上文 XComponent 组件,预览流为XComponent组件提供的surface
try
{
let
previewOutput
:
camera
.
PreviewOutput
;
captureSession
.
addOutput
(
previewOutput
);
try
{
}
catch
(
error
)
{
previewOutput
=
cameraManager
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
);
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
err
.
code
);
let
err
=
error
as
BusinessError
;
}
console
.
error
(
`Failed to create the PreviewOutput instance. error code:
${
err
.
code
}
`
);
}
// 向会话中添加拍照输出流
// 监听预览输出错误信息
try
{
previewOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
captureSession
.
addOutput
(
photoOutput
);
console
.
info
(
`Preview output error code:
${
error
.
code
}
`
);
}
catch
(
error
)
{
});
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to addOutput(photoOutput). errorCode =
'
+
err
.
code
);
// 创建ImageReceiver对象,并设置照片参数:分辨率大小是根据前面 photoProfilesArray 获取的当前设备所支持的拍照分辨率大小去设置
}
let
imageReceiver
:
image
.
ImageReceiver
=
image
.
createImageReceiver
(
1920
,
1080
,
4
,
8
);
// 获取照片显示SurfaceId
let
photoSurfaceId
:
string
=
await
imageReceiver
.
getReceivingSurfaceId
();
// 创建拍照输出流
let
photoOutput
:
camera
.
PhotoOutput
;
try
{
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
err
.
code
);
}
//创建会话
let
captureSession
:
camera
.
CaptureSession
;
try
{
captureSession
=
cameraManager
.
createCaptureSession
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to create the CaptureSession instance. errorCode =
'
+
err
.
code
);
}
// 提交会话配置
// 监听session错误信息
await
captureSession
.
commitConfig
();
captureSession
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
console
.
info
(
`Capture session error code:
${
error
.
code
}
`
);
// 启动会话
});
await
captureSession
.
start
().
then
(()
=>
{
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
// 开始配置会话
});
try
{
// 判断设备是否支持闪光灯
captureSession
.
beginConfig
();
let
flashStatus
:
boolean
;
}
catch
(
error
)
{
try
{
let
err
=
error
as
BusinessError
;
flashStatus
=
captureSession
.
hasFlash
();
console
.
error
(
'
Failed to beginConfig. errorCode =
'
+
err
.
code
);
}
catch
(
error
)
{
}
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to hasFlash. errorCode =
'
+
err
.
code
);
}
console
.
info
(
'
Promise returned with the flash light support status:
'
+
flashStatus
);
if
(
flashStatus
)
{
// 向会话中添加相机输入流
// 判断是否支持自动闪光灯模式
let
flashModeStatus
:
boolean
;
try
{
try
{
let
status
:
boolean
=
captureSession
.
isFlashModeSupported
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
captureSession
.
addInput
(
cameraInput
);
flashModeStatus
=
status
;
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to
check whether the flash mode is supported
. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to
addInput
. errorCode =
'
+
err
.
code
);
}
}
if
(
flashModeStatus
)
{
// 设置自动闪光灯模式
// 向会话中添加预览输出流
try
{
captureSession
.
addOutput
(
previewOutput
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to addOutput(previewOutput). errorCode =
'
+
err
.
code
);
}
// 向会话中添加拍照输出流
try
{
captureSession
.
addOutput
(
photoOutput
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to addOutput(photoOutput). errorCode =
'
+
err
.
code
);
}
// 提交会话配置
await
captureSession
.
commitConfig
();
// 启动会话
await
captureSession
.
start
().
then
(()
=>
{
console
.
info
(
'
Promise returned to indicate the session start success.
'
);
});
// 判断设备是否支持闪光灯
let
flashStatus
:
boolean
;
try
{
flashStatus
=
captureSession
.
hasFlash
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to hasFlash. errorCode =
'
+
err
.
code
);
}
console
.
info
(
'
Promise returned with the flash light support status:
'
+
flashStatus
);
if
(
flashStatus
)
{
// 判断是否支持自动闪光灯模式
let
flashModeStatus
:
boolean
;
try
{
try
{
captureSession
.
setFlashMode
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
let
status
:
boolean
=
captureSession
.
isFlashModeSupported
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
flashModeStatus
=
status
;
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to set the flash mode. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to check whether the flash mode is supported. errorCode =
'
+
err
.
code
);
}
if
(
flashModeStatus
)
{
// 设置自动闪光灯模式
try
{
captureSession
.
setFlashMode
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to set the flash mode. errorCode =
'
+
err
.
code
);
}
}
}
}
}
}
// 判断是否支持连续自动变焦模式
// 判断是否支持连续自动变焦模式
let
focusModeStatus
:
boolean
;
let
focusModeStatus
:
boolean
;
try
{
let
status
:
boolean
=
captureSession
.
isFocusModeSupported
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
focusModeStatus
=
status
;
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to check whether the focus mode is supported. errorCode =
'
+
err
.
code
);
}
if
(
focusModeStatus
)
{
// 设置连续自动变焦模式
try
{
try
{
captureSession
.
setFocusMode
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
let
status
:
boolean
=
captureSession
.
isFocusModeSupported
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
focusModeStatus
=
status
;
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to
set the focus mode
. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to
check whether the focus mode is supported
. errorCode =
'
+
err
.
code
);
}
}
}
// 获取相机支持的可变焦距比范围
if
(
focusModeStatus
)
{
let
zoomRatioRange
:
Array
<
number
>
;
// 设置连续自动变焦模式
try
{
try
{
zoomRatioRange
=
captureSession
.
getZoomRatioRange
();
captureSession
.
setFocusMode
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to get the zoom ratio range. errorCode =
'
+
err
.
code
);
console
.
error
(
'
Failed to set the focus mode. errorCode =
'
+
err
.
code
);
}
}
}
// 设置可变焦距比
// 获取相机支持的可变焦距比范围
try
{
let
zoomRatioRange
:
Array
<
number
>
;
captureSession
.
setZoomRatio
(
zoomRatioRange
[
0
]);
try
{
}
catch
(
error
)
{
zoomRatioRange
=
captureSession
.
getZoomRatioRange
();
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to set the zoom ratio value. errorCode =
'
+
err
.
code
);
let
err
=
error
as
BusinessError
;
}
console
.
error
(
'
Failed to get the zoom ratio range. errorCode =
'
+
err
.
code
);
let
settings
:
camera
.
PhotoCaptureSetting
=
{
}
quality
:
camera
.
QualityLevel
.
QUALITY_LEVEL_HIGH
,
// 设置图片质量高
rotation
:
camera
.
ImageRotation
.
ROTATION_0
// 设置图片旋转角度0
// 设置可变焦距比
}
try
{
// 使用当前拍照设置进行拍照
captureSession
.
setZoomRatio
(
zoomRatioRange
[
0
]);
photoOutput
.
capture
(
settings
,
async
(
err
:
BusinessError
)
=>
{
}
catch
(
error
)
{
if
(
err
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
console
.
error
(
'
Failed to set the zoom ratio value. errorCode =
'
+
err
.
code
);
return
;
}
let
photoCaptureSetting
:
camera
.
PhotoCaptureSetting
=
{
quality
:
camera
.
QualityLevel
.
QUALITY_LEVEL_HIGH
,
// 设置图片质量高
rotation
:
camera
.
ImageRotation
.
ROTATION_0
// 设置图片旋转角度0
}
}
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
// 使用当前拍照设置进行拍照
});
photoOutput
.
capture
(
photoCaptureSetting
,
(
err
:
BusinessError
)
=>
{
// 停止当前会话
if
(
err
)
{
captureSession
.
stop
();
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
return
;
}
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
});
// 停止当前会话
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
浏览文件 @
66e5ee93
...
@@ -10,6 +10,8 @@
...
@@ -10,6 +10,8 @@
```
ts
```
ts
import
image
from
'
@ohos.multimedia.image
'
;
import
image
from
'
@ohos.multimedia.image
'
;
import
camera
from
'
@ohos.multimedia.camera
'
;
import
type
{
BusinessError
}
from
'
@ohos.base
'
;
```
```
2.
获取SurfaceId。
2.
获取SurfaceId。
...
@@ -17,16 +19,18 @@
...
@@ -17,16 +19,18 @@
通过image的createImageReceiver方法创建ImageReceiver实例,再通过实例的getReceivingSurfaceId方法获取SurfaceId,与拍照输出流相关联,获取拍照输出流的数据。
通过image的createImageReceiver方法创建ImageReceiver实例,再通过实例的getReceivingSurfaceId方法获取SurfaceId,与拍照输出流相关联,获取拍照输出流的数据。
```
ts
```
ts
async
function
getImageReceiverSurfaceId
()
{
async
function
getImageReceiverSurfaceId
():
Promise
<
string
>
{
let
photoSurfaceId
:
string
;
let
receiver
:
image
.
ImageReceiver
=
image
.
createImageReceiver
(
640
,
480
,
4
,
8
);
let
receiver
:
image
.
ImageReceiver
=
image
.
createImageReceiver
(
640
,
480
,
4
,
8
);
console
.
info
(
'
before ImageReceiver check
'
);
console
.
info
(
'
before ImageReceiver check
'
);
if
(
receiver
!==
undefined
)
{
if
(
receiver
!==
undefined
)
{
console
.
info
(
'
ImageReceiver is ok
'
);
console
.
info
(
'
ImageReceiver is ok
'
);
let
photoSurfaceId
:
string
=
await
receiver
.
getReceivingSurfaceId
();
photoSurfaceId
=
await
receiver
.
getReceivingSurfaceId
();
console
.
info
(
'
ImageReceived id:
'
+
JSON
.
stringify
(
photoSurfaceId
)
);
console
.
info
(
`ImageReceived id:
${
JSON
.
stringify
(
photoSurfaceId
)}
`
);
}
else
{
}
else
{
console
.
info
(
'
ImageReceiver is not ok
'
);
console
.
info
(
'
ImageReceiver is not ok
'
);
}
}
return
photoSurfaceId
;
}
}
```
```
...
@@ -35,16 +39,19 @@
...
@@ -35,16 +39,19 @@
通过CameraOutputCapability类中的photoProfiles()方法,可获取当前设备支持的拍照输出流,通过createPhotoOutput()方法传入支持的某一个输出流及步骤一获取的SurfaceId创建拍照输出流。
通过CameraOutputCapability类中的photoProfiles()方法,可获取当前设备支持的拍照输出流,通过createPhotoOutput()方法传入支持的某一个输出流及步骤一获取的SurfaceId创建拍照输出流。
```
ts
```
ts
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCapability
.
photoProfiles
;
function
getPhotoOutput
(
cameraManager
:
camera
.
CameraManager
,
cameraOutputCapability
:
camera
.
CameraOutputCapability
,
photoSurfaceId
:
string
):
camera
.
PhotoOutput
{
if
(
!
photoProfilesArray
)
{
let
photoProfilesArray
:
Array
<
camera
.
Profile
>
=
cameraOutputCapability
.
photoProfiles
;
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
if
(
!
photoProfilesArray
)
{
}
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
);
let
photoOutput
:
camera
.
PhotoOutput
;
}
try
{
let
photoOutput
:
camera
.
PhotoOutput
;
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
);
try
{
}
catch
(
error
)
{
photoOutput
=
cameraManager
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
);
let
err
=
error
as
BusinessError
;
}
catch
(
error
)
{
console
.
error
(
'
Failed to createPhotoOutput errorCode =
'
+
err
.
code
);
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to createPhotoOutput. error:
${
JSON
.
stringify
(
err
)}
`
);
}
return
photoOutput
;
}
}
```
```
...
@@ -53,67 +60,69 @@
...
@@ -53,67 +60,69 @@
配置相机的参数可以调整拍照的一些功能,包括闪光灯、变焦、焦距等。
配置相机的参数可以调整拍照的一些功能,包括闪光灯、变焦、焦距等。
```
ts
```
ts
// 判断设备是否支持闪光灯
function
configuringSession
(
captureSession
:
camera
.
CaptureSession
):
void
{
let
flashStatus
:
boolean
;
// 判断设备是否支持闪光灯
try
{
let
flashStatus
:
boolean
;
flashStatus
=
captureSession
.
hasFlash
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to hasFlash. errorCode =
'
+
err
.
code
);
}
console
.
info
(
'
Promise returned with the flash light support status:
'
+
flashStatus
);
if
(
flashStatus
)
{
// 判断是否支持自动闪光灯模式
let
flashModeStatus
:
boolean
;
try
{
try
{
let
status
:
boolean
=
captureSession
.
isFlashModeSupported
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
flashStatus
=
captureSession
.
hasFlash
();
flashModeStatus
=
status
;
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to check whether the flash mode is supported. errorCode =
'
+
err
.
code
);
console
.
error
(
`Failed to hasFlash. error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
if
(
flashModeStatus
)
{
console
.
info
(
`Promise returned with the flash light support status:
${
flashStatus
}
`
);
// 设置自动闪光灯模式
if
(
flashStatus
)
{
// 判断是否支持自动闪光灯模式
let
flashModeStatus
:
boolean
;
try
{
try
{
captureSession
.
setFlashMode
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
let
status
:
boolean
=
captureSession
.
isFlashModeSupported
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
flashModeStatus
=
status
;
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to set the flash mode. errorCode =
'
+
err
.
code
);
console
.
error
(
`Failed to check whether the flash mode is supported. error:
${
JSON
.
stringify
(
err
)}
`
);
}
if
(
flashModeStatus
)
{
// 设置自动闪光灯模式
try
{
captureSession
.
setFlashMode
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to set the flash mode. error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
}
}
}
}
// 判断是否支持连续自动变焦模式
// 判断是否支持连续自动变焦模式
let
focusModeStatus
:
boolean
;
let
focusModeStatus
:
boolean
;
try
{
let
status
:
boolean
=
captureSession
.
isFocusModeSupported
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
focusModeStatus
=
status
;
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to check whether the focus mode is supported. errorCode =
'
+
err
.
code
);
}
if
(
focusModeStatus
)
{
// 设置连续自动变焦模式
try
{
try
{
let
status
:
boolean
=
captureSession
.
isFocusModeSupported
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
focusModeStatus
=
status
;
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to check whether the focus mode is supported. error:
${
JSON
.
stringify
(
err
)}
`
);
}
if
(
focusModeStatus
)
{
// 设置连续自动变焦模式
try
{
captureSession
.
setFocusMode
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
captureSession
.
setFocusMode
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to set the focus mode. error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
// 获取相机支持的可变焦距比范围
let
zoomRatioRange
:
Array
<
number
>
;
try
{
zoomRatioRange
=
captureSession
.
getZoomRatioRange
();
}
catch
(
error
)
{
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to set the focus mode. errorCode =
'
+
err
.
code
);
console
.
error
(
`Failed to get the zoom ratio range. error:
${
JSON
.
stringify
(
err
)}
`
);
}
// 设置可变焦距比
try
{
captureSession
.
setZoomRatio
(
zoomRatioRange
[
0
]);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
`Failed to set the zoom ratio value. error:
${
JSON
.
stringify
(
err
)}
`
);
}
}
}
// 获取相机支持的可变焦距比范围
let
zoomRatioRange
:
Array
<
number
>
;
try
{
zoomRatioRange
=
captureSession
.
getZoomRatioRange
();
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to get the zoom ratio range. errorCode =
'
+
err
.
code
);
}
// 设置可变焦距比
try
{
captureSession
.
setZoomRatio
(
zoomRatioRange
[
0
]);
}
catch
(
error
)
{
let
err
=
error
as
BusinessError
;
console
.
error
(
'
Failed to set the zoom ratio value. errorCode =
'
+
err
.
code
);
}
}
```
```
...
@@ -122,19 +131,21 @@
...
@@ -122,19 +131,21 @@
通过photoOutput类的capture()方法,执行拍照任务。该方法有两个参数,第一个参数为拍照设置参数的setting,setting中可以设置照片的质量和旋转角度,第二参数为回调函数。
通过photoOutput类的capture()方法,执行拍照任务。该方法有两个参数,第一个参数为拍照设置参数的setting,setting中可以设置照片的质量和旋转角度,第二参数为回调函数。
```
ts
```
ts
let
settings
:
camera
.
PhotoCaptureSetting
=
{
function
capture
(
captureLocation
:
camera
.
Location
,
photoOutput
:
camera
.
PhotoOutput
):
void
{
quality
:
camera
.
QualityLevel
.
QUALITY_LEVEL_HIGH
,
// 设置图片质量高
let
settings
:
camera
.
PhotoCaptureSetting
=
{
rotation
:
camera
.
ImageRotation
.
ROTATION_0
,
// 设置图片旋转角度0
quality
:
camera
.
QualityLevel
.
QUALITY_LEVEL_HIGH
,
// 设置图片质量高
location
:
captureLocation
,
// 设置图片地理位置
rotation
:
camera
.
ImageRotation
.
ROTATION_0
,
// 设置图片旋转角度0
mirror
:
false
// 设置镜像使能开关(默认关)
location
:
captureLocation
,
// 设置图片地理位置
};
mirror
:
false
// 设置镜像使能开关(默认关)
photoOutput
.
capture
(
settings
,
async
(
err
:
BusinessError
)
=>
{
};
if
(
err
)
{
photoOutput
.
capture
(
settings
,
(
err
:
BusinessError
)
=>
{
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
if
(
err
)
{
return
;
console
.
error
(
`Failed to capture the photo. error:
${
JSON
.
stringify
(
err
)}
`
);
}
return
;
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
}
});
console
.
info
(
'
Callback invoked to indicate the photo capture request success.
'
);
});
}
```
```
## 状态监听
## 状态监听
...
@@ -144,24 +155,30 @@
...
@@ -144,24 +155,30 @@
-
通过注册固定的captureStart回调函数获取监听拍照开始结果,photoOutput创建成功时即可监听,拍照第一次曝光时触发,该事件返回此次拍照的captureId。
-
通过注册固定的captureStart回调函数获取监听拍照开始结果,photoOutput创建成功时即可监听,拍照第一次曝光时触发,该事件返回此次拍照的captureId。
```
ts
```
ts
photoOutput
.
on
(
'
captureStart
'
,
(
err
:
BusinessError
,
captureId
:
number
)
=>
{
function
onPhotoOutputCaptureStart
(
photoOutput
:
camera
.
PhotoOutput
):
void
{
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
)
。
-
通过注册固定的captureEnd回调函数获取监听拍照结束结果,photoOutput创建成功时即可监听,该事件返回结果为拍照完全结束后的相关信息
[
CaptureEndInfo
](
../reference/apis/js-apis-camera.md#captureendinfo
)
。
```
ts
```
ts
photoOutput
.
on
(
'
captureEnd
'
,
(
err
:
BusinessError
,
captureEndInfo
:
camera
.
CaptureEndInfo
)
=>
{
function
onPhotoOutputCaptureEnd
(
photoOutput
:
camera
.
PhotoOutput
):
void
{
console
.
info
(
`photo capture end, captureId :
${
captureEndInfo
.
captureId
}
`
);
photoOutput
.
on
(
'
captureEnd
'
,
(
err
:
BusinessError
,
captureEndInfo
:
camera
.
CaptureEndInfo
)
=>
{
console
.
info
(
`frameCount :
${
captureEndInfo
.
frameCount
}
`
);
console
.
info
(
`photo capture end, captureId :
${
captureEndInfo
.
captureId
}
`
);
});
console
.
info
(
`frameCount :
${
captureEndInfo
.
frameCount
}
`
);
});
}
```
```
-
通过注册固定的error回调函数获取监听拍照输出流的错误结果。callback返回拍照输出接口使用错误时的对应错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
-
通过注册固定的error回调函数获取监听拍照输出流的错误结果。callback返回拍照输出接口使用错误时的对应错误码,错误码类型参见
[
CameraErrorCode
](
../reference/apis/js-apis-camera.md#cameraerrorcode
)
。
```
ts
```
ts
photoOutput
.
on
(
'
error
'
,
(
error
:
BusinessError
)
=>
{
function
onPhotoOutputError
(
photoOutput
:
camera
.
PhotoOutput
):
void
{
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录