這里為何我特意強(qiáng)調(diào)是OpenHarmony3.2 beta4,因?yàn)槲野l(fā)現(xiàn)即使同為3.2版本,beta4上的Camera相關(guān)的api和beta2版本差距都非常大,于是選取了當(dāng)前最新的版本進(jìn)行講解。

??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??
隨著OpenHarmony的版本更新,在3.2上已經(jīng)提供了非常豐富的API來(lái)調(diào)用照相機(jī)。此處講解的是原生的使用相機(jī)的流程,并發(fā)像Android普通應(yīng)用開(kāi)發(fā)一樣通過(guò)一個(gè)intent直接調(diào)用系統(tǒng)相機(jī)應(yīng)用進(jìn)行拍照,根據(jù)原生的調(diào)用相機(jī)的API可以讓大家自己定義功能更加豐富的相機(jī)應(yīng)用。
這里為何我特意強(qiáng)調(diào)是OpenHarmony3.2 beta4,因?yàn)槲野l(fā)現(xiàn)即使同為3.2版本,beta4上的Camera相關(guān)的api和beta2版本差距都非常大,于是選取了當(dāng)前最新的版本進(jìn)行講解。
既然使用相機(jī),那么第一步是先想辦法把相機(jī)點(diǎn)亮,即能通過(guò)攝像頭看到預(yù)覽畫(huà)面,后面才是拍照、錄像、分布式拍照等功能實(shí)現(xiàn)。
關(guān)于sdk的問(wèn)題
目前在OpenHarmony3.2上調(diào)用相機(jī),需要使用ohos-full-sdk,而非大家下載DevEco Studio所帶的sdk,那個(gè)sdk被稱作為public sdk。關(guān)于sdk的替換辦法可以參考官方文檔“ ??full-SDK替換指南??”,我這里不過(guò)多贅述。
此處核心要注意的一點(diǎn)是,目前我3.2 beta4上用的sdk對(duì)應(yīng)的版本號(hào)為3.2.9.4

而目前官方文檔上寫(xiě)的能下載到的sdk最高版本只有3.2.5.6。

因此,需要我們手動(dòng)下載系統(tǒng)源碼,自己完成sdk的編譯才行,我這里是基于3.2 beta4的系統(tǒng)源碼自行編譯出來(lái)的full-sdk。
啟用相機(jī)打開(kāi)預(yù)覽畫(huà)面核心流程與代碼實(shí)現(xiàn)
(1)動(dòng)態(tài)權(quán)限申請(qǐng)
需要獲取ohos.permission.CAMERA權(quán)限
(2)相機(jī)相關(guān)API操作流程

上面是相機(jī)完整功能使用的時(shí)序圖,這里我們先只按照時(shí)序圖中的流程只實(shí)現(xiàn)預(yù)覽部分。
(3)配合XComponent組件完成相機(jī)預(yù)覽流的輸出
XComponent組件中通過(guò)XComponentController的getXComponentSurfaceId方法可以獲取到sufaceId,然后通過(guò)相機(jī)管理對(duì)象cameraManager.createPreviewOutput這個(gè)關(guān)鍵方法可以綁定該surface,從而實(shí)現(xiàn)預(yù)覽畫(huà)面的輸出。
啟用相機(jī)打開(kāi)預(yù)覽畫(huà)面代碼實(shí)現(xiàn)
import camera from '@ohos.multimedia.camera'
const PERMISSIONS: Array<string> = [
'ohos.permission.CAMERA']
let previewWidth;
let previewHeight;
@Entry
@Component
struct Index {
private mXComponentController: XComponentController = new XComponentController()
private surfaceId: string = '-1'
async initCamera(surfaceId: string){
//動(dòng)態(tài)獲取隱私權(quán)限
let context = getContext(this) as any
await context.requestPermissionsFromUser(PERMISSIONS)
console.log('grantPermission,requestPermissionsFromUser');
// 創(chuàng)建CameraManager對(duì)象
let cameraManager = await camera.getCameraManager(context)
if (!cameraManager) {
console.error('Failed to get the CameraManager instance');
}
// 獲取相機(jī)列表
let cameraArray = await cameraManager.getSupportedCameras()
if (!cameraArray) {
console.error('Failed to get the cameras');
}
for (let index = 0; index < cameraArray.length; index++) {
console.log('cameraId : ' + cameraArray[index].cameraId) // 獲取相機(jī)ID
console.log('cameraPosition : ' + cameraArray[index].cameraPosition) // 獲取相機(jī)位置
console.log('cameraType : ' + cameraArray[index].cameraType) // 獲取相機(jī)類型
console.log('connectionType : ' + cameraArray[index].connectionType) // 獲取相機(jī)連接類型
}
// 創(chuàng)建相機(jī)輸入流
let cameraInput = await cameraManager.createCameraInput(cameraArray[0])
// 打開(kāi)相機(jī)
await cameraInput.open().then(() => {
console.log('opencamera succ.');
}).catch(function(err){
console.log("opencamera failed with error:"+ err);
});
// 獲取相機(jī)設(shè)備支持的輸出流能力
let cameraOutputCap = await cameraManager.getSupportedOutputCapability(cameraArray[0]);
if (!cameraOutputCap) {
console.error("outputCapability outputCapability == null || undefined")
} else {
console.log("outputCapability: " + JSON.stringify(cameraOutputCap));
}
//獲取相機(jī)支持的輸出能力--支持的預(yù)覽配置信息
let previewProfilesArray = cameraOutputCap.previewProfiles;
if (!previewProfilesArray) {
console.error("createOutput previewProfilesArray == null || undefined")
}else{
console.log("previewProfiles:"+JSON.stringify(previewProfilesArray[0]))
previewWidth = previewProfilesArray[0].size.width;
previewHeight = previewProfilesArray[0].size.height;
}
// 創(chuàng)建預(yù)覽輸出流,其中參數(shù) surfaceId 參考下面 XComponent 組件,預(yù)覽流為XComponent組件提供的surface
let previewOutput = await cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId)
if (!previewOutput) {
console.error("Failed to create the PreviewOutput instance.")
}else{
console.log("create the PreviewOutput instance succ.")
}
//創(chuàng)建會(huì)話
let captureSession = await cameraManager.createCaptureSession()
if (!captureSession) {
console.error('Failed to create the CaptureSession instance.');
return;
}
console.log('Callback returned with the CaptureSession instance.' + captureSession);
// 開(kāi)始配置會(huì)話
await captureSession.beginConfig().then(()=>{
console.log('captureSession beginConfig succ');
}).catch(function(err){
console.log("captureSession beginConfig failed with error:"+ err);
});
// 向會(huì)話中添加相機(jī)輸入流
await captureSession.addInput(cameraInput).then(() => {
console.log('captureSession addInput instance is added.');
}).catch(function(err){
console.log("captureSession addInput failed with error:"+ err);
});
// 向會(huì)話中添加預(yù)覽輸入流
await captureSession.addOutput(previewOutput).then(() => {
console.log('captureSession addOutput previewOutput instance is added.');
}).catch(function(err){
console.log("captureSession addOutput previewOutput failed with error:"+ err);
});
// 提交會(huì)話配置
await captureSession.commitConfig().then(() => {
console.log('captureSession commitConfig success.');
}).catch(function(err){
console.log("captureSession commitConfig failed with error:"+ err);
});
// 啟動(dòng)會(huì)話
await captureSession.start().then(() => {
console.log('captureSession start success.');
}).catch(function(err){
console.log("captureSession start failed with error:"+ err);
});
}
build() {
Flex() {
XComponent({ // 創(chuàng)建XComponent
id: '',
type: 'surface',
libraryname: '',
controller: this.mXComponentController
})
.onLoad(() => { // 設(shè)置onload回調(diào)
// 設(shè)置Surface寬高(1920*1080),預(yù)覽尺寸設(shè)置參考前面 previewProfilesArray 獲取的當(dāng)前設(shè)備所支持的預(yù)覽分辨率大小去設(shè)置
this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:previewWidth,surfaceHeight:previewHeight})
// 獲取Surface ID
this.surfaceId = this.mXComponentController.getXComponentSurfaceId()
this.initCamera(this.surfaceId)
})
.width('100%') // 設(shè)置XComponent寬度
.height('100%') // 設(shè)置XComponent高度
}
}
}
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??