!25 基于图形修改后camera组件的的适配

Merge pull request !25 from obama/dujingcheng
This commit is contained in:
openharmony_ci
2021-07-13 09:05:20 +00:00
committed by Gitee
7 changed files with 178 additions and 40 deletions
+87 -24
View File
@@ -65,6 +65,8 @@ constexpr int32_t SAMPLE_RATE = 48000;
constexpr int32_t FRAME_RATE = 30;
constexpr int32_t RATE = 4096;
constexpr double FPS = 30;
constexpr int32_t MAX_WIDTH = 480;
constexpr int32_t MAX_HEIGHT = 892;
inline void SaveFileData(const std::string& filePath, const char &buffer, uint32_t size, std::string& path)
{
@@ -129,7 +131,6 @@ void FrameCallback::OnFrameFinished(const Media::Camera &camera,
}
surface->ReleaseBuffer(buffer, -1);
}
delete surface;
}
delete &frameConfig;
}
@@ -185,6 +186,10 @@ void CameraCallback::OnCreated(const Media::Camera &camera)
if (hasCallPreView_) {
StartPreview();
}
if (prepareEventListener_) {
prepareEventListener_();
}
}
void CameraCallback::OnCreateFailed(const std::string cameraId, int32_t errorCode)
@@ -326,7 +331,7 @@ void CameraCallback::StartPreview()
return;
}
LOGI("Camera StartPreview.");
LOGI("Camera StartPreview start.");
if (camera_ == nullptr) {
LOGE("Camera is null.");
return;
@@ -346,8 +351,8 @@ void CameraCallback::StartPreview()
if (!subWindow_) {
OHOS::WindowConfig config;
memset_s(&config, sizeof(OHOS::WindowConfig), 0, sizeof(OHOS::WindowConfig));
config.height = windowSize_.Width();
config.width = windowSize_.Height();
config.height = windowSize_.Height();
config.width = windowSize_.Width();
config.format = PIXEL_FMT_RGBA_8888;
config.pos_x = windowOffset_.GetX();
config.pos_y = windowOffset_.GetY();
@@ -490,11 +495,12 @@ void CameraCallback::Stop(bool isClosePreView)
}
if (recordState_ == State::STATE_RUNNING) {
LOGI("CameraCallback:Stop:CloseRecorder.");
CloseRecorder();
}
recordState_ = State::STATE_IDLE;
previewState_ = State::STATE_IDLE;
if (isClosePreView) {
if (isClosePreView && subWindow_) {
LOGI("CameraCallback: Destroy subWindow.");
subWindow_.reset();
auto context = context_.Upgrade();
@@ -548,38 +554,79 @@ void CameraCallback::onRecord(bool isSucces, std::string info)
onRecordListener_(result);
}
void CameraCallback::OnCameraSizeChange(int32_t width, int32_t height)
void CameraCallback::OnCameraSizeChange(double width, double height)
{
auto context = context_.Upgrade();
LOGE("CameraCallback:OnCameraSizeChange %{public}d %{public}d %{public}p-", width, height, subWindow_.get());
if (subWindow_) {
subWindow_->SetSubWindowSize(width, height);
context->ClipRootHole(windowOffset_.GetX(), windowOffset_.GetY(), static_cast<double>(width), static_cast<double>(height));
if (!context) {
LOGE("Camera::CameraCallback: context is null.");
return;
}
if (width + windowOffset_.GetX() > MAX_WIDTH) {
windowSize_.SetWidth(MAX_WIDTH - windowOffset_.GetX());
} else {
windowSize_.SetWidth(width);
}
if (height + windowOffset_.GetY() > MAX_HEIGHT) {
windowSize_.SetHeight(MAX_HEIGHT - windowOffset_.GetY());
} else {
windowSize_.SetHeight(height);
}
if (subWindow_) {
LOGE("CameraCallback::OnCameraSizeChange: %{public}lf %{public}lf.", width, height);
subWindow_->SetSubWindowSize(windowSize_.Width(), windowSize_.Height());
context->ClipRootHole(windowOffset_.GetX(), windowOffset_.GetY(),
windowSize_.Width(), windowSize_.Height());
} else {
LOGE("CameraCallback::OnCameraSizeChange: subwindow is null.");
}
windowSize_.SetWidth(width);
windowSize_.SetWidth(height);
}
void CameraCallback::OnCameraOffsetChange(int32_t x, int32_t y)
void CameraCallback::OnCameraOffsetChange(double x, double y)
{
auto context = context_.Upgrade();
if (subWindow_) {
subWindow_->Move(x, y);
context->ClipRootHole(static_cast<double>(x), static_cast<double>(y), windowSize_.Width(), windowSize_.Height());
if (!context) {
LOGE("Camera::CameraCallback: context is null.");
return;
}
bool sizeChange = false;
if (x + windowSize_.Width() > MAX_WIDTH) {
windowSize_.SetWidth(MAX_WIDTH - x);
sizeChange = true;
}
if (y + windowSize_.Height() > MAX_HEIGHT) {
windowSize_.SetHeight(MAX_HEIGHT - y);
sizeChange = true;
}
if (sizeChange) {
OnCameraSizeChange(windowSize_.Width(), windowSize_.Height());
}
windowOffset_.SetX(x);
windowOffset_.SetX(y);
windowOffset_.SetY(y);
if (subWindow_) {
LOGE("CameraCallback::OnCameraOffsetChange: %{public}lf %{public}lf.", x, y);
subWindow_->Move(windowOffset_.GetX(), windowOffset_.GetY());
context->ClipRootHole(windowOffset_.GetX(), windowOffset_.GetY(),
windowSize_.Width(), windowSize_.Height());
} else {
LOGE("CameraCallback::OnCameraOffsetChange: subwindow is null.");
}
}
void Camera::Release()
{
LOGI("Camera: Release!");
LOGI("Camera: Release start.");
Stop(true);
Media::Camera *camera = const_cast<Media::Camera *>(cameraCallback_.GetCameraInstance());
if (camera != nullptr) {
camera->StopLoopingCapture();
camera = nullptr;
camera->Release();
}
LOGI("Camera: Release end.");
}
void Camera::CreateCamera()
@@ -665,14 +712,30 @@ void Camera::AddRecordListener(RecordListener&& listener)
cameraCallback_.AddRecordListener(std::move(listener));
}
void Camera::OnCameraSizeChange(int32_t width, int32_t height)
void Camera::OnCameraSizeChange(double width, double height)
{
cameraCallback_.OnCameraSizeChange(width, height);
auto context = context_.Upgrade();
if (!context) {
LOGE("Camera::OnCameraSizeChange: context is null.");
return;
}
float viewScale = context->GetViewScale();
LOGI("Camera::OnCameraSizeChange:viewScale %{public}f .", viewScale);
cameraCallback_.OnCameraSizeChange(width * viewScale, height * viewScale);
}
void Camera::OnCameraOffsetChange(int32_t x, int32_t y)
void Camera::OnCameraOffsetChange(double x, double y)
{
cameraCallback_.OnCameraOffsetChange(x, y);
auto context = context_.Upgrade();
if (!context) {
LOGE("Camera::OnCameraOffsetChange: context is null.");
return;
}
float viewScale = context->GetViewScale();
LOGI("Camera::OnCameraOffsetChange:viewScale %{public}f .", viewScale);
cameraCallback_.OnCameraOffsetChange(x * viewScale, y * viewScale);
}
} // namespace OHOS::Ace
+17 -5
View File
@@ -65,6 +65,7 @@ private:
class CameraCallback : public Media::CameraStateCallback {
public:
ACE_DISALLOW_COPY_AND_MOVE(CameraCallback);
using PrepareEventListener = std::function<void()>;
explicit CameraCallback(Media::EventHandler &eventHdlr) : eventHandler_(eventHdlr)
{}
@@ -97,8 +98,12 @@ public:
void AddErrorListener(ErrorListener&& listener);
void AddRecordListener(RecordListener&& listener);
void SetCatchFilePath(std::string cacheFilePath);
void OnCameraSizeChange(int32_t width, int32_t height);
void OnCameraOffsetChange(int32_t x, int32_t y);
void OnCameraSizeChange(double width, double height);
void OnCameraOffsetChange(double x, double y);
void AddPrepareEventListener(PrepareEventListener&& listener)
{
prepareEventListener_ = std::move(listener);
}
protected:
void OnCreated(const Media::Camera &camera) override;
@@ -127,7 +132,7 @@ private:
std::unique_ptr<OHOS::SubWindow> subWindow_;
WeakPtr<PipelineContext> context_;
sptr<Surface> captureSurface_;
Size windowSize_ = Size(TEMPORARY_WINDOW_SIZE, TEMPORARY_WINDOW_SIZE);
Size windowSize_ = Size(0, 0);
Offset windowOffset_ = Offset(0, 0);
bool isReady_ = false;
@@ -135,6 +140,7 @@ private:
State previewState_ = State::STATE_IDLE;
State recordState_ = State::STATE_IDLE;
PrepareEventListener prepareEventListener_;
};
class SurfaceListener : public IBufferConsumerListener {
@@ -149,6 +155,8 @@ class Camera : public virtual AceType {
DECLARE_ACE_TYPE(Camera, AceType);
public:
using PrepareEventListener = std::function<void()>;
Camera(const WeakPtr<PipelineContext> &context, Media::EventHandler &eventHdlr)
: context_(context), cameraCallback_(eventHdlr) {}
~Camera() override = default;
@@ -161,8 +169,12 @@ public:
void StartPreview();
void StartRecord();
void OnCameraSizeChange(int32_t width, int32_t height);
void OnCameraOffsetChange(int32_t x, int32_t y);
void OnCameraSizeChange(double width, double height);
void OnCameraOffsetChange(double x, double y);
void AddPrepareEventListener(PrepareEventListener&& listener)
{
cameraCallback_.AddPrepareEventListener(std::move(listener));
}
void AddTakePhotoListener(TakePhotoListener&& listener);
void AddErrorListener(ErrorListener&& listener);
@@ -99,8 +99,10 @@ void CameraElement::Prepare(const WeakPtr<Element>& parent)
void CameraElement::HiddenChange(bool hidden)
{
if (hidden) {
LOGI("CameraElement::HiddenChange: hidden.");
ReleasePlatformResource();
} else {
LOGI("CameraElement::HiddenChange: show.");
CreatePlatformResource();
}
}
@@ -115,7 +117,13 @@ void CameraElement::InitEvent(const RefPtr<CameraComponent>& cameraComponent)
void CameraElement::OnTextureSize(int64_t textureId, int32_t textureWidth, int32_t textureHeight)
{
LOGI("CameraElement::OnTextureSize");
if (camera_) {
auto renderTexture = AceType::DynamicCast<RenderTexture>(renderNode_);
if (!renderTexture) {
LOGI("CameraElement::OnTextureSize: renderNode is not renderTexture type.");
return;
}
if (camera_ && renderTexture->GetFit() == ImageFit::FILL) {
LOGE("CameraElement:OnTextureSize %{public}d %{public}d ", textureWidth, textureHeight);
camera_->OnCameraSizeChange(textureWidth, textureHeight);
}
@@ -124,11 +132,28 @@ void CameraElement::OnTextureSize(int64_t textureId, int32_t textureWidth, int32
void CameraElement::OnTextureOffset(int64_t textureId, int32_t x, int32_t y)
{
LOGI("CameraElement::OnTextureOffset");
if (camera_) {
camera_->OnCameraOffsetChange(x, y);
LOGE("Camera:OnTextureOffset %{public}d %{public}d", x, y);
StartPreview();
auto renderTexture = AceType::DynamicCast<RenderTexture>(renderNode_);
if (!renderTexture) {
LOGI("CameraElement::OnTextureSize: renderNode is not renderTexture type.");
return;
}
if (camera_ && renderTexture->GetFit() == ImageFit::FILL) {
LOGE("Camera:OnTextureOffset %{public}d %{public}d", x, y);
camera_->OnCameraOffsetChange(x, y);
}
}
void CameraElement::OnPrepared()
{
LOGI("CameraElement::OnPrepared");
if (!renderNode_) {
return;
}
auto camera = AceType::MakeRefPtr<CameraComponent>();
camera->SetFit(ImageFit::FILL);
renderNode_->Update(camera);
}
void CameraElement::CreatePlatformResource()
@@ -209,6 +234,11 @@ void CameraElement::InitListener()
return;
}
if (!camera_) {
LOGE("CameraElement::InitListener: camera is null.");
return;
}
auto uiTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::UI);
auto takePhotoListener = [weak = WeakClaim(this), uiTaskExecutor](
const std::map<std::string, std::string> result) {
@@ -219,6 +249,7 @@ void CameraElement::InitListener()
}
});
};
camera_->AddTakePhotoListener(takePhotoListener);
auto onError = [weak = WeakClaim(this), uiTaskExecutor](const std::string& errorcode, const std::string& errormsg) {
uiTaskExecutor.PostTask([&weak, errorcode, errormsg] {
@@ -228,6 +259,7 @@ void CameraElement::InitListener()
}
});
};
camera_->AddErrorListener(onError);
auto recorderCallBack = [weak = WeakClaim(this), uiTaskExecutor](
const std::map<std::string, std::string> result) {
@@ -238,12 +270,17 @@ void CameraElement::InitListener()
}
});
};
camera_->AddRecordListener(recorderCallBack);
if (camera_) {
camera_->AddTakePhotoListener(takePhotoListener);
camera_->AddErrorListener(onError);
camera_->AddRecordListener(recorderCallBack);
}
auto preparedCallBack = [weak = WeakClaim(this), uiTaskExecutor]() {
uiTaskExecutor.PostTask([&weak] {
auto camera = weak.Upgrade();
if (camera) {
camera->OnPrepared();
}
});
};
camera_->AddPrepareEventListener(preparedCallBack);
}
void CameraElement::OnTakePhotoCallBack(const std::map<std::string, std::string>& result)
@@ -56,6 +56,7 @@ private:
void InitEvent(const RefPtr<CameraComponent>& cameraComponent);
void OnError(const std::string& errorcode, const std::string& errormsg);
void HiddenChange(bool hidden);
void OnPrepared();
void OnRecorderCallBack(const std::map<std::string, std::string>& result);
void TakePhoto(const Size& size);
@@ -92,6 +92,7 @@ public:
theme->dividerColor_ = themeConstants->GetColor(THEME_PICKER_SELECT_DIVIDER_COLOR);
theme->gradientHeight_ = themeConstants->GetDimension(THEME_PICKER_GRADIENT_HEIGHT);
theme->columnFixedWidth_ = themeConstants->GetDimension(THEME_PICKER_COLUMN_FIXED_WIDTH);
theme->pressColor_ = themeConstants->GetColor(THEME_CLICKEFFIC_DEFAULT_COLOR_OHOS);
Parse(themeConstants->GetThemeStyle(), theme);
return theme;
}
@@ -86,7 +86,8 @@ void RenderTexture::PerformLayout()
textureSizeChangeEvent_(textureId_, drawSize_.Width(), drawSize_.Height());
}
if (textureOffsetChangeEvent_) {
textureOffsetChangeEvent_(textureId_, (int32_t)alignmentX_, (int32_t)alignmentY_);
textureOffsetChangeEvent_(textureId_,
(int32_t)(alignmentX_ + GetGlobalOffset().GetX()), (int32_t)(alignmentY_ + GetGlobalOffset().GetY()));
}
MarkNeedRender();
}
@@ -63,6 +63,29 @@ public:
return true;
}
ImageFit GetFit() const
{
return imageFit_;
}
void OnAppShow() override
{
LOGE("RenderTexture: Camera OnAppShow.");
RenderNode::OnAppShow();
if (hiddenChangeEvent_) {
hiddenChangeEvent_(false);
}
}
void OnAppHide() override
{
LOGE("RenderTexture: Camera OnAppHidden.");
RenderNode::OnAppHide();
if (hiddenChangeEvent_) {
hiddenChangeEvent_(true);
}
}
protected:
RenderTexture();