Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
0f568881
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
0f568881
编写于
10月 24, 2022
作者:
O
openharmony_ci
提交者:
Gitee
10月 24, 2022
浏览文件
操作
浏览文件
下载
差异文件
!10945 update camera instruction
Merge pull request !10945 from supeng/master
上级
43756968
fa6ac285
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
281 addition
and
299 deletion
+281
-299
zh-cn/application-dev/media/camera.md
zh-cn/application-dev/media/camera.md
+281
-299
zh-cn/application-dev/media/figures/camera_framework_process.jpg
...pplication-dev/media/figures/camera_framework_process.jpg
+0
-0
未找到文件。
zh-cn/application-dev/media/camera.md
浏览文件 @
0f568881
...
@@ -2,7 +2,13 @@
...
@@ -2,7 +2,13 @@
## 场景介绍
## 场景介绍
相机模块支持相机相关基础功能的开发,主要包括预览、拍照、录像等。本文将对相机各种基础功能的开发进行介绍说明。
OpenHarmony相机模块支持相机业务的开发,开发者可以通过已开放的接口实现相机硬件的访问、操作和新功能开发,最常见的操作如:预览、拍照和录像等。开发者也可以通过合适的接口或者接口组合实现闪光灯控制、曝光时间控制、手动对焦和自动对焦控制、变焦控制以及更多的功能。
开发者在调用Camera能力时,需要了解Camera的一些基本概念:
-
**相机静态能力**
:用于描述相机的固有能力的一系列参数,比如朝向、支持的分辨率等信息。
-
**物理相机**
:物理相机就是独立的实体摄像头设备。物理相机ID是用于标志每个物理摄像头的唯一字串。
-
**异步操作**
:为保证UI线程不被阻塞,大部分Camera调用都是异步的。对于每个API均提供了callback函数和Promise函数。
## 开发步骤
## 开发步骤
...
@@ -12,53 +18,63 @@
...
@@ -12,53 +18,63 @@
### 全流程场景
### 全流程场景
包含流程:创建实例、参数设置、会话管理、拍照、录像、释放资源等。
包含流程:
权限申请、
创建实例、参数设置、会话管理、拍照、录像、释放资源等。
Xcomponent创建方法可参考:
[
XComponent创建方法
](
#xcomponent创建方法
)
#### 权限申请
拍照保存接口可参考:
[
图片处理API文档
](
image.md#imagereceiver的使用
)
在使用相机之前,需要申请相机的相关权限,保证应用拥有相机硬件及其他功能权限,应用权限的介绍请参考权限章节,相机涉及权限如下表。
| 权限名称 | 权限属性值 |
| -------- | ------------------------------ |
| 相机权限 | ohos.permission.CAMERA |
| 录音权限 | ohos.permission.MICROPHONE |
| 存储权限 | ohos.permission.WRITE_MEDIA |
| 读取权限 | ohos.permission.READ_MEDIA |
| 位置权限 | ohos.permission.MEDIA_LOCATION |
参考代码如下:
```
typescript
const
PERMISSIONS
:
Array
<
string
>
=
[
'
ohos.permission.CAMERA
'
,
'
ohos.permission.MICROPHONE
'
,
'
ohos.permission.MEDIA_LOCATION
'
,
'
ohos.permission.READ_MEDIA
'
,
'
ohos.permission.WRITE_MEDIA
'
]
function
applyPermission
()
{
console
.
info
(
'
[permission] get permission
'
);
globalThis
.
abilityContext
.
requestPermissionFromUser
(
PERMISSIONS
)
}
```
#### 创建实例
#### 创建实例
```
js
在实现一个相机应用之前必须先创建一个独立的相机设备,然后才能继续相机的其他操作。如果此步骤操作失败,相机可能被占用或无法使用。如果被占用,必须等到相机释放后才能重新获取CameraManager对象。通过getSupportedCameras() 方法,获取当前使用的设备支持的相机列表。相机列表中存储了当前设备拥有的所有相机ID,如果列表不为空,则列表中的每个ID都支持独立创建相机对象;否则,说明正在使用的设备无可用的相机,不能继续后续的操作。相机设备具备预览、拍照、录像、Metadata等输出流,需要通过getSupportedOutputCapability()接口获取各个输出流的具体能力,通过该接口,可以获取当前设备支持的所有输出流能力,分别在CameraOutputCapability中的各个profile字段中,相机设备创建的建议步骤如下:
```
typescript
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
media
from
'
@ohos.multimedia.media
'
import
featureAbility
from
'
@ohos.ability.featureAbility
'
// 创建CameraManager对象
// 创建CameraManager对象
let
cameraManager
let
cameraManager
=
await
camera
.
getCameraManager
(
null
)
await
camera
.
getCameraManager
(
globalThis
.
Context
,
(
err
,
manager
)
=>
{
if
(
!
cameraManager
)
{
if
(
err
)
{
console
.
error
(
'
Failed to get the CameraManager instance
'
);
console
.
error
(
'
Failed to get the CameraManager instance ${err.message}
'
);
}
return
;
}
console
.
log
(
'
Callback returned with the CameraManager instance
'
);
cameraManager
=
manager
})
// 注册回调函数监听相机状态变化,获取状态变化的相机信息
cameraManager
.
on
(
'
cameraStatus
'
,
(
cameraStatusInfo
)
=>
{
console
.
log
(
'
camera :
'
+
cameraStatusInfo
.
camera
.
cameraId
);
console
.
log
(
'
status:
'
+
cameraStatusInfo
.
status
);
})
// 获取相机列表
// 获取相机列表
let
cameraArray
let
cameraArray
=
await
cameraManager
.
getSupportedCameras
()
await
cameraManager
.
getCameras
((
err
,
cameras
)
=>
{
if
(
!
cameraArray
)
{
if
(
err
)
{
console
.
error
(
'
Failed to get the cameras
'
);
console
.
error
(
'
Failed to get the cameras. ${err.message}
'
);
}
return
;
}
console
.
log
(
'
Callback returned with an array of supported cameras:
'
+
cameras
.
length
);
cameraArray
=
cameras
})
for
(
let
cameraIndex
=
0
;
cameraIndex
<
cameraArray
.
length
;
cameraI
ndex
++
)
{
for
(
let
index
=
0
;
index
<
cameraArray
.
length
;
i
ndex
++
)
{
console
.
log
(
'
cameraId :
'
+
cameraArray
[
cameraI
ndex
].
cameraId
)
// 获取相机ID
console
.
log
(
'
cameraId :
'
+
cameraArray
[
i
ndex
].
cameraId
)
// 获取相机ID
console
.
log
(
'
cameraPosition :
'
+
cameraArray
[
cameraI
ndex
].
cameraPosition
)
// 获取相机位置
console
.
log
(
'
cameraPosition :
'
+
cameraArray
[
i
ndex
].
cameraPosition
)
// 获取相机位置
console
.
log
(
'
cameraType :
'
+
cameraArray
[
cameraI
ndex
].
cameraType
)
// 获取相机类型
console
.
log
(
'
cameraType :
'
+
cameraArray
[
i
ndex
].
cameraType
)
// 获取相机类型
console
.
log
(
'
connectionType :
'
+
cameraArray
[
cameraI
ndex
].
connectionType
)
// 获取相机连接类型
console
.
log
(
'
connectionType :
'
+
cameraArray
[
i
ndex
].
connectionType
)
// 获取相机连接类型
}
}
// 创建相机输入流
// 创建相机输入流
...
@@ -68,52 +84,73 @@ await cameraManager.createCameraInput(cameraArray[0].cameraId).then((input) => {
...
@@ -68,52 +84,73 @@ await cameraManager.createCameraInput(cameraArray[0].cameraId).then((input) => {
cameraInput
=
input
cameraInput
=
input
})
})
// 获取相机设备支持的输出流能力
let
cameraOutputCap
=
await
camera
.
getSupportedOutputCapability
(
cameraInput
);
if
(
!
cameraOutputCap
)
{
console
.
error
(
"
outputCapability outputCapability == null || undefined
"
)
}
else
{
console
.
info
(
"
outputCapability:
"
+
JSON
.
stringify
(
cameraOutputCap
));
}
let
previewProfilesArray
=
cameraOutputCap
.
previewProfiles
;
if
(
!
previewProfilesArray
)
{
console
.
error
(
"
createOutput previewProfilesArray == null || undefined
"
)
}
let
photoProfilesArray
=
cameraOutputCap
.
photoProfiles
;
if
(
!
photoProfilesArray
)
{
console
.
error
(
"
createOutput photoProfilesArray == null || undefined
"
)
}
let
videoProfilesArray
=
cameraOutputCap
.
videoProfiles
;
if
(
!
videoProfilesArray
)
{
console
.
error
(
"
createOutput videoProfilesArray == null || undefined
"
)
}
let
metadataObjectTypesArray
=
cameraOutputCap
.
supportedMetadataObjectTypes
;
if
(
!
metadataObjectTypesArray
)
{
console
.
error
(
"
createOutput metadataObjectTypesArray == null || undefined
"
)
}
// 创建预览输出流
// 创建预览输出流
let
previewOutput
let
previewOutput
=
await
camera
.
createPreviewOutput
(
previewProfilesArray
[
0
],
surfaceId
)
camera
.
createPreviewOutput
((
globalThis
.
surfaceId
),
(
err
,
output
)
=>
{
if
(
!
previewOutput
)
{
if
(
err
)
{
console
.
error
(
"
Failed to create the PreviewOutput instance.
"
)
console
.
error
(
'
Failed to create the PreviewOutput instance. ${err.message}
'
);
}
return
;
}
console
.
log
(
'
Callback returned with previewOutput instance
'
);
previewOutput
=
output
});
// 创建ImageReceiver对象,并设置照片参数
// 创建ImageReceiver对象,并设置照片参数
let
imageReceiver
=
await
image
.
createImageReceiver
(
1920
,
1080
,
4
,
8
)
let
imageReceiver
=
await
image
.
createImageReceiver
(
1920
,
1080
,
4
,
8
)
// 获取照片显示SurfaceId
// 获取照片显示SurfaceId
let
photoSurfaceId
=
await
imageReceiver
.
getReceivingSurfaceId
()
let
photoSurfaceId
=
await
imageReceiver
.
getReceivingSurfaceId
()
// 创建拍照输出流
// 创建拍照输出流
let
photoOutput
let
photoOutput
=
await
this
.
camera
.
createPhotoOutput
(
photoProfilesArray
[
0
],
photoSurfaceId
)
camera
.
createPhotoOutput
((
photoSurfaceId
),
(
err
,
output
)
=>
{
if
(
!
photoOutput
)
{
if
(
err
)
{
console
.
error
(
'
Failed to create the PhotoOutput instance.
'
);
console
.
error
(
'
Failed to create the PhotoOutput instance. ${err.message}
'
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned with the PhotoOutput instance.
'
);
photoOutput
=
output
});
// 创建视频录制的参数
// 创建视频录制的参数
let
videoProfile
=
{
audioBitrate
:
48000
,
audioChannels
:
2
,
audioCodec
:
'
audio/mp4a-latm
'
,
audioSampleRate
:
48000
,
fileFormat
:
'
mp4
'
,
videoBitrate
:
48000
,
videoCodec
:
'
video/mp4v-es
'
,
videoFrameWidth
:
640
,
videoFrameHeight
:
480
,
videoFrameRate
:
30
}
let
videoConfig
=
{
let
videoConfig
=
{
audioSourceType
:
1
,
audioSourceType
:
1
,
videoSourceType
:
0
,
videoSourceType
:
1
,
profile
:
videoProfile
,
profile
:
{
url
:
'
file:///data/media/01.mp4
'
,
audioBitrate
:
48000
,
orientationHint
:
0
,
audioChannels
:
2
,
location
:
{
latitude
:
30
,
longitude
:
130
},
audioCodec
:
'
audio/mp4v-es
'
,
audioSampleRate
:
48000
,
durationTime
:
1000
,
fileFormat
:
'
mp4
'
,
videoBitrate
:
48000
,
videoCodec
:
'
video/mp4v-es
'
,
videoFrameWidth
:
640
,
videoFrameHeight
:
480
,
videoFrameRate
:
30
},
url
:
'
file:///data/media/01.mp4
'
,
orientationHint
:
0
,
maxSize
:
100
,
maxDuration
:
500
,
rotation
:
0
}
}
// 创建录像输出流
// 创建录像输出流
...
@@ -129,159 +166,110 @@ await videoRecorder.getInputSurface().then((id) => {
...
@@ -129,159 +166,110 @@ await videoRecorder.getInputSurface().then((id) => {
console
.
log
(
'
getInputSurface called
'
)
console
.
log
(
'
getInputSurface called
'
)
videoSurfaceId
=
id
videoSurfaceId
=
id
})
})
```
videoRecorder详细创建方法可参考:
[
视频录制开发指导
](
./video-recorder.md
)
```
js
// 创建VideoOutput对象
let
videoOutput
camera
.
createVideoOutput
((
surfaceId
),
(
err
,
output
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to create the VideoOutput instance. ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback returned with the VideoOutput instance
'
);
videoOutput
=
output
});
// 创建VideoOutput对象
let
videoOutput
=
camera
.
createVideoOutput
(
videoProfilesArray
[
0
],
videoSurfaceId
)
if
(
!
videoOutput
)
{
console
.
error
(
'
Failed to create the videoOutput instance.
'
);
return
;
}
```
```
预览流、拍照流和录像流的输入均需要提前创建surface,其中预览流为XComponent组件提供的surface,拍照流为ImageReceiver提供的surface,录像流为VideoRecorder的surface。
#### 参数设置
**XComponent**
```
js
```
typescript
// 判断设备是否支持闪光灯
mXComponentController
:
XComponentController
=
new
XComponentController
// 创建XComponentController
let
flashStatus
await
cameraInput
.
hasFlash
().
then
((
status
)
=>
{
console
.
log
(
'
Promise returned with the flash light support status:
'
+
status
);
flashStatus
=
status
})
if
(
flashStatus
)
{
// 判断是否支持自动闪光灯模式
let
flashModeStatus
cameraInput
.
isFlashModeSupported
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
,
(
err
,
status
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to check whether the flash mode is supported. ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback returned with the flash mode support status:
'
+
status
);
flashModeStatus
=
status
})
if
(
flashModeStatus
)
{
// 设置自动闪光灯模式
cameraInput
.
setFlashMode
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
,
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to set the flash mode ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback returned with the successful execution of setFlashMode.
'
);
})
}
}
// 判断是否支持连续自动变焦模式
build
()
{
let
focusModeStatus
Flex
()
{
cameraInput
.
isFocusModeSupported
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
,
(
err
,
status
)
=>
{
XComponent
({
// 创建XComponent
if
(
err
)
{
id
:
''
,
console
.
error
(
'
Failed to check whether the focus mode is supported. ${err.message}
'
);
type
:
'
surface
'
,
return
;
libraryname
:
''
,
controller
:
this
.
mXComponentController
})
.
onload
(()
=>
{
// 设置onload回调
// 设置Surface宽高(1920*1080)
this
.
mXComponentController
.
setXComponentSurfaceSize
({
surfaceWidth
:
1920
,
surfaceHeight
:
1080
})
// 获取Surface ID
globalThis
.
surfaceId
=
mXComponentController
.
getXComponentSurfaceId
()
})
.
width
(
'
1920px
'
)
// 设置XComponent宽度
.
height
(
'
1080px
'
)
// 设置XComponent高度
}
}
console
.
log
(
'
Callback returned with the focus mode support status:
'
+
status
);
focusModeStatus
=
status
})
if
(
focusModeStatus
)
{
// 设置连续自动变焦模式
cameraInput
.
setFocusMode
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
,
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to set the focus mode ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback returned with the successful execution of setFocusMode.
'
);
})
}
}
```
// 获取相机支持的可变焦距比范围
**ImageReceiver**
let
zoomRatioRange
cameraInput
.
getZoomRatioRange
((
err
,
range
)
=>
{
```
typescript
if
(
err
)
{
function
getImageReceiverSurfaceId
()
{
console
.
error
(
'
Failed to get the zoom ratio range. ${err.message}
'
);
var
receiver
=
image
.
createImageReceiver
(
640
,
480
,
4
,
8
)
return
;
console
.
log
(
TAG
+
'
before ImageReceiver check
'
)
if
(
receiver
!==
undefined
)
{
console
.
log
(
'
ImageReceiver is ok
'
)
surfaceId1
=
await
receiver
.
getReceivingSurfaceId
()
console
.
log
(
'
ImageReceived id:
'
+
JSON
.
stringify
(
surfaceId1
))
}
else
{
console
.
log
(
'
ImageReceiver is not ok
'
)
}
}
console
.
log
(
'
Callback returned with zoom ratio range:
'
+
range
.
length
);
}
zoomRatioRange
=
range
```
})
// 设置可变焦距比
**VideoRecorder**
cameraInput
.
setZoomRatio
(
zoomRatioRange
[
0
],
(
err
)
=>
{
if
(
err
)
{
```
typescript
console
.
error
(
'
Failed to set the zoom ratio value ${err.message}
'
);
function
getVideoRecorderSurface
()
{
return
;
await
getFd
(
'
CameraManager.mp4
'
);
mVideoConfig
.
url
=
mFdPath
;
media
.
createVideoRecorder
((
err
,
recorder
)
=>
{
console
.
info
(
'
Entering create video receiver
'
)
mVideoRecorder
=
recorder
console
.
info
(
'
videoRecorder is :
'
+
JSON
.
stringify
(
mVideoRecorder
))
console
.
info
(
'
videoRecorder.prepare called.
'
)
mVideoRecorder
.
prepare
(
mVideoConfig
,
(
err
)
=>
{
console
.
info
(
'
videoRecorder.prepare success.
'
)
mVideoRecorder
.
getInputSurface
((
err
,
id
)
=>
{
console
.
info
(
'
getInputSurface called
'
)
mVideoSurface
=
id
console
.
info
(
'
getInputSurface surfaceId:
'
+
JSON
.
stringify
(
mVideoSurface
))
})
})
})
}
}
console
.
log
(
'
Callback returned with the successful execution of setZoomRatio.
'
);
})
```
```
#### 会话管理
#### 会话管理
##### 创建会话
##### 创建会话
```
js
```
typescript
// 创建Context对象
// 创建Context对象
let
context
=
featureAbility
.
getContext
()
let
context
=
featureAbility
.
getContext
()
//创建会话
//创建会话
let
captureSession
let
captureSession
=
await
camera
.
createCaptureSession
()
await
camera
.
createCaptureSession
((
context
),
(
err
,
session
)
=>
{
if
(
!
captureSession
)
{
if
(
err
)
{
console
.
error
(
'
Failed to create the CaptureSession instance.
'
);
console
.
error
(
'
Failed to create the CaptureSession instance. ${err.message}
'
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned with the CaptureSession instance.
'
+
session
);
console
.
log
(
'
Callback returned with the CaptureSession instance.
'
+
session
);
captureSession
=
session
});
// 开始配置会话
// 开始配置会话
await
captureSession
.
beginConfig
((
err
)
=>
{
await
captureSession
.
beginConfig
()
if
(
err
)
{
console
.
error
(
'
Failed to start the configuration. ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate the begin config success.
'
);
});
// 向会话中添加相机输入流
// 向会话中添加相机输入流
await
captureSession
.
addInput
(
cameraInput
,
(
err
)
=>
{
await
captureSession
.
addInput
(
cameraInput
)
if
(
err
)
{
console
.
error
(
'
Failed to add the CameraInput instance. ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate that the CameraInput instance is added.
'
);
});
// 向会话中添加预览输入流
// 向会话中添加预览输入流
await
captureSession
.
addOutput
(
previewOutput
,
(
err
)
=>
{
await
captureSession
.
addOutput
(
previewOutput
)
if
(
err
)
{
console
.
error
(
'
Failed to add the PreviewOutput instance ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate that the PreviewOutput instance is added.
'
);
});
// 向会话中添加拍照输出流
// 向会话中添加拍照输出流
await
captureSession
.
addOutput
(
photoOutput
,
(
err
)
=>
{
await
captureSession
.
addOutput
(
photoOutput
)
if
(
err
)
{
console
.
error
(
'
Failed to add the PhotoOutput instance ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate that the PhotoOutput instance is added.
'
);
});
// 提交会话配置
// 提交会话配置
await
captureSession
.
commitConfig
((
err
)
=>
{
await
captureSession
.
commitConfig
()
if
(
err
)
{
console
.
error
(
'
Failed to commit the configuration. ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate the commit config success.
'
);
});
// 启动会话
// 启动会话
await
captureSession
.
start
().
then
(()
=>
{
await
captureSession
.
start
().
then
(()
=>
{
...
@@ -291,67 +279,108 @@ await captureSession.start().then(() => {
...
@@ -291,67 +279,108 @@ await captureSession.start().then(() => {
##### 切换会话
##### 切换会话
```
js
```
typescript
// 停止当前会话
// 停止当前会话
await
captureSession
.
stop
((
err
)
=>
{
await
captureSession
.
stop
()
if
(
err
)
{
console
.
error
(
'
Failed to stop the session ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate the session stop success.
'
);
});
// 开始配置会话
// 开始配置会话
await
captureSession
.
beginConfig
((
err
)
=>
{
await
captureSession
.
beginConfig
()
if
(
err
)
{
console
.
error
(
'
Failed to start the configuration. ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate the begin config success.
'
);
});
// 从会话中移除拍照输出流
// 从会话中移除拍照输出流
await
captureSession
.
removeOutput
(
photoOutput
,
(
err
)
=>
{
await
captureSession
.
removeOutput
(
photoOutput
)
if
(
err
)
{
console
.
error
(
'
Failed to remove the PhotoOutput instance. ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate that the PhotoOutput instance is removed.
'
);
});
// 向会话中添加录像输出流
// 向会话中添加录像输出流
await
captureSession
.
addOutput
(
videoOutput
,
(
err
)
=>
{
await
captureSession
.
addOutput
(
videoOutput
)
// 提交会话配置
await
captureSession
.
commitConfig
()
// 启动会话
await
captureSession
.
start
().
then
(()
=>
{
console
.
log
(
'
Promise returned to indicate the session start success.
'
);
})
```
#### 参数设置
```
typescript
// 判断设备是否支持闪光灯
let
flashStatus
=
await
captureSession
.
hasFlash
()
if
(
!
flashStatus
)
{
console
.
error
(
'
Failed to check whether the device has the flash mode.
'
);
}
console
.
log
(
'
Promise returned with the flash light support status:
'
+
flashStatus
);
if
(
flashStatus
)
{
// 判断是否支持自动闪光灯模式
let
flashModeStatus
captureSession
.
isFlashModeSupported
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
,
async
(
err
,
status
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to check whether the flash mode is supported. ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback returned with the flash mode support status:
'
+
status
);
flashModeStatus
=
status
})
if
(
flashModeStatus
)
{
// 设置自动闪光灯模式
captureSession
.
setFlashMode
(
camera
.
FlashMode
.
FLASH_MODE_AUTO
,
async
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to set the flash mode ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback returned with the successful execution of setFlashMode.
'
);
})
}
}
// 判断是否支持连续自动变焦模式
let
focusModeStatus
captureSession
.
isFocusModeSupported
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
,
async
(
err
,
status
)
=>
{
if
(
err
)
{
if
(
err
)
{
console
.
error
(
'
Failed to
add the VideoOutput instance
${err.message}
'
);
console
.
error
(
'
Failed to
check whether the focus mode is supported.
${err.message}
'
);
return
;
return
;
}
}
console
.
log
(
'
Callback invoked to indicate that the VideoOutput instance is added.
'
);
console
.
log
(
'
Callback returned with the focus mode support status:
'
+
status
);
});
focusModeStatus
=
status
})
if
(
focusModeStatus
)
{
// 设置连续自动变焦模式
captureSession
.
setFocusMode
(
camera
.
FocusMode
.
FOCUS_MODE_CONTINUOUS_AUTO
,
async
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed to set the focus mode ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback returned with the successful execution of setFocusMode.
'
);
})
}
// 提交会话配置
// 获取相机支持的可变焦距比范围
await
captureSession
.
commitConfig
((
err
)
=>
{
let
zoomRatioRange
=
await
captureSession
.
getZoomRatioRange
()
if
(
!
zoomRatioRange
)
{
console
.
error
(
'
Failed to get the zoom ratio range.
'
);
return
;
}
// 设置可变焦距比
captureSession
.
setZoomRatio
(
zoomRatioRange
[
0
],
async
(
err
)
=>
{
if
(
err
)
{
if
(
err
)
{
console
.
error
(
'
Failed to
commit the configuration.
${err.message}
'
);
console
.
error
(
'
Failed to
set the zoom ratio value
${err.message}
'
);
return
;
return
;
}
}
console
.
log
(
'
Callback invoked to indicate the commit config success.
'
);
console
.
log
(
'
Callback returned with the successful execution of setZoomRatio.
'
);
});
// 启动会话
await
captureSession
.
start
().
then
(()
=>
{
console
.
log
(
'
Promise returned to indicate the session start success.
'
);
})
})
```
```
#### 拍照
#### 拍照
```
js
```
typescript
let
settings
=
{
let
settings
=
{
quality
:
camera
.
QualityLevel
.
QUALITY_LEVEL_HIGH
,
// 设置图片质量高
quality
:
camera
.
QualityLevel
.
QUALITY_LEVEL_HIGH
,
// 设置图片质量高
rotation
:
camera
.
ImageRotation
.
ROTATION_0
// 设置图片旋转角度0
rotation
:
camera
.
ImageRotation
.
ROTATION_0
// 设置图片旋转角度0
}
}
// 使用当前拍照设置进行拍照
// 使用当前拍照设置进行拍照
photoOutput
.
capture
(
settings
,
(
err
)
=>
{
photoOutput
.
capture
(
settings
,
async
(
err
)
=>
{
if
(
err
)
{
if
(
err
)
{
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
console
.
error
(
'
Failed to capture the photo ${err.message}
'
);
return
;
return
;
...
@@ -362,9 +391,9 @@ photoOutput.capture(settings, (err) => {
...
@@ -362,9 +391,9 @@ photoOutput.capture(settings, (err) => {
#### 录像
#### 录像
```
js
```
typescript
// 启动录像输出流
// 启动录像输出流
videoOutput
.
start
((
err
)
=>
{
videoOutput
.
start
(
async
(
err
)
=>
{
if
(
err
)
{
if
(
err
)
{
console
.
error
(
'
Failed to start the video output ${err.message}
'
);
console
.
error
(
'
Failed to start the video output ${err.message}
'
);
return
;
return
;
...
@@ -392,81 +421,34 @@ await videoOutput.stop((err) => {
...
@@ -392,81 +421,34 @@ await videoOutput.stop((err) => {
});
});
```
```
拍照保存接口可参考:
[
图片处理API文档
](
image.md#imagereceiver的使用
)
#### 释放资源
#### 释放资源
```
js
```
typescript
// 停止当前会话
// 停止当前会话
await
captureSession
.
stop
((
err
)
=>
{
await
captureSession
.
stop
()
if
(
err
)
{
console
.
error
(
'
Failed to stop the session ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate the session stop success.
'
);
});
// 释放相机输入流
// 释放相机输入流
await
cameraInput
.
release
((
err
)
=>
{
await
cameraInput
.
release
()
if
(
err
)
{
console
.
error
(
'
Failed to release the CameraInput instance ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate that the CameraInput instance is released successfully.
'
);
});
// 释放预览输出流
// 释放预览输出流
await
previewOutput
.
release
((
err
)
=>
{
await
previewOutput
.
release
()
if
(
err
)
{
console
.
error
(
'
Failed to release the PreviewOutput instance ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate that the PreviewOutput instance is released successfully.
'
);
});
// 释放拍照输出流
// 释放拍照输出流
await
photoOutput
.
release
((
err
)
=>
{
await
photoOutput
.
release
()
if
(
err
)
{
console
.
error
(
'
Failed to release the PhotoOutput instance ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate that the PhotoOutput instance is released successfully.
'
);
});
// 释放录像输出流
// 释放录像输出流
await
videoOutput
.
release
((
err
)
=>
{
await
videoOutput
.
release
()
if
(
err
)
{
console
.
error
(
'
Failed to release the VideoOutput instance ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate that the VideoOutput instance is released successfully.
'
);
});
// 释放会话
// 释放会话
await
captureSession
.
release
((
err
)
=>
{
await
captureSession
.
release
()
if
(
err
)
{
console
.
error
(
'
Failed to release the CaptureSession instance ${err.message}
'
);
return
;
}
console
.
log
(
'
Callback invoked to indicate that the CaptureSession instance is released successfully.
'
);
});
```
#### XComponent创建方法
// 会话置空
预览画面显示需要获取SurfaceId
captureSession
=
null
```
```
js
## 流程图
mXComponentController
:
XComponentController
=
new
XComponentController
// 创建XComponentController
build
()
{
应用使用相机的流程示意图如下
Flex
()
{
![
camera_framework process
](
figures/camera_framework_process.jpg
)
XComponent
({
// 创建XComponent
\ No newline at end of file
id
:
''
,
type
:
'
surface
'
,
libraryname
:
''
,
controller
:
this
.
mXComponentController
})
.
onload
(()
=>
{
// 设置onload回调
// 设置Surface宽高(1920*1080)
this
.
mXComponentController
.
setXComponentSurfaceSize
({
surfaceWidth
:
1920
,
surfaceHeight
:
1080
})
// 获取Surface ID
globalThis
.
surfaceId
=
mXComponentController
.
getXComponentSurfaceId
()
})
.
width
(
'
1920px
'
)
// 设置XComponent宽度
.
height
(
'
1080px
'
)
// 设置XComponent高度
}
}
```
\ No newline at end of file
zh-cn/application-dev/media/figures/camera_framework_process.jpg
0 → 100644
浏览文件 @
0f568881
1.9 MB
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录