同步操作将从 OpenHarmony/multimedia_camera_framework 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
The camera_standard repository enables your application to provide camera functions. You can access and operate the camera device and develop new functions through open APIs. Common operations include preview, photographing and video recording.
Capture Photo
This function is used to capture a photo.
Preview
This function repeatedly captures multiple frames in the buffer once the camera is started. It is used to preview the subject before capturing a photo or start recording a video.
Record Video
This function repeatedly captures multiple frames in the buffer once the recording is started and continues till the recording is stopped. It is used for video recording.
The structure of the repository directory is as follows:
/foundation/multimedia/camera_framework # Camera code
├── frameworks # Framework code
│ ├── native # Internal Native API Implementation
│ │ ├── camera # Camera Framework Implementation
│ │ └── metadata # Metadata Implementation
│ └── js # External JS API Implementation
│ └── camera_napi # Camera NAPI Implementation
├── interfaces # Interfaces
│ ├── inner_api # Internal Native APIs
│ └── kits # External JS APIs
├── LICENSE # License file
├── ohos.build # Build file
├── sa_profile # Service configuration profile
└── services # Service code
├── camera_service # Camera Service Implementation
└── etc # Camera Service Init Configuration
Steps to capture a photo:
Create the buffer consumer listener(CaptureSurfaceListener) to save the image.
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);
}
}
};
Get the Camera Manager Instance and retrieve the camera object list.
sptr<CameraManager> camManagerObj = CameraManager::GetInstance();
std::vector<sptr<CameraInfo>> cameraObjList = camManagerObj->GetCameras();
Create the Camera Input using the Camera Object to open the camera.
sptr<CaptureInput> cameraInput = camManagerObj->CreateCameraInput(cameraObjList[0]);
Create the Capture Session
sptr<CaptureSession> captureSession = camManagerObj->CreateCaptureSession();
Begin configuring the Capture Session.
int32_t result = captureSession->BeginConfig();
Add the Camera Input to the Capture Session.
result = captureSession->AddInput(cameraInput);
Create the Consumer Surface and register listerner for buffer updates. The photo width and height can be configured to the supported resolution which is 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);
Create a Photo Output with Surface created above.
sptr<CaptureOutput> photoOutput = camManagerObj->CreatePhotoOutput(photoSurface);
Add the Photo Output to the Capture Session.
result = captureSession->AddOutput(photoOutput);
Commit the configuration to Capture Session.
result = captureSession->CommitConfig();
Capture the Photo.
result = ((sptr<PhotoOutput> &)photoOutput)->Capture();
Release the Capture Session resources.
captureSession->Release();
Release the Camera Input to close the camera.
cameraInput->Release();
Steps to start and stop preview:
Get the Camera Manager Instance and retrieve the camera object list.
sptr<CameraManager> camManagerObj = CameraManager::GetInstance();
std::vector<sptr<CameraInfo>> cameraObjList = camManagerObj->GetCameras();
Create the Camera Input using the Camera Object to open the camera.
sptr<CaptureInput> cameraInput = camManagerObj->CreateCameraInput(cameraObjList[0]);
Create the Capture Session
sptr<CaptureSession> captureSession = camManagerObj->CreateCaptureSession();
Begin configuring the Capture Session.
int32_t result = captureSession->BeginConfig();
Add the Camera Input to the Capture Session.
result = captureSession->AddInput(cameraInput);
Create a Preview Output with Surface obtained from Window Manager to render on display. The preview width and Height can be configured to one of the camera supported resolutions. And to save to a file, can follow the step to create Surface and register listerner for buffer updates mentioned in capture photo.
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);
Add the Preview Output to the Capture Session.
result = captureSession->AddOutput(previewOutput);
Commit the configuration to Capture Session.
result = captureSession->CommitConfig();
Start the Preview
result = captureSession->Start();
Stop the Preview when needed.
result = captureSession->Stop();
Release the Capture Session resources.
captureSession->Release();
Release the Camera Input to close the camera.
cameraInput->Release();
Steps to record Video:
Get the Camera Manager Instance and retrieve the camera object list.
sptr<CameraManager> camManagerObj = CameraManager::GetInstance();
std::vector<sptr<CameraInfo>> cameraObjList = camManagerObj->GetCameras();
Create the Camera Input using the Camera Object to open the camera.
sptr<CaptureInput> cameraInput = camManagerObj->CreateCameraInput(cameraObjList[0]);
Create the Capture Session
sptr<CaptureSession> captureSession = camManagerObj->CreateCaptureSession();
Begin configuring the Capture Session.
s int32_t result = captureSession->BeginConfig(); ```
Add the Camera Input to the Capture Session.
result = captureSession->AddInput(cameraInput);
Create a Video Output with Surface obtained from Recoder to MUX with audio and save the file. And to save just Video buffer to a file, can follow the step to create Surface and register listerner for buffer updates mentioned in capture photo. The video resolution can be configured to one of the camera and recorder supported resolutions while setting the recorder configurations.
videoSurface->SetUserData(CameraManager::surfaceFormat, std::to_string(OHOS_CAMERA_FORMAT_YCRCB_420_SP));
sptr<CaptureOutput> videoOutput = camManagerObj->CreateVideoOutput(videoSurface);
Add the Video Output to the Capture Session.
result = captureSession->AddOutput(videoOutput);
Commit the configuration to Capture Session.
result = captureSession->CommitConfig();
Start the Video Recording.
result = ((sptr<VideoOutput> &)videoOutput)->Start();
Stop the recording when needed.
result = ((sptr<VideoOutput> &)videoOutput)->Stop();
Release the Capture Session resources.
captureSession->Release();
Release the Camera Input to close the camera.
cameraInput->Release();
Below steps demonstrate how to switch between the camera devices. Initially a video output is added to the capture session. If user wish to switch between cameras, existing input and output have to be removed first and then add a new input and output(i.e., photo output in this case).
Get the Camera Manager Instance and retrieve the camera object list.
sptr<CameraManager> camManagerObj = CameraManager::GetInstance();
std::vector<sptr<CameraInfo>> cameraObjList = camManagerObj->GetCameras();
Create the Camera Input using the Camera Object to open the camera.
sptr<CaptureInput> cameraInput = camManagerObj->CreateCameraInput(cameraObjList[0]);
Create the Capture Session
sptr<CaptureSession> captureSession = camManagerObj->CreateCaptureSession();
Begin configuring the Capture Session.
int32_t result = captureSession->BeginConfig();
Add the Camera Input to the Capture Session.
result = captureSession->AddInput(cameraInput);
Create a Video Output with Surface
sptr<CaptureOutput> videoOutput = camManagerObj->CreateVideoOutput(videoSurface);
Add the Video Output to the Capture Session.
result = captureSession->AddOutput(videoOutput);
Commit the configuration to Capture Session.
result = captureSession->CommitConfig();
Start the Video Recording.
result = ((sptr<VideoOutput> &)videoOutput)->Start();
Stop the recording when needed.
result = ((sptr<VideoOutput> &)videoOutput)->Stop();
In order to remove camera input and output, configure the Capture Session again.
int32_t result = captureSession->BeginConfig();
Remove the Camera Input in the new capture session configuration.
int32_t result = captureSession->RemoveInput(cameraInput);
Remove the Camera Output as well.
int32_t result = captureSession->RemoveOutut(videoOutput);
Create new camera input, add it to capture session
sptr<CaptureInput> cameraInput2 = camManagerObj->CreateCameraInput(cameraObjList[1]);
result = captureSession->AddInput(cameraInput2);
Create the photo output and add the photo output to the capture session once it is successfully created. Create the Consumer Surface and register listerner for buffer updates prior to creation of photo output. The surface is used for creation of photo output.
// 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);
Commit the configuration to Capture Session.
result = captureSession->CommitConfig();
Release the Camera Input that is removed from session.
cameraInput->Release();
Capture the Photo.
result = ((sptr<PhotoOutput> &)photoOutput)->Capture();
Release the Capture Session resources.
captureSession->Release();
Release the Camera Input to close the camera.
cameraInput2->Release();
Flash mode can be configured on the Camera Input before capturing a photo or recording a video.
Set the flash mode for photo capture:
cameraInput->LockForControl();
cameraInput->SetFlashMode(OHOS_CAMERA_FLASH_MODE_OPEN);
cameraInput->UnlockForControl();
Set the flash mode for video recording:
cameraInput->LockForControl();
cameraInput->SetFlashMode(OHOS_CAMERA_FLASH_MODE_ALWAYS_OPEN);
cameraInput->UnlockForControl();
Turnoff the flash:
cameraInput->LockForControl();
cameraInput->SetFlashMode(OHOS_CAMERA_FLASH_MODE_CLOSE);
cameraInput->UnlockForControl();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。