import camera from '@ohos.multimedia.camera'
import camera from '@ohos.multimedia.camera';
import { BusinessError } from '@kit.BasicServicesKit';
import { MLog } from '@ohos/ts-core-lib';
const TAG = "camera-capture"
export class CameraCapture {
private cameraManager?: camera.CameraManager
private fontCamera?: camera.CameraDevice //前置
private backCamera?: camera.CameraDevice //后置
private surfaceId: string = ""
private profile?: camera.Profile
private photoOuput?: camera.PhotoOutput
private cameraInput?: camera.CameraInput
private session?: camera.Session
private previewOutput?: camera.PreviewOutput
async init(surfaceId: string) {
try {
this.cameraManager = camera.getCameraManager(getContext(this))
this.cameraManager.on('cameraStatus', (data) => {
MLog.d(TAG, "cameraStatus", data)
})
let muted = this.cameraManager.isCameraMuted()
if (muted) {
MLog.i(TAG, "相机被禁用")
return
}
this.surfaceId = surfaceId
let cameras = this.cameraManager.getSupportedCameras()
//查找前置相机和后置相机
cameras.forEach(item => {
if (item.cameraPosition == camera.CameraPosition.CAMERA_POSITION_BACK) {
this.backCamera = item
} else if (item.cameraPosition == camera.CameraPosition.CAMERA_POSITION_FRONT) {
this.fontCamera = item
}
})
//profile 输出配置
let data = this.getSupportedOutputCapability(this.fontCamera!, this.cameraManager!, camera.SceneMode.NORMAL_PHOTO)
this.profile = data.previewProfiles[0]
//创建 输入 输出 预览
this.cameraInput = this.createInput(this.cameraManager, this.fontCamera!)
this.cameraInput?.on('error', this.fontCamera, (err) => {
MLog.d(TAG, "cameraInput error", err)
})
this.photoOuput = this.createOutputPhoto(this.cameraManager, this.profile)
this.previewOutput = this.createPreview(this.cameraManager, this.profile)
this.session = this.cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO)
this.session.beginConfig()
this.session.addInput(this.cameraInput)
this.session.addOutput(this.previewOutput)
this.session.addOutput(this.photoOuput)
this.session.commitConfig()
this.session?.start()
} catch (e) {
MLog.i(TAG, "The getCameraManager call failed. error code", e)
}
}
async start() {
// this.session?.start()
}
async capture() {
await this.photoOuput?.capture()
}
async stop() {
await this.session?.stop()
}
async release() {
await this.cameraInput?.close()
await this.photoOuput?.release()
await this.previewOutput?.release()
await this.session?.stop()
await this.session?.release()
}
private createInput(cameraManager: camera.CameraManager, device: camera.CameraDevice) {
let cameraInput: camera.CameraInput | undefined = undefined;
try {
cameraInput = cameraManager.createCameraInput(device);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
MLog.e(TAG, `The createCameraInput call failed. error code: ${err.code}`);
}
return cameraInput;
}
private createPreview(cameraManager: camera.CameraManager, profile: camera.Profile) {
let previewOutput: camera.PreviewOutput | undefined = undefined;
try {
previewOutput = cameraManager.createPreviewOutput(profile, this.surfaceId);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
MLog.e(TAG, `The createCameraInput call failed. error code: ${err.code}`);
}
return previewOutput;
}
private createOutputPhoto(cameraManager: camera.CameraManager, profile: camera.Profile): camera.PhotoOutput | undefined {
let photoOutput: camera.PhotoOutput | undefined
try {
photoOutput = cameraManager.createPhotoOutput(profile);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
MLog.e(TAG, `The createPhotoOutput call failed. error code: ${err.code}`);
}
return photoOutput;
}
getSupportedOutputCapability(camera: camera.CameraDevice, cameraManager: camera.CameraManager, sceneMode: camera.SceneMode): camera.CameraOutputCapability {
let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(camera, sceneMode);
return cameraOutputCapability;
}
}
预览是黑屏的。
HarmonyOS
赞
收藏 0
回答 1
待解决
相关问题
import camera from '@ohos.multimedia.camera' 报错,无法编译
3466浏览 • 1回复 待解决
import brightness from '@ohos.brightness' 无法导入
355浏览 • 1回复 待解决
import asset from '@ohos.security.asset'报错
2117浏览 • 1回复 待解决
JS引入import deviceInfo from '@ohos.deviceInfo'失败
6838浏览 • 1回复 待解决
import asset from "@ohos.security.asset" 导入报错
1662浏览 • 1回复 待解决
HarmonyOS import router from '@ohos.router';和import { router } from '@kit.ArkUI'哪个是最新的
307浏览 • 1回复 待解决
申请权限ohos.permission.CAMERA以使用相机时,可以用camera picker替代,不用申请权限吗?
149浏览 • 1回复 待解决
OpenHarmony 3.0 LTS camera驱动
4114浏览 • 0回复 待解决
camera 获取预览数据
1773浏览 • 1回复 待解决
HarmonyOS camera设置对焦无效果
330浏览 • 1回复 待解决
OpenHarmony camera sensor调试疑问
1803浏览 • 0回复 待解决
camera_lite预览功能如何实现?
2358浏览 • 0回复 待解决
HarmonyOS import tag from '@ohos.nfc.tag'提示设备不支持该能力
243浏览 • 1回复 待解决
ndk中没有native camera对应头文件
1832浏览 • 1回复 待解决
HarmonyOS Camera录制视频如何适配竖屏?
225浏览 • 1回复 待解决
关于鸿蒙camera外设的驱动编写疑问
4517浏览 • 1回复 待解决
HarmonyOS 项目中import tag from '@ohos.nfc.tag';会报红,而且接口也用不了
459浏览 • 1回复 待解决
关于import router from '@system.router'提示问题?
5025浏览 • 1回复 待解决
HarmonyOS camera picker 可以实现轻触拍照,长按录制吗?
244浏览 • 1回复 待解决
HarmonyOS Camera_CaptureSession 能不能重用
332浏览 • 1回复 待解决
HarmonyOS Camera录制视频如何适配竖屏
116浏览 • 0回复 待解决
camera demo可执行文件编译
4073浏览 • 0回复 待解决
HarmonyOS camera预览退后台,不输出相机数据
363浏览 • 1回复 待解决
OpenHarmony中camera_service对应的模块是怎么绑定的
1793浏览 • 0回复 待解决
调用 API方法camera.getCameraManager传入context报错如何解决
642浏览 • 1回复 待解决
参考以下demo: