未验证 提交 6a75f3ad 编写于 作者: O openharmony_ci 提交者: Gitee

!23406 cameraFramework ArkTs整改

Merge pull request !23406 from 章超123/monthly_20230815
......@@ -13,10 +13,10 @@
```
2. 通过getCameraManager()方法,获取cameraManager对象。
[各类Context的获取方式](../application-models/application-context-stage.md)
```ts
let cameraManager: camera.CameraManager;
let context: Context = getContext(this); // [各类Context的获取方式](../application-models/application-context-stage.md)
let context: Context = getContext(this);
cameraManager = camera.getCameraManager(context);
```
......@@ -29,15 +29,15 @@
```ts
let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
if (cameraArray != undefined && cameraArray.length <= 0) {
console.error("cameraManager.getSupportedCameras error");
return;
console.error("cameraManager.getSupportedCameras error");
return;
}
for (let index = 0; index < cameraArray.length; index++) {
console.info('cameraId : ' + cameraArray[index].cameraId); // 获取相机ID
console.info('cameraPosition : ' + cameraArray[index].cameraPosition); // 获取相机位置
console.info('cameraType : ' + cameraArray[index].cameraType); // 获取相机类型
console.info('connectionType : ' + cameraArray[index].connectionType); // 获取相机连接类型
console.info('cameraId : ' + cameraArray[index].cameraId); // 获取相机ID
console.info('cameraPosition : ' + cameraArray[index].cameraPosition); // 获取相机位置
console.info('cameraType : ' + cameraArray[index].cameraType); // 获取相机类型
console.info('connectionType : ' + cameraArray[index].connectionType); // 获取相机连接类型
}
```
......@@ -47,22 +47,23 @@
// 创建相机输入流
let cameraInput: camera.CameraInput;
try {
cameraInput = cameraManager.createCameraInput(cameraArray[0]);
cameraInput = cameraManager.createCameraInput(cameraArray[0]);
} catch (error) {
console.error('Failed to createCameraInput errorCode = ' + error.code);
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}`);
console.info(`Camera input error code: ${error.code}`);
});
// 打开相机
await cameraInput.open();
// 获取相机设备支持的输出流能力
let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraArray[0]);
if (!cameraOutputCapability) {
console.error("cameraManager.getSupportedOutputCapability error");
return;
console.error("cameraManager.getSupportedOutputCapability error");
return;
}
console.info("outputCapability: " + JSON.stringify(cameraOutputCapability));
```
......
......@@ -16,8 +16,8 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
try {
metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes);
} catch (error) {
// 失败返回错误码error.code并处理
console.info(error.code);
let err = error as BusinessError;
console.info('Failed to createMetadataOutput, error code: '+ err.code);
}
```
......@@ -26,8 +26,8 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
```ts
metadataOutput.start().then(() => {
console.info('Callback returned with metadataOutput started.');
}).catch((err) => {
console.info('Failed to metadataOutput start '+ err.code);
}).catch((err: BusinessError) => {
console.info('Failed to metadataOutput start, error code: '+ err.code);
});
```
......@@ -36,7 +36,7 @@ Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递
```ts
metadataOutput.stop().then(() => {
console.info('Callback returned with metadataOutput stopped.');
}).catch((err) => {
}).catch((err: BusinessError) => {
console.info('Failed to metadataOutput stop '+ err.code);
});
```
......
# 人像模式拍照
# 人像模式拍照实现方案
## 开发流程
......@@ -9,7 +9,7 @@
![portraitgraphing Development Process](figures/portrait-capture-development-process.png)
## 完整示例
[各类Context的获取方式](../application-models/application-context-stage.md)
```ts
import camera from '@ohos.multimedia.camera';
import image from '@ohos.multimedia.image';
......@@ -17,17 +17,17 @@ import media from '@ohos.multimedia.media';
// 创建CameraManager对象
let context: Context = getContext(this); // [各类Context的获取方式](../application-models/application-context-stage.md)
let context: Context = getContext(this);
let cameraManager: camera.CameraManager = camera.getCameraManager(context);
if (!cameraManager) {
console.error("camera.getCameraManager error");
return;
console.error("camera.getCameraManager error");
return;
}
// 创建ModeManager对象
let modeManager: camera.ModeManager = camera.getModeManager(context);
if (!cameraManager) {
console.error("camera.getModeManager error");
return;
console.error("camera.getModeManager error");
return;
}
// 监听相机状态变化
cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => {
......@@ -37,34 +37,35 @@ cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.C
// 获取相机列表
let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
if (cameraArray.length <= 0) {
console.error("cameraManager.getSupportedCameras error");
return;
console.error("cameraManager.getSupportedCameras error");
return;
}
for (let index = 0; index < cameraArray.length; index++) {
console.info('cameraId : ' + cameraArray[index].cameraId); // 获取相机ID
console.info('cameraPosition : ' + cameraArray[index].cameraPosition); // 获取相机位置
console.info('cameraType : ' + cameraArray[index].cameraType); // 获取相机类型
console.info('connectionType : ' + cameraArray[index].connectionType); // 获取相机连接类型
console.info('cameraId : ' + cameraArray[index].cameraId); // 获取相机ID
console.info('cameraPosition : ' + cameraArray[index].cameraPosition); // 获取相机位置
console.info('cameraType : ' + cameraArray[index].cameraType); // 获取相机类型
console.info('connectionType : ' + cameraArray[index].connectionType); // 获取相机连接类型
}
// 获取模式列表
let cameraModeArray: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraArray[0]);
if (cameraModeArray.length <= 0) {
console.error("modeManager.getSupportedModes error");
return;
console.error("modeManager.getSupportedModes error");
return;
}
// 创建相机输入流
let cameraInput: camera.CameraInput;
try {
cameraInput = cameraManager.createCameraInput(cameraArray[0]);
cameraInput = cameraManager.createCameraInput(cameraArray[0]);
} catch (error) {
console.error('Failed to createCameraInput errorCode = ' + error.code);
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}`);
console.info(`Camera input error code: ${error.code}`);
})
// 打开相机
......@@ -73,82 +74,89 @@ await cameraInput.open();
// 获取当前模式相机设备支持的输出流能力
let cameraOutputCap: camera.CameraOutputCapability = modeManager.getSupportedOutputCapability(cameraArray[0], cameraModeArray[0]);
if (!cameraOutputCap) {
console.error("modeManager.getSupportedOutputCapability error")
return;
console.error("modeManager.getSupportedOutputCapability error");
return;
}
console.info("outputCapability: " + JSON.stringify(cameraOutputCap));
let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;
if (!previewProfilesArray) {
console.error("createOutput previewProfilesArray == null || undefined")
console.error("createOutput previewProfilesArray == null || undefined");
}
let photoProfilesArray: Array<camera.Profile> = cameraOutputCap.photoProfiles;
if (!photoProfilesArray) {
console.error("createOutput photoProfilesArray == null || undefined")
console.error("createOutput photoProfilesArray == null || undefined");
}
// 创建预览输出流,其中参数 surfaceId 参考上文 XComponent 组件,预览流为XComponent组件提供的surface
let previewOutput: camera.PreviewOutput;
try {
previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId);
previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId);
} catch (error) {
console.error("Failed to create the PreviewOutput instance.");
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}`);
console.info(`Preview output error code: ${error.code}`);
})
// 创建ImageReceiver对象,并设置照片参数:分辨率大小是根据前面 photoProfilesArray 获取的当前设备所支持的拍照分辨率大小去设置
let imageReceiver: image.ImageReceiver = await image.createImageReceiver(1920, 1080, 4, 8)
let imageReceiver: image.ImageReceiver = image.createImageReceiver(1920, 1080, 4, 8);
// 获取照片显示SurfaceId
let photoSurfaceId: string = await imageReceiver.getReceivingSurfaceId()
let photoSurfaceId: string = await imageReceiver.getReceivingSurfaceId();
// 创建拍照输出流
let photoOutput: camera.PhotoOutput;
try {
photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0], photoSurfaceId);
photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0], photoSurfaceId);
} catch (error) {
console.error('Failed to createPhotoOutput errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to createPhotoOutput errorCode = ' + err.code);
}
//创建portrait会话
let portraitSession: camera.CaptureSession;
try {
portraitSession = modeManager.createCaptureSession(cameraModeArray[0]);
portraitSession = modeManager.createCaptureSession(cameraModeArray[0]);
} catch (error) {
console.error('Failed to create the CaptureSession instance. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to create the CaptureSession instance. errorCode = ' + err.code);
}
// 监听portraitSession错误信息
portraitSession.on('error', (error: BusinessError) => {
console.info(`Capture session error code: ${error.code}`);
console.info(`Capture session error code: ${error.code}`);
});
// 开始配置会话
try {
portraitSession.beginConfig();
portraitSession.beginConfig();
} catch (error) {
console.error('Failed to beginConfig. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to beginConfig. errorCode = ' + err.code);
}
// 向会话中添加相机输入流
try {
portraitSession.addInput(cameraInput);
portraitSession.addInput(cameraInput);
} catch (error) {
console.error('Failed to addInput. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to addInput. errorCode = ' + err.code);
}
// 向会话中添加预览输出流
try {
portraitSession.addOutput(previewOutput);
portraitSession.addOutput(previewOutput);
} catch (error) {
console.error('Failed to addOutput(previewOutput). errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to addOutput(previewOutput). errorCode = ' + err.code);
}
// 向会话中添加拍照输出流
try {
portraitSession.addOutput(photoOutput);
portraitSession.addOutput(photoOutput);
} catch (error) {
console.error('Failed to addOutput(photoOutput). errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to addOutput(photoOutput). errorCode = ' + err.code);
}
// 提交会话配置
......@@ -156,86 +164,97 @@ await portraitSession.commitConfig();
// 启动会话
await portraitSession.start().then(() => {
console.info('Promise returned to indicate the session start success.');
console.info('Promise returned to indicate the session start success.');
})
// 获取支持的美颜类型
let beautyTypes: Array<camera.BeautyType>;
try {
beautyTypes = portraitSession.getSupportedBeautyTypes();
beautyTypes = portraitSession.getSupportedBeautyTypes();
} catch (error) {
console.error('Failed to get the beauty types. errorCode = ' + error.code);
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]);
beautyRanges = portraitSession.getSupportedBeautyRanges(beautyTypes[0]);
} catch (error) {
console.error('Failed to get the beauty types ranges. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to get the beauty types ranges. errorCode = ' + err.code);
}
// 设置美颜类型及对应的美颜强度
try {
portraitSession.setBeauty(beautyTypes[0], beautyRanges[0]);
portraitSession.setBeauty(beautyTypes[0], beautyRanges[0]);
} catch (error) {
console.error('Failed to set the beauty type value. errorCode = ' + error.code);
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]);
beautyLevel = portraitSession.getBeauty(beautyTypes[0]);
} catch (error) {
console.error('Failed to get the beauty type value. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to get the beauty type value. errorCode = ' + err.code);
}
// 获取支持的滤镜类型
let filterTypes: Array<camera.FilterType>;
try {
filterTypes = portraitSession.getSupportedFilters();
filterTypes = portraitSession.getSupportedFilters();
} catch (error) {
console.error('Failed to get the filter types. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to get the filter types. errorCode = ' + err.code);
}
// 设置滤镜类型
try {
portraitSession.setFilter(filterTypes[0]);
portraitSession.setFilter(filterTypes[0]);
} catch (error) {
console.error('Failed to set the filter type value. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to set the filter type value. errorCode = ' + err.code);
}
// 获取已经设置的滤镜类型
let filter: number;
try {
filter = portraitSession.getFilter();
filter = portraitSession.getFilter();
} catch (error) {
console.error('Failed to get the filter type value. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to get the filter type value. errorCode = ' + err.code);
}
// 获取支持的虚化类型
let portraitTypes: Array<camera.PortraitEffect>;
try {
portraitTypes = portraitSession.getSupportedPortraitEffects();
portraitTypes = portraitSession.getSupportedPortraitEffects();
} catch (error) {
console.error('Failed to get the portrait effects types. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to get the portrait effects types. errorCode = ' + err.code);
}
// 设置虚化类型
try {
portraitSession.setPortraitEffect(portraitTypes[0]);
portraitSession.setPortraitEffect(portraitTypes[0]);
} catch (error) {
console.error('Failed to set the portrait effects value. errorCode = ' + error.code);
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();
effect = portraitSession.getPortraitEffect();
} catch (error) {
console.error('Failed to get the portrait effects value. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to get the portrait effects value. errorCode = ' + err.code);
}
let captureSettings: camera.PhotoCaptureSetting;
// 使用当前拍照设置进行拍照
photoOutput.capture(settings, async (err: BusinessError) => {
if (err) {
console.error('Failed to capture the photo ${err.message}');
return;
}
console.info('Callback invoked to indicate the photo capture request success.');
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();
......
......@@ -31,19 +31,21 @@
![](figures/deferred-surface-sequence-diagram.png)
[各类Context的获取方式](../application-models/application-context-stage.md)
```js
import camera from '@ohos.multimedia.camera';
function async preview(context: Context, cameraInfo: camera.Device, previewProfile: camera.Profile, photoProfile: camera.Profile, surfaceId: string): Promise<void> {
async function Preview(context: Context, cameraInfo: camera.CameraDevice, previewProfile: camera.Profile, photoProfile: camera.Profile, surfaceId: string): Promise<void> {
const cameraManager: camera.CameraManager = camera.getCameraManager(context);
const cameraInput camera.CameraInput = await cameraManager.createCameraInput(cameraInfo);
const cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameraInfo);
const previewOutput: camera.PreviewOutput = await cameraManager.createDeferredPreviewOutput(previewProfile);
const photoOutput: camera.PhotoOutput = await cameraManager.createPhotoOutput(photoProfile);
const session: camera.CaptureSession = await mCameraManager.createCaptureSession();
await session.beginConfig();
await session.addInput(cameraInput);
await session.addOutput(previewOutput);
await session.addOutput(photoOutput);
const photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId);
const session: camera.CaptureSession = cameraManager.createCaptureSession();
session.beginConfig();
session.addInput(cameraInput);
session.addOutput(previewOutput);
session.addOutput(photoOutput);
await session.commitConfig();
await session.start();
await previewOutput.addDeferredSurface(surfaceId);
......@@ -79,30 +81,31 @@ function async preview(context: Context, cameraInfo: camera.Device, previewProfi
![](figures/quick-thumbnail-sequence-diagram.png)
[各类Context的获取方式](../application-models/application-context-stage.md)
```js
import camera from '@ohos.multimedia.camera';
let context: Context = getContext(this); // [各类Context的获取方式](../application-models/application-context-stage.md)
let context: Context = getContext(this);
let cameraManager: camera.CameraManager = camera.getCameraManager(context);
let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
// 创建CaptureSession实例
let captureSession: camera.CaptureSession = await cameraManager.createCaptureSession();
let captureSession: camera.CaptureSession = cameraManager.createCaptureSession();
// 开始配置会话
await captureSession.beginConfig();
captureSession.beginConfig();
// 把CameraInput加入到会话
let cameraInput: camera.CameraInput = await cameraManager.createCameraInput(cameras[0]);
await cameraInput.open();
await captureSession.addInput(cameraInput);
let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]);
cameraInput.open();
captureSession.addInput(cameraInput);
// 把PhotoOutPut加入到会话
let photoOutPut: camera.PhotoOutput = await cameraManager.createPhotoOutput(photoProfile, surfaceId);
await captureSession.addOutput(photoOutPut);
let photoOutPut: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId);
captureSession.addOutput(photoOutPut);
let isSupported: boolean = photoOutPut.isQuickThumbnailSupported();
if (isSupported) {
// 使能快速缩略图
photoOutPut.enableQuickThumbnail(true);
photoOutPut.on('quickThumbnail', (err: BusinessError, pixelmap: image.PixelMap) => {
if (err || pixelmap === undefined) {
console.error('photoOutPut on thumbnail failed');
return;
console.error('photoOutPut on thumbnail failed');
return;
}
// 显示或保存pixelmap
showOrSavePicture(pixelmap);
......@@ -135,16 +138,19 @@ if (isSupported) {
![](figures/prelaunch-sequence-diagram.png)
[各类Context的获取方式](../application-models/application-context-stage.md)
- **桌面应用**
```js
import camera from '@ohos.multimedia.camera';
let cameraManager: camera.CameraManager = camera.getCameraManager(globalThis.abilityContext);
let context: Context = getContext(this);
let cameraManager: camera.CameraManager = camera.getCameraManager(context);
try {
cameraManager.prelaunch();
} catch (error) {
console.error(`catch error: Code: ${error.code}, message: ${error.message}`);
let err = error as BusinessError;
console.error(`catch error: Code: ${err.code}, message: ${err.message}`);
}
```
......@@ -156,14 +162,15 @@ if (isSupported) {
```js
import camera from '@ohos.multimedia.camera';
cameraManager: camera.CameraManager = camera.getCameraManager(globalThis.abilityContext);
let context: Context = getContext(this);
let cameraManager: camera.CameraManager = camera.getCameraManager(context);
let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
if(cameraManager.isPrelaunchSupported(cameras[0])) {
try {
cameraManager.setPrelaunchConfig({cameraDevice: cameras[0]});
} catch (error) {
console.error(`catch error: Code: ${error.code}, message: ${error.message}`);
let err = error as BusinessError;
console.error(`catch error: Code: ${err.code}, message: ${err.message}`);
}
}
```
......@@ -7,11 +7,9 @@
详细的API说明请参考[Camera API参考](../reference/apis/js-apis-camera.md)
1. 创建Surface。
XComponent组件为预览流提供的Surface,而XComponent的能力由UI提供,相关介绍可参考[XComponent组件参考](../reference/arkui-ts/ts-basic-components-xcomponent.md)
**注**:预览流与录像输出流的分辨率的宽高比要保持一致,如示例代码中宽高比为1920:1080 = 16:9,则需要预览流中的分辨率的宽高比也为16:9,如分辨率选择640:360,或960:540,或1920:1080,以此类推
```ts
// 创建XComponentController
mXComponentController: XComponentController = new XComponentController;
......@@ -26,7 +24,6 @@
})
.onLoad(() => {
// 设置Surface宽高(1920*1080),预览尺寸设置参考前面 previewProfilesArray 获取的当前设备所支持的预览分辨率大小去设置
// 预览流与录像输出流的分辨率的宽高比要保持一致
this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:1920,surfaceHeight:1080});
// 获取Surface ID
globalThis.surfaceId = this.mXComponentController.getXComponentSurfaceId();
......@@ -36,22 +33,22 @@
}
}
```
2. 通过CameraOutputCapability类中的previewProfiles()方法获取当前设备支持的预览能力,返回previewProfilesArray数组 。通过createPreviewOutput()方法创建预览输出流,其中,createPreviewOutput()方法中的两个参数分别是previewProfilesArray数组中的第一项和步骤一中获取的surfaceId。
```ts
let previewProfilesArray: Array<camera.Profile> = cameraOutputCapability.previewProfiles;
let previewOutput: camera.PreviewOutput;
try {
previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId);
}
catch (error) {
console.error("Failed to create the PreviewOutput instance." + error);
} catch (error) {
let err = error as BusinessError;
console.error("Failed to create the PreviewOutput instance. error code: " + err.code);
}
```
3. 使能。通过start()方法输出预览流,接口调用失败会返回相应错误码,错误码类型参见[CameraErrorCode](../reference/apis/js-apis-camera.md#cameraerrorcode)
```ts
previewOutput.start().then(() => {
console.info('Callback returned with previewOutput started.');
......@@ -66,7 +63,7 @@
在相机应用开发过程中,可以随时监听预览输出流状态,包括预览流启动、预览流结束、预览流输出错误。
- 通过注册固定的frameStart回调函数获取监听预览启动结果,previewOutput创建成功时即可监听,预览第一次曝光时触发,有该事件返回结果则认为预览流已启动。
```ts
previewOutput.on('frameStart', () => {
console.info('Preview frame started');
......@@ -74,7 +71,7 @@
```
- 通过注册固定的frameEnd回调函数获取监听预览结束结果,previewOutput创建成功时即可监听,预览完成最后一帧时触发,有该事件返回结果则认为预览流已结束。
```ts
previewOutput.on('frameEnd', () => {
console.info('Preview frame ended');
......@@ -82,7 +79,7 @@
```
- 通过注册固定的error回调函数获取监听预览输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见[CameraErrorCode](../reference/apis/js-apis-camera.md#cameraerrorcode)
```ts
previewOutput.on('error', (previewOutputError: BusinessError) => {
console.info(`Preview output error code: ${previewOutputError.code}`);
......
......@@ -8,18 +8,18 @@
## 完整示例
[各类Context的获取方式](../application-models/application-context-stage.md)
```ts
import camera from '@ohos.multimedia.camera';
import media from '@ohos.multimedia.media';
// 创建CameraManager对象
let context: Context = getContext(this); // [各类Context的获取方式](../application-models/application-context-stage.md)
let context: Context = getContext(this);
let cameraManager: camera.CameraManager = camera.getCameraManager(context);
if (!cameraManager) {
console.error("camera.getCameraManager error");
return;
}
}
// 监听相机状态变化
cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => {
......@@ -27,6 +27,12 @@ cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.C
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) {
......@@ -38,17 +44,17 @@ 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) {
console.error("createOutput videoProfilesArray == null || undefined");
}
}
let metadataObjectTypesArray: Array<camera.MetadataObjectType> = cameraOutputCap.supportedMetadataObjectTypes;
if (!metadataObjectTypesArray) {
......@@ -68,7 +74,7 @@ let AVRecorderProfile = {
videoFrameHeight : 480,
videoFrameRate : 30
};
let AVRecorderConfig = {
let aVRecorderConfig = {
audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC,
videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV,
profile : AVRecorderProfile,
......@@ -87,7 +93,7 @@ media.createAVRecorder((error: BusinessError, recorder: media.AVRecorder) => {
}
});
avRecorder.prepare(AVRecorderConfig: media.AVRecorderConfig, (err: BusinessError) => {
avRecorder.prepare(aVRecorderConfig, (err: BusinessError) => {
if (err == null) {
console.log('prepare success');
} else {
......@@ -138,13 +144,6 @@ try {
console.error('Failed to beginConfig. errorCode = ' + error.code);
}
// 获取相机列表
let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
if (cameraArray.length <= 0) {
console.error("cameraManager.getSupportedCameras error")
return;
}
// 创建相机输入流
let cameraInput: camera.CameraInput;
try {
......
......@@ -7,7 +7,7 @@
详细的API说明请参考[Camera API参考](../reference/apis/js-apis-camera.md)
1. 导入media模块。创建拍照输出流的SurfaceId以及拍照输出的数据,都需要用到系统提供的[media接口](../reference/apis/js-apis-media.md)能力,导入media接口的方法如下。
```ts
import media from '@ohos.multimedia.media';
```
......@@ -15,7 +15,7 @@
2. 创建Surface。
系统提供的media接口可以创建一个录像AVRecorder实例,通过该实例的getInputSurface方法获取SurfaceId,与录像输出流做关联,处理录像输出流输出的数据。
```ts
let avRecorder: media.AVRecorder;
media.createAVRecorder((error: BusinessError, recorder: media.AVRecorder) => {
......@@ -26,8 +26,9 @@
console.info(`createAVRecorder fail, error:${error}`);
}
});
// AVRecorderConfig可参考下一章节
avRecorder.prepare(AVRecorderConfig: media.AVRecorderConfig, (err: BusinessError) => {
// aVRecorderConfig可参考下一章节
let aVRecorderConfig: media.AVRecorderConfig;
avRecorder.prepare(aVRecorderConfig, (err: BusinessError) => {
if (err == null) {
console.log('prepare success');
} else {
......@@ -35,28 +36,26 @@
}
});
let videoSurfaceId: string = null;
let videoSurfaceId: string = null;
avRecorder.getInputSurface().then((surfaceId: string) => {
console.info('getInputSurface success');
videoSurfaceId = surfaceId;
}).catch((err) => {
console.info('getInputSurface failed and catch error is ' + err.message);
videoSurfaceId = surfaceId;
}).catch((err: BusinessError) => {
console.info('getInputSurface failed and catch error is ' + err.message);
});
```
3. 创建录像输出流。
通过CameraOutputCapability类中的videoProfiles,可获取当前设备支持的录像输出流。然后,定义创建录像的参数,通过createVideoOutput方法创建录像输出流。
**注**:预览流与录像输出流的分辨率的宽高比要保持一致,如示例代码中宽高比为640:480 = 4:3,则需要预览流中的分辨率的宽高比也为4:3,如分辨率选择640:480,或960:720,或1440:1080,以此类推
```ts
let videoProfilesArray: Array<camera.VideoProfile> = cameraOutputCapability.videoProfiles;
if (!videoProfilesArray) {
console.error("createOutput videoProfilesArray == null || undefined");
}
// 创建视频录制的参数,预览流与录像输出流的分辨率的宽(videoFrameWidth)高(videoFrameHeight)比要保持一致
// 创建视频录制的参数
let videoConfig = {
videoSourceType: media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV,
profile: {
......@@ -87,10 +86,11 @@
try {
videoOutput = cameraManager.createVideoOutput(videoProfilesArray[0], videoSurfaceId);
} catch (error) {
console.error('Failed to create the videoOutput instance. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to create the videoOutput instance. errorCode = ' + err.code);
}
```
4. 开始录像。
先通过videoOutput的start方法启动录像输出流,再通过avRecorder的start方法开始录像。
......@@ -110,9 +110,9 @@
```
5. 停止录像。
先通过avRecorder的stop方法停止录像,再通过videoOutput的stop方法停止录像输出流。
```ts
videoRecorder.stop().then(() => {
console.info('stop success');
......@@ -133,7 +133,7 @@
在相机应用开发过程中,可以随时监听录像输出流状态,包括录像开始、录像结束、录像流输出的错误。
- 通过注册固定的frameStart回调函数获取监听录像开始结果,videoOutput创建成功时即可监听,录像第一次曝光时触发,有该事件返回结果则认为录像开始。
```ts
videoOutput.on('frameStart', () => {
console.info('Video frame started');
......@@ -141,7 +141,7 @@
```
- 通过注册固定的frameEnd回调函数获取监听录像结束结果,videoOutput创建成功时即可监听,录像完成最后一帧时触发,有该事件返回结果则认为录像流已结束。
```ts
videoOutput.on('frameEnd', () => {
console.info('Video frame ended');
......@@ -149,7 +149,7 @@
```
- 通过注册固定的error回调函数获取监听录像输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见[CameraErrorCode](../reference/apis/js-apis-camera.md#cameraerrorcode)
```ts
videoOutput.on('error', (error: BusinessError) => {
console.info(`Video output error code: ${error.code}`);
......
......@@ -22,7 +22,8 @@
try {
captureSession = cameraManager.createCaptureSession();
} catch (error) {
console.error('Failed to create the CaptureSession instance. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to create the CaptureSession instance. errorCode = ' + err.code);
}
```
......@@ -32,7 +33,8 @@
try {
captureSession.beginConfig();
} catch (error) {
console.error('Failed to beginConfig. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to beginConfig. errorCode = ' + err.code);
}
```
......@@ -44,17 +46,20 @@
try {
captureSession.addInput(cameraInput);
} catch (error) {
console.error('Failed to addInput. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to addInput. errorCode = ' + err.code);
}
try {
captureSession.addOutput(previewOutput);
} catch (error) {
console.error('Failed to addOutput(previewOutput). errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to addOutput(previewOutput). errorCode = ' + err.code);
}
try {
captureSession.addOutput(photoOutput);
} catch (error) {
console.error('Failed to addOutput(photoOutput). errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to addOutput(photoOutput). errorCode = ' + err.code);
}
await captureSession.commitConfig();
await captureSession.start().then(() => {
......@@ -69,18 +74,21 @@
try {
captureSession.beginConfig();
} catch (error) {
console.error('Failed to beginConfig. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to beginConfig. errorCode = ' + err.code);
}
// 从会话中移除拍照输出流
try {
captureSession.removeOutput(photoOutput);
} catch (error) {
console.error('Failed to removeOutput(photoOutput). errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to removeOutput(photoOutput). errorCode = ' + err.code);
}
// 向会话中添加视频输出流
try {
captureSession.addOutput(videoOutput);
} catch (error) {
console.error('Failed to addOutput(videoOutput). errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to addOutput(videoOutput). errorCode = ' + err.code);
}
```
......@@ -7,14 +7,14 @@
![Photographing Development Process](figures/photographing-development-process.png)
## 完整示例
[各类Context的获取方式](../application-models/application-context-stage.md)
```ts
import camera from '@ohos.multimedia.camera';
import image from '@ohos.multimedia.image';
import media from '@ohos.multimedia.media';
// 创建CameraManager对象
let context: Context = getContext(this); // [各类Context的获取方式](../application-models/application-context-stage.md)
let context: Context = getContext(this);
let cameraManager: camera.CameraManager = camera.getCameraManager(context);
if (!cameraManager) {
console.error("camera.getCameraManager error");
......@@ -45,7 +45,8 @@ let cameraInput: camera.CameraInput;
try {
cameraInput = cameraManager.createCameraInput(cameraArray[0]);
} catch (error) {
console.error('Failed to createCameraInput errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to createCameraInput errorCode = ' + err.code);
}
// 监听cameraInput错误信息
......@@ -80,7 +81,8 @@ let previewOutput: camera.PreviewOutput;
try {
previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId);
} catch (error) {
console.error("Failed to create the PreviewOutput instance.");
let err = error as BusinessError;
console.error(`Failed to create the PreviewOutput instance. error code: ${err.code}`);
}
// 监听预览输出错误信息
......@@ -89,7 +91,7 @@ previewOutput.on('error', (error: BusinessError) => {
});
// 创建ImageReceiver对象,并设置照片参数:分辨率大小是根据前面 photoProfilesArray 获取的当前设备所支持的拍照分辨率大小去设置
let imageReceiver: image.ImageReceiver = await image.createImageReceiver(1920, 1080, 4, 8);
let imageReceiver: image.ImageReceiver = image.createImageReceiver(1920, 1080, 4, 8);
// 获取照片显示SurfaceId
let photoSurfaceId: string = await imageReceiver.getReceivingSurfaceId();
// 创建拍照输出流
......@@ -97,14 +99,16 @@ let photoOutput: camera.PhotoOutput;
try {
photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0], photoSurfaceId);
} catch (error) {
console.error('Failed to createPhotoOutput errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to createPhotoOutput errorCode = ' + err.code);
}
//创建会话
let captureSession: camera.CaptureSession;
try {
captureSession = cameraManager.createCaptureSession();
} catch (error) {
console.error('Failed to create the CaptureSession instance. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to create the CaptureSession instance. errorCode = ' + err.code);
}
// 监听session错误信息
......@@ -116,28 +120,32 @@ captureSession.on('error', (error: BusinessError) => {
try {
captureSession.beginConfig();
} catch (error) {
console.error('Failed to beginConfig. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to beginConfig. errorCode = ' + err.code);
}
// 向会话中添加相机输入流
try {
captureSession.addInput(cameraInput);
} catch (error) {
console.error('Failed to addInput. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to addInput. errorCode = ' + err.code);
}
// 向会话中添加预览输出流
try {
captureSession.addOutput(previewOutput);
} catch (error) {
console.error('Failed to addOutput(previewOutput). errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to addOutput(previewOutput). errorCode = ' + err.code);
}
// 向会话中添加拍照输出流
try {
captureSession.addOutput(photoOutput);
} catch (error) {
console.error('Failed to addOutput(photoOutput). errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to addOutput(photoOutput). errorCode = ' + err.code);
}
// 提交会话配置
......@@ -152,7 +160,8 @@ let flashStatus: boolean;
try {
flashStatus = captureSession.hasFlash();
} catch (error) {
console.error('Failed to hasFlash. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to hasFlash. errorCode = ' + err.code);
}
console.info('Promise returned with the flash light support status:' + flashStatus);
......@@ -163,14 +172,16 @@ if (flashStatus) {
let status: boolean = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO);
flashModeStatus = status;
} catch (error) {
console.error('Failed to check whether the flash mode is supported. errorCode = ' + error.code);
let err = error as BusinessError;
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) {
console.error('Failed to set the flash mode. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to set the flash mode. errorCode = ' + err.code);
}
}
}
......@@ -181,7 +192,8 @@ try {
let status: boolean = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO);
focusModeStatus = status;
} catch (error) {
console.error('Failed to check whether the focus mode is supported. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to check whether the focus mode is supported. errorCode = ' + err.code);
}
if (focusModeStatus) {
......@@ -189,23 +201,26 @@ if (focusModeStatus) {
try {
captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO);
} catch (error) {
console.error('Failed to set the focus mode. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to set the focus mode. errorCode = ' + err.code);
}
}
// 获取相机支持的可变焦距比范围
let zoomRatioRange;
let zoomRatioRange: Array<number>;
try {
zoomRatioRange: Array<number> = captureSession.getZoomRatioRange();
zoomRatioRange = captureSession.getZoomRatioRange();
} catch (error) {
console.error('Failed to get the zoom ratio range. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to get the zoom ratio range. errorCode = ' + err.code);
}
// 设置可变焦距比
try {
captureSession.setZoomRatio(zoomRatioRange[0]);
} catch (error) {
console.error('Failed to set the zoom ratio value. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to set the zoom ratio value. errorCode = ' + err.code);
}
let settings: camera.PhotoCaptureSetting = {
quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, // 设置图片质量高
......
......@@ -17,12 +17,12 @@
通过image的createImageReceiver方法创建ImageReceiver实例,再通过实例的getReceivingSurfaceId方法获取SurfaceId,与拍照输出流相关联,获取拍照输出流的数据。
```ts
function getImageReceiverSurfaceId() {
async function getImageReceiverSurfaceId() {
let receiver: image.ImageReceiver = image.createImageReceiver(640, 480, 4, 8);
console.info('before ImageReceiver check');
if (receiver !== undefined) {
console.info('ImageReceiver is ok');
let photoSurfaceId: string = receiver.getReceivingSurfaceId();
let photoSurfaceId: string = await receiver.getReceivingSurfaceId();
console.info('ImageReceived id: ' + JSON.stringify(photoSurfaceId));
} else {
console.info('ImageReceiver is not ok');
......@@ -43,7 +43,8 @@
try {
photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0], photoSurfaceId);
} catch (error) {
console.error('Failed to createPhotoOutput errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to createPhotoOutput errorCode = ' + err.code);
}
```
......@@ -57,7 +58,8 @@
try {
flashStatus = captureSession.hasFlash();
} catch (error) {
console.error('Failed to hasFlash. errorCode = ' + error.code);
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) {
......@@ -67,14 +69,16 @@
let status: boolean = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO);
flashModeStatus = status;
} catch (error) {
console.error('Failed to check whether the flash mode is supported. errorCode = ' + error.code);
let err = error as BusinessError;
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) {
console.error('Failed to set the flash mode. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to set the flash mode. errorCode = ' + err.code);
}
}
}
......@@ -84,14 +88,16 @@
let status: boolean = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO);
focusModeStatus = status;
} catch (error) {
console.error('Failed to check whether the focus mode is supported. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to check whether the focus mode is supported. errorCode = ' + err.code);
}
if (focusModeStatus) {
// 设置连续自动变焦模式
try {
captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO);
} catch (error) {
console.error('Failed to set the focus mode. errorCode = ' + error.code);
} catch (error) {
let err = error as BusinessError;
console.error('Failed to set the focus mode. errorCode = ' + err.code);
}
}
// 获取相机支持的可变焦距比范围
......@@ -99,13 +105,15 @@
try {
zoomRatioRange = captureSession.getZoomRatioRange();
} catch (error) {
console.error('Failed to get the zoom ratio range. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to get the zoom ratio range. errorCode = ' + err.code);
}
// 设置可变焦距比
try {
captureSession.setZoomRatio(zoomRatioRange[0]);
} catch (error) {
console.error('Failed to set the zoom ratio value. errorCode = ' + error.code);
let err = error as BusinessError;
console.error('Failed to set the zoom ratio value. errorCode = ' + err.code);
}
```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册