5 回复
import common from '@ohos.app.ability.common';
import camera from '@ohos.multimedia.camera';
import { BusinessError } from '@ohos.base';
import { abilityAccessCtrl, PermissionRequestResult, Permissions } from '@kit.AbilityKit';
async function previewCamera(baseContext: common.BaseContext, surfaceId: string): Promise<void> {
let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext);
let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
let cameraInput: camera.CameraInput | undefined = undefined;
try{
cameraInput = cameraManager.createCameraInput(cameraArray[0]);
cameraInput.on('error', cameraArray[0], (error: BusinessError) => {
console.error(`Camera input error code: ${error.code}`);
});
await cameraInput.open();
} catch (e) {
let error:BusinessError = e as BusinessError;
console.error(`${error.code}:${error.message}`)
}
let cameraOutputCap: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraArray[0]);
let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;
if (!previewProfilesArray) {
console.error("cameraManager.getSupportedOutputCapability error");
return undefined;
}
let previewOutput: camera.PreviewOutput | undefined = undefined;
previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId);
let captureSession: camera.PhotoSession | undefined = undefined;
captureSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession;
try {
captureSession.beginConfig();
captureSession.addInput(cameraInput);
captureSession.addOutput(previewOutput);
await captureSession.commitConfig();
await captureSession.start();
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`${error.code}:${error.message}`)
}
}
@Entry
@Component
export struct CameraPage {
@StorageProp('statusBarHeight') statusBarHeight: number = 0;
private xcomponentController: XComponentController = new XComponentController;
private surfaceId: string = '';
requestCameraPermission(): void {
let permission: Permissions[] = ['ohos.permission.CAMERA'];
let context: Context = getContext(this) as Context;
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(context, permission).then((data: PermissionRequestResult) => {
let grantStatus: Array<number> = data.authResults;
let length: number = grantStatus.length;
for (let i = 0; i < length; i++) {
if (grantStatus[i] != 0) {
return;
}
}
// 授权成功
}).catch((err: BusinessError) => {
console.error(`${err.code}:${err.message}`)
})
}
async aboutToAppear(): Promise<void> {
this.requestCameraPermission()
}
build() {
Column() {
Row() {
XComponent({
id: 'xcomponent',
type: XComponentType.SURFACE,
controller: this.xcomponentController
})
.onLoad(() => {
this.xcomponentController.setXComponentSurfaceSize({ surfaceWidth: 1920, surfaceHeight: 1080 });
this.surfaceId = this.xcomponentController.getXComponentSurfaceId()
})
.width('720px')
.height('480px')
}
.backgroundColor(Color.Black)
Button("开始预览")
.onClick(() => {
previewCamera(getContext(this), this.surfaceId);
})
}
}
}
更多关于HarmonyOS 鸿蒙Next相机预览的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
大佬怎么获取预览的图片啊就是打开预览后自动捕获的照片,
您好,建议您另外提个新的问题帖子,我们的技术支持人员会在论坛中定期扫描提问的技术问题并回复,您的问题如果放到评论里面是扫描不到的。
预览图片是什么意思。上面输出的是预览流数据,你如果想获取预览的图片,可以试一下有页面之后使用组件截图,