fix: prevent face image stretching distortion#3004
fix: prevent face image stretching distortion#3004deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
Conversation
1. Added image cropping to square aspect ratio before scaling to prevent stretching 2. Calculate center crop region based on minimum dimension of source image 3. Scale cropped image to target size while maintaining aspect ratio 4. Apply circular mask to the properly proportioned image Log: Fixed face image display issue where images appeared stretched or distorted fix: 修复人脸图像拉伸变形问题 1. 在缩放前添加图像裁剪为正方形比例,防止拉伸变形 2. 根据源图像最小尺寸计算中心裁剪区域 3. 将裁剪后的图像按比例缩放到目标尺寸 4. 对比例正确的图像应用圆形遮罩 Log: 修复了人脸图像显示时出现拉伸或变形的问题 PMS: BUG-304729
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds a preprocessing step to crop face images to a centered square before scaling and masking, so the displayed circular avatar is not stretched or distorted. Sequence diagram for updated face image processing pipelinesequenceDiagram
actor User
participant FaceAuthController
participant DA_img_source as DA_img
participant QImage
participant QPixmap
participant QPainter
User->>FaceAuthController: updateFaceImgContent(context, DA_img)
FaceAuthController->>DA_img_source: Validate img
DA_img_source-->>FaceAuthController: img data
FaceAuthController->>QImage: Construct QImage from RGB data
QImage-->>FaceAuthController: im
FaceAuthController->>QPixmap: fromImage(im) to create sourcePix
QPixmap-->>FaceAuthController: sourcePix
FaceAuthController->>FaceAuthController: Compute sourceSize, offsetX, offsetY
FaceAuthController->>QPixmap: copy(offsetX, offsetY, sourceSize, sourceSize)
QPixmap-->>FaceAuthController: square croppedPix
FaceAuthController->>QPixmap: scaled(Faceimg_SIZE, Faceimg_SIZE, KeepAspectRatio)
QPixmap-->>FaceAuthController: croppedPix scaled
FaceAuthController->>QPixmap: Create target pix(Faceimg_SIZE, Faceimg_SIZE)
FaceAuthController->>QPainter: Begin painting on pix
FaceAuthController->>QPainter: Set circular clip path
FaceAuthController->>QPainter: drawPixmap(0, 0, croppedPix)
QPainter-->>FaceAuthController: Circular masked avatar
FaceAuthController->>FaceAuthController: Encode pix to base64
FaceAuthController->>User: Emit faceImgContentChanged (updated avatar)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider deriving
sourceSize,offsetX, andoffsetYfromim.width()/im.height()instead ofimg->width/img->heightso the cropping logic is tied to the actualQImagebeing used rather than the raw buffer metadata. - It may be worth guarding against zero or negative dimensions before creating
QPixmap/cropping (e.g., early return ifsourceSize <= 0) to avoid undefined behavior when an invalidDA_imgis passed in.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider deriving `sourceSize`, `offsetX`, and `offsetY` from `im.width()`/`im.height()` instead of `img->width`/`img->height` so the cropping logic is tied to the actual `QImage` being used rather than the raw buffer metadata.
- It may be worth guarding against zero or negative dimensions before creating `QPixmap`/cropping (e.g., early return if `sourceSize <= 0`) to avoid undefined behavior when an invalid `DA_img` is passed in.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
deepin pr auto review这段代码主要实现了人脸识别图像的裁剪、缩放和圆形遮罩处理的功能。以下是对该代码片段的审查意见,涵盖语法逻辑、代码质量、代码性能和代码安全四个方面: 1. 语法逻辑
2. 代码质量
3. 代码性能
4. 代码安全
综合改进代码示例void FaceAuthController::updateFaceImgContent(void* const context, const DA_img* img)
{
// 1. 安全性检查:增加对 img 和 data 的检查
if (!context || !img || !img->data) {
qWarning() << "Invalid context or image data";
return;
}
// 获取 controller
auto controller = static_cast<FaceAuthController*>(context);
// 2. 健壮性:检查图像尺寸
if (img->width <= 0 || img->height <= 0) {
qWarning() << "Invalid image dimensions";
return;
}
// 假设输入必须是 RGB888,如果不是则需要转换,这里保持原逻辑但增加注释说明假设条件
QImage im((uchar *)img->data, img->width, img->height, QImage::Format_RGB888);
if (im.isNull()) {
qWarning() << "Failed to construct QImage from data";
return;
}
// 3. 性能与逻辑优化:直接使用 QImage 进行操作可能比 QPixmap 稍快,且便于处理像素
// 先进行裁剪
int sourceSize = qMin(img->width, img->height);
int offsetX = (img->width - sourceSize) / 2;
int offsetY = (img->height - sourceSize) / 2;
// copy() 会进行深拷贝,确保数据独立
QImage croppedImage = im.copy(offsetX, offsetY, sourceSize, sourceSize);
// 缩放至目标尺寸
QImage scaledImage = croppedImage.scaled(Faceimg_SIZE, Faceimg_SIZE,
Qt::IgnoreAspectRatio, // 因为已经裁剪为正方形,这里可以直接 IgnoreAspectRatio
Qt::SmoothTransformation);
// 4. 绘制圆形遮罩
QPixmap pix(Faceimg_SIZE, Faceimg_SIZE);
pix.fill(Qt::transparent);
{
QPainter painter(&pix);
painter.setRenderHint(QPainter::Antialiasing); // 增加抗锯齿提示,提升边缘质量
QPainterPath path;
path.addEllipse(0, 0, Faceimg_SIZE, Faceimg_SIZE);
painter.setClipPath(path);
// 这里将 QImage 转换为 QPixmap 绘制,或者直接 drawImage
painter.drawPixmap(0, 0, QPixmap::fromImage(scaledImage));
}
QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
// 保存为 PNG 格式通常比 JPG 更适合这种带透明度的图形,且质量无损
pix.save(&buffer, "PNG");
controller->m_faceImgContent = "data:image/png;base64," + buffer.data().toBase64();
emit controller->faceImgContentChanged();
}总结这段代码的主要改进点在于增加了输入参数的空指针和尺寸检查,提升了程序的健壮性和安全性。同时,通过明确 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: mhduiy, robertkill The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
|
This pr force merged! (status: blocked) |
Log: Fixed face image display issue where images appeared stretched or distorted
fix: 修复人脸图像拉伸变形问题
Log: 修复了人脸图像显示时出现拉伸或变形的问题
PMS: BUG-304729
Summary by Sourcery
Bug Fixes: