一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

鴻蒙實戰(zhàn)開發(fā)Camera組件:【相機】

jf_46214456 ? 來源:jf_46214456 ? 作者:jf_46214456 ? 2024-03-08 16:20 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

相機組件支持相機業(yè)務(wù)的開發(fā),開發(fā)者可以通過已開放的接口實現(xiàn)相機硬件的訪問、操作和新功能開發(fā),最常見的操作如:預(yù)覽、拍照和錄像等。

基本概念

  • 拍照
    此功能用于拍攝采集照片。
  • 預(yù)覽
    此功能用于在開啟相機后,在緩沖區(qū)內(nèi)重復(fù)采集攝像幀,支持在拍照或錄像前進行攝像幀預(yù)覽顯示。
  • 錄像
    此功能用于在開始錄像后和結(jié)束錄像前的時間段內(nèi),在緩沖區(qū)內(nèi)重復(fù)采集攝像幀,支持視頻錄制。

圖 1 相機組件架構(gòu)圖

目錄

倉目錄結(jié)構(gòu)如下:

/foundation/multimedia/camera_framework   # 相機組件業(yè)務(wù)代碼
├── frameworks                           # 框架代碼
│   ├── native                           # 內(nèi)部接口實現(xiàn)
│   │   ├── camera                       # 相機框架實現(xiàn)
│   │   └── metadata                     # 元數(shù)據(jù)實現(xiàn)
│   └── js                               # 外部接口實現(xiàn)
│       └── camera_napi                  # 相機NAPI實現(xiàn)
├── interfaces                           # 接口代碼
│   ├── inner_api                        # 內(nèi)部接口
│   └── kits                             # 外部接口
├── LICENSE                              # 許可證文件
├── ohos.build                           # 構(gòu)建文件
├── sa_profile                           # 服務(wù)配置文件
└── services                             # 服務(wù)代碼
    ├── camera_service                   # 相機服務(wù)實現(xiàn)
    └── etc                              # 相機服務(wù)配置

使用說明

拍照

拍照的步驟:

  1. 創(chuàng)建緩沖區(qū)消費者端監(jiān)聽器(CaptureSurfaceListener)以保存圖像。
    class CaptureSurfaceListener : public IBufferConsumerListener {
    public:
        int32_t mode_;
        sptr< Surface > surface_;
    
        void OnBufferAvailable() override
        {
            int32_t flushFence = 0;
            int64_t timestamp = 0;
            OHOS::Rect damage; // initialize the damage
    
            OHOS::sptr< OHOS::SurfaceBuffer > buffer = nullptr;
            surface_- >AcquireBuffer(buffer, flushFence, timestamp, damage);
            if (buffer != nullptr) {
                void* addr = buffer- >GetVirAddr();
                int32_t size = buffer- >GetSize();
    
                // Save the buffer(addr) to a file.
    
                surface_- >ReleaseBuffer(buffer, -1);
            }
        }
    };
    
  2. 獲取相機管理器實例并獲取相機對象列表。
    sptr< CameraManager > camManagerObj = CameraManager::GetInstance();
    std::vector< sptr< CameraInfo >> cameraObjList = camManagerObj- >GetCameras();
    
  3. 使用相機對象創(chuàng)建相機輸入來打開相機。
    sptr< CaptureInput > cameraInput = camManagerObj- >CreateCameraInput(cameraObjList[0]);
    
  4. 創(chuàng)建采集會話。
    sptr< CaptureSession > captureSession = camManagerObj- >CreateCaptureSession();
    
  5. 開始配置采集會話。
    int32_t result = captureSession- >BeginConfig();
    
  6. 將相機輸入添加到采集會話。
    result = captureSession- >AddInput(cameraInput);
    
  7. 創(chuàng)建消費者 Surface 并注冊監(jiān)聽器以監(jiān)聽緩沖區(qū)更新。拍照的寬和高可以配置為所支持的 1280x960 分辨率。
    sptr< Surface > photoSurface = Surface::CreateSurfaceAsConsumer();
    int32_t photoWidth = 1280;
    int32_t photoHeight = 960;
    photoSurface- >SetDefaultWidthAndHeight(photoWidth, photoHeight);
    photoSurface- >SetUserData(CameraManager::surfaceFormat, std::to_string(OHOS_CAMERA_FORMAT_JPEG));
    sptr< CaptureSurfaceListener > capturelistener = new(std::nothrow) CaptureSurfaceListener();
    capturelistener- >mode_ = MODE_PHOTO;
    capturelistener- >surface_ = photoSurface;
    photoSurface- >RegisterConsumerListener((sptr< IBufferConsumerListener > &)capturelistener);
    
  8. 使用上面創(chuàng)建的 Surface 創(chuàng)建拍照輸出。
    sptr< CaptureOutput > photoOutput = camManagerObj- >CreatePhotoOutput(photoSurface);
    
  9. 將拍照輸出添加到采集會話。
    result = captureSession- >AddOutput(photoOutput);
    
  10. 將配置提交到采集會話。
result = captureSession- >CommitConfig();
  1. 拍攝照片。
result = ((sptr PhotoOutput > &)photoOutput)- >Capture();
  1. 釋放采集會話資源。
captureSession- >Release();
  1. 釋放相機輸入關(guān)閉相機。
cameraInput- >Release();

開始和停止預(yù)覽

開始和停止預(yù)覽的步驟:

  1. 獲取相機管理器實例并獲取相機對象列表。
    sptr< CameraManager > camManagerObj = CameraManager::GetInstance();
    std::vector< sptr< CameraInfo >> cameraObjList = camManagerObj- >GetCameras();
    
  2. 使用相機對象創(chuàng)建相機輸入來打開相機。
    sptr< CaptureInput > cameraInput = camManagerObj- >CreateCameraInput(cameraObjList[0]);
    
  3. 創(chuàng)建采集會話。
    sptr< CaptureSession > captureSession = camManagerObj- >CreateCaptureSession();
    
  4. 開始配置采集會話。
    int32_t result = captureSession- >BeginConfig();
    
  5. 將相機輸入添加到采集會話。
    result = captureSession- >AddInput(cameraInput);
    
  6. 使用從窗口管理器獲得的 Surface 創(chuàng)建預(yù)覽輸出用以在顯示上渲染。預(yù)覽的寬和高可以配置為所支持的 640x480 或 832x480 分辨率,如果想保存到文件,可以按照拍照流程提到步驟,創(chuàng)建 Surface,注冊監(jiān)聽器以監(jiān)聽緩沖區(qū)更新。
    int32_t previewWidth = 640;
    int32_t previewHeight = 480;
    previewSurface- >SetUserData(CameraManager::surfaceFormat, std::to_string(OHOS_CAMERA_FORMAT_YCRCB_420_SP));
    sptr< CaptureOutput > previewOutput = camManagerObj- >CreateCustomPreviewOutput(previewSurface, previewWidth, previewHeight);
    
  7. 將預(yù)覽輸出添加到采集會話。
    result = captureSession- >AddOutput(previewOutput);
    
  8. 將配置提交到采集會話。
    result = captureSession- >CommitConfig();
    
  9. 開始預(yù)覽。
    result = captureSession- >Start();
    
  10. 需要時停止預(yù)覽。
result = captureSession- >Stop();
  1. 釋放采集會話資源。
captureSession- >Release();
  1. 釋放相機輸入關(guān)閉相機。
cameraInput- >Release();

視頻錄像

視頻錄像的步驟:

  1. 獲取相機管理器實例并獲取相機對象列表。
    sptr< CameraManager > camManagerObj = CameraManager::GetInstance();
    std::vector< sptr< CameraInfo >> cameraObjList = camManagerObj- >GetCameras();
    
  2. 使用相機對象創(chuàng)建相機輸入來打開相機。
    sptr< CaptureInput > cameraInput = camManagerObj- >CreateCameraInput(cameraObjList[0]);
    
  3. 創(chuàng)建采集會話。
    sptr< CaptureSession > captureSession = camManagerObj- >CreateCaptureSession();
    
  4. 開始配置采集會話。
    int32_t result = captureSession- >BeginConfig();
    
  5. 將相機輸入添加到采集會話。
    result = captureSession- >AddInput(cameraInput);
    
  6. 通過 Surface 創(chuàng)建一個視頻輸出,來與音頻合成并保存到文件,Surface 通過 Recoder 獲取。如果想僅保存視頻緩沖數(shù)據(jù)到文件里,可以按照拍照流程提到步驟,創(chuàng)建 Surface,注冊監(jiān)聽器以監(jiān)聽緩沖區(qū)更新。錄像的分辨率可以在錄制器內(nèi)配置為所支持的 1280x720 或 640x360 分辨率。
    videoSurface- >SetUserData(CameraManager::surfaceFormat, std::to_string(OHOS_CAMERA_FORMAT_YCRCB_420_SP));
    sptr< CaptureOutput > videoOutput = camManagerObj- >CreateVideoOutput(videoSurface);
    
  7. 將視頻輸出添加到采集會話。
    result = captureSession- >AddOutput(videoOutput);
    
  8. 將配置提交到采集會話。
    result = captureSession- >CommitConfig();
    
  9. 開始視頻錄制。
    result = ((sptr VideoOutput > &)videoOutput)- >Start();
    
  10. 需要時停止錄制。
result = ((sptr VideoOutput > &)videoOutput)- >Stop();
  1. 釋放采集會話的資源。
captureSession- >Release();
  1. 釋放相機輸入關(guān)閉相機。
cameraInput- >Release();

切換多個照相機設(shè)備

以下演示如何切換多個照相機設(shè)備。最初在采集會話中有一個視頻輸出(video output)。如果用戶想要切換其他 照相機,現(xiàn)存的相機輸入和輸出需要先移除并加入新的相機輸入和輸出(示例中使用的是photo output)。

  1. 獲取相機管理器實例并獲取相機對象列表。
    sptr< CameraManager > camManagerObj = CameraManager::GetInstance();
    std::vector< sptr< CameraInfo >> cameraObjList = camManagerObj- >GetCameras();
    
  2. 使用相機對象創(chuàng)建相機輸入來打開相機。
    sptr< CaptureInput > cameraInput = camManagerObj- >CreateCameraInput(cameraObjList[0]);
    
  3. 創(chuàng)建采集會話。
    sptr< CaptureSession > captureSession = camManagerObj- >CreateCaptureSession();
    
  4. 開始配置采集會話。
    int32_t result = captureSession- >BeginConfig()
    
  5. 將相機輸入添加到采集會話。
    result = captureSession- >AddInput(cameraInput);
    
  6. 通過Surface創(chuàng)建一個視頻輸出。
    sptr< CaptureOutput > videoOutput = camManagerObj- >CreateVideoOutput(videoSurface);
    
  7. 將視頻輸出添加到采集會話。
    result = captureSession- >AddOutput(videoOutput);
    
  8. 將配置提交到采集會話。
    result = captureSession- >CommitConfig();
    
  9. 開始錄制視頻。
    result = ((sptr VideoOutput > &)videoOutput)- >Start();
    
  10. 需要時停止錄制。
result = ((sptr VideoOutput > &)videoOutput)- >Stop();
  1. 重新配置會話并移除相機輸入和輸出。
int32_t result = captureSession- >BeginConfig();
  1. 在新的會話配置中移除相機輸入。
int32_t result = captureSession- >RemoveInput(cameraInput);
  1. 同樣移除相機輸出。
int32_t result = captureSession- >RemoveOutut(videoOutput);
  1. 創(chuàng)建新的相機輸入,并把它添加到采集會話。
sptr< CaptureInput > cameraInput2 = camManagerObj- >CreateCameraInput(cameraObjList[1]);
result = captureSession- >AddInput(cameraInput2);
  1. 創(chuàng)建拍照輸出,成功創(chuàng)建后將拍照輸出添加到采集會話。創(chuàng)建消費者 Surface 并注冊監(jiān)聽器以監(jiān)聽新的拍照輸出緩沖區(qū)更新。這個 Surface 用于新創(chuàng)建的拍照輸出。
// Get the surface
sptr< Surface > photoSurface = Surface::CreateSurfaceAsConsumer();
int32_t photoWidth = 1280;
int32_t photoHeight = 960;
photoSurface- >SetDefaultWidthAndHeight(photoWidth, photoHeight);
photoSurface- >SetUserData(CameraManager::surfaceFormat, std::to_string(OHOS_CAMERA_FORMAT_JPEG));
sptr< CaptureSurfaceListener > capturelistener = new(std::nothrow) CaptureSurfaceListener();
capturelistener- >mode_ = MODE_PHOTO;
capturelistener- >surface_ = photoSurface;
photoSurface- >RegisterConsumerListener((sptr< IBufferConsumerListener > &)capturelistener);

// Create the Photo Output
sptr< CaptureOutput > photoOutput = camManagerObj- >CreatePhotoOutput(photoSurface);

// Add the output to the capture session
result = captureSession- >AddOutput(photoOutput);
  1. 將配置提交到采集會話。
result = captureSession- >CommitConfig();
  1. 釋放被移出會話的相機輸入。
cameraInput- >Release();
  1. 拍攝照片。
result = ((sptr PhotoOutput > &)photoOutput)- >Capture();
  1. 釋放采集會話資源。
captureSession- >Release();
  1. 釋放相機輸入關(guān)閉相機。
cameraInput2- >Release();

設(shè)置閃光燈

拍照和錄像前可以在相機輸入里設(shè)置閃光燈。

  1. 在照相中設(shè)置閃光燈。

    cameraInput- >LockForControl();
    cameraInput- >SetFlashMode(OHOS_CAMERA_FLASH_MODE_OPEN);
    cameraInput- >UnlockForControl();
    
  2. 在錄像中設(shè)置閃光燈。

    cameraInput- >LockForControl();
    cameraInput- >SetFlashMode(OHOS_CAMERA_FLASH_MODE_ALWAYS_OPEN);
    cameraInput- >UnlockForControl();
    
  3. 關(guān)閉閃光燈。

    cameraInput- >LockForControl();
    cameraInput- >SetFlashMode(OHOS_CAMERA_FLASH_MODE_CLOSE);
    cameraInput- >UnlockForControl();
    

    鴻蒙OpenHarmony知識已更新←前往

765e5079845b5bb77f2aa30a2da70670.jpeg

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • Camera
    +關(guān)注

    關(guān)注

    0

    文章

    81

    瀏覽量

    21531
  • 鴻蒙
    +關(guān)注

    關(guān)注

    60

    文章

    2618

    瀏覽量

    44038
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點推薦

    【HarmonyOS 5】金融應(yīng)用開發(fā)鴻蒙組件實踐

    【HarmonyOS 5】金融應(yīng)用開發(fā)鴻蒙組件實踐 ##鴻蒙開發(fā)能力 ##HarmonyOS SDK應(yīng)用服務(wù)##
    的頭像 發(fā)表于 07-11 18:20 ?277次閱讀
    【HarmonyOS 5】金融應(yīng)用<b class='flag-5'>開發(fā)</b><b class='flag-5'>鴻蒙</b><b class='flag-5'>組件</b>實踐

    鴻蒙5開發(fā)寶藏案例分享---一多開發(fā)實例(音樂)

    各位開發(fā)者小伙伴們好呀!今天咱們來點硬核干貨!最近在鴻蒙文檔中心挖到一座“金礦”——官方竟然暗藏了100+實戰(zhàn)案例,從分布式架構(gòu)到交互動效優(yōu)化應(yīng)有盡有!這些案例不僅藏著華為工程師的私房技巧,還直接
    的頭像 發(fā)表于 06-30 11:54 ?335次閱讀

    鴻蒙5開發(fā)寶藏案例分享---Swiper組件性能優(yōu)化實戰(zhàn)

    鴻蒙寶藏:Swiper組件性能優(yōu)化實戰(zhàn),告別卡頓丟幀! 大家好!最近在鴻蒙開發(fā)時,偶然發(fā)現(xiàn)了官方文檔里埋藏的 性能優(yōu)化寶藏案例 ,尤其是&l
    發(fā)表于 06-12 17:53

    鴻蒙5開發(fā)寶藏案例分享---瀑布流優(yōu)化實戰(zhàn)分享

    鴻蒙瀑布流性能優(yōu)化實戰(zhàn):告別卡頓的寶藏指南! 大家好!最近在鴻蒙文檔里挖到一個 性能優(yōu)化寶藏庫 ,原來官方早就準備好了各種場景的最佳實踐!今天重點分享「瀑布流加載慢丟幀」的解決方案,附完整代碼解析
    發(fā)表于 06-12 17:41

    鴻蒙5開發(fā)寶藏案例分享---性能優(yōu)化案例解析

    鴻蒙性能優(yōu)化寶藏指南:實戰(zhàn)工具與代碼案例解析 大家好呀!今天在翻鴻蒙開發(fā)者文檔時,意外挖到一個 性能優(yōu)化寶藏庫 ——原來官方早就提供了超多實用工具和案例,但很多小伙伴可能沒發(fā)現(xiàn)!這篇就
    發(fā)表于 06-12 16:36

    鴻蒙5開發(fā)寶藏案例分享---埋點開發(fā)實戰(zhàn)指南

    鴻蒙埋點開發(fā)寶藏指南:官方案例實戰(zhàn)解析,輕松搞定數(shù)據(jù)追蹤! 大家好呀!我是HarmonyOS開發(fā)路上的探索者。最近在折騰應(yīng)用埋點時,意外發(fā)現(xiàn)了鴻蒙
    發(fā)表于 06-12 16:30

    鴻蒙5開發(fā)寶藏案例分享---應(yīng)用架構(gòu)實戰(zhàn)技巧

    大家好! 今天咱們聊聊鴻蒙開發(fā)中那些“官方文檔提了但實際開發(fā)難找”的架構(gòu)設(shè)計技巧。結(jié)合官方文檔,我會用 真實代碼案例+通俗講解 ,幫你把分層架構(gòu)和線程通信落地到項目里,告別“理論會了,代碼不會
    發(fā)表于 06-12 16:14

    鴻蒙5開發(fā)寶藏案例分享---Pura X開發(fā)案例分享

    爆發(fā),現(xiàn)在吃透這些技術(shù),你就是明年漲薪最靚的仔! **? (原創(chuàng)整理不易,如果覺得有用,點個贊讓我知道吧~ 下期分享\"鴻蒙分布式相機開發(fā)\"實戰(zhàn)!)
    發(fā)表于 06-12 11:47

    HarmonyOS實戰(zhàn)組件化項目搭建

    前言 鴻蒙應(yīng)用開發(fā)已經(jīng)成為互聯(lián)網(wǎng)新的風(fēng)口,開發(fā)鴻蒙軟件已經(jīng)成為今年工作的核心目標。在軟件開發(fā)過程中,對于復(fù)雜度較大,功能較多的軟件都會采用
    的頭像 發(fā)表于 06-09 14:58 ?206次閱讀
    HarmonyOS<b class='flag-5'>實戰(zhàn)</b>:<b class='flag-5'>組件</b>化項目搭建

    鴻蒙5開發(fā)寶藏案例分享---折疊屏懸停態(tài)開發(fā)實踐

    ?【鴻蒙折疊屏開發(fā)寶藏指南】原來官方藏了這么多好東西!手把手教你玩轉(zhuǎn)懸停態(tài)開發(fā)**?** Hey小伙伴們!我是你們的老朋友XX,最近在肝鴻蒙折疊屏項目時,意外挖到了官方文檔里的隱藏寶藏
    發(fā)表于 06-03 12:04

    鴻蒙5開發(fā)寶藏案例分享---一多分欄開發(fā)實踐

    ?【HarmonyOS開發(fā)者的寶藏指南】一次搞定多設(shè)備分欄布局,原來還能這么玩! 大家好呀!今天在鴻蒙社區(qū)挖到一個超實用的大寶藏——原來官方早就藏了一堆分欄布局的實戰(zhàn)案例!作為被多端適配折磨
    發(fā)表于 06-03 12:03

    鴻蒙Flutter實戰(zhàn):14-現(xiàn)有Flutter 項目支持鴻蒙 II

    分別安裝官方的3.22版本,以及鴻蒙社區(qū)的 3.22.0 版本 3.搭建 Flutter鴻蒙開發(fā)環(huán)境 參考文章《鴻蒙Flutter實戰(zhàn):0
    發(fā)表于 12-26 14:59

    鴻蒙Flutter實戰(zhàn):09-現(xiàn)有Flutter項目支持鴻蒙

    # 鴻蒙Flutter實戰(zhàn):現(xiàn)有Flutter項目支持鴻蒙 ## 背景 原來使用Flutter開發(fā)的項目,需要適配鴻蒙。 ## 環(huán)境搭
    發(fā)表于 10-23 16:36

    鴻蒙Flutter實戰(zhàn):08-如何調(diào)試代碼

    # 鴻蒙Flutter實戰(zhàn):如何調(diào)試代碼 ## 1.環(huán)境搭建 參考文章[鴻蒙Flutter實戰(zhàn):01-搭建開發(fā)環(huán)境](https://g
    發(fā)表于 10-23 16:29

    鴻蒙Flutter實戰(zhàn):07混合開發(fā)

    # 鴻蒙Flutter實戰(zhàn):混合開發(fā) 鴻蒙Flutter混合開發(fā)主要有兩種形式。 ## 1.基于har 將flutter module
    發(fā)表于 10-23 16:00