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' 报错,无法编译
3292浏览 • 1回复 待解决
import asset from '@ohos.security.asset'报错
1992浏览 • 1回复 待解决
import brightness from '@ohos.brightness' 无法导入
295浏览 • 1回复 待解决
HarmonyOS import router from '@ohos.router';和import { router } from '@kit.ArkUI'哪个是最新的
178浏览 • 1回复 待解决
import asset from "@ohos.security.asset" 导入报错
1579浏览 • 1回复 待解决
JS引入import deviceInfo from '@ohos.deviceInfo'失败
6654浏览 • 1回复 待解决
申请权限ohos.permission.CAMERA以使用相机时,可以用camera picker替代,不用申请权限吗?
55浏览 • 1回复 待解决
OpenHarmony 3.0 LTS camera驱动
4023浏览 • 0回复 待解决
camera 获取预览数据
1669浏览 • 1回复 待解决
HarmonyOS import tag from '@ohos.nfc.tag'提示设备不支持该能力
142浏览 • 1回复 待解决
HarmonyOS camera设置对焦无效果
257浏览 • 1回复 待解决
OpenHarmony camera sensor调试疑问
1701浏览 • 0回复 待解决
camera_lite预览功能如何实现?
2256浏览 • 0回复 待解决
HarmonyOS 项目中import tag from '@ohos.nfc.tag';会报红,而且接口也用不了
373浏览 • 1回复 待解决
ndk中没有native camera对应头文件
1751浏览 • 1回复 待解决
关于鸿蒙camera外设的驱动编写疑问
4427浏览 • 1回复 待解决
HarmonyOS Camera录制视频如何适配竖屏?
108浏览 • 1回复 待解决
关于import router from '@system.router'提示问题?
4875浏览 • 1回复 待解决
camera demo可执行文件编译
3975浏览 • 0回复 待解决
HarmonyOS Camera_CaptureSession 能不能重用
276浏览 • 1回复 待解决
HarmonyOS camera picker 可以实现轻触拍照,长按录制吗?
71浏览 • 1回复 待解决
HarmonyOS camera预览退后台,不输出相机数据
298浏览 • 1回复 待解决
HarmonyOS Camera预览可以直接输出到纹理上吗
249浏览 • 1回复 待解决
【求助】自定义相机Camera2焦距异常
7855浏览 • 1回复 待解决
Camera相机预览黑屏,拍照photoOutPut capture catch code:"7400104"
1745浏览 • 1回复 待解决
参考以下demo: