gpt4 book ai didi

android - 我如何处理预览和人脸检测的旋转问题

转载 作者:行者123 更新时间:2023-11-29 00:26:38 26 4
gpt4 key购买 nike

我有一些问题困扰了我很长一段时间。

我得到了显示实时预览的自定义相机应用程序。此外,我正在使用 FaceDetection 来更好地关注人脸。当我在图库中查看我拍摄的照片时,我可以正确地看到矩形。下一步是使 FaceDetection-Rectangle 在实时预览中可见。所以我决定使用从 Preview-Rectangle 获取坐标的 Canvas ,并将它们转换为 Canvas 可以使用的坐标。

我的问题是我必须将预览旋转 90 度才能正确显示预览。因此,当我在绘制之前也旋转 Canvas 的 View 时,矩形会正确显示并移动右轴。但是矩形可以左右移出屏幕,并且只使用大约一半的可用高度。我认为旋转会引起麻烦,但我无法解决问题。有人有想法吗?

屏幕截图(我添加了紫色线以显示红色矩形无法到达的顶部/底部): enter image description here

预览:

        mCamera = Camera.open();
mCamera.getParameters();
mCamera.setDisplayOrientation(90);
mCamera.setFaceDetectionListener(new FaceDetectionListener() {

@Override
public void onFaceDetection(Face[] faces, Camera camera) {

if(mDraw != null) {
mDraw.update(f.rect, f);
}
}
}
}
});
mCamera.startFaceDetection();
}

private DrawOnTop mDraw = null;
public void setDrawOnTop(DrawOnTop d) {
this.mDraw = d;
}

DrawOnTop:

public DrawOnTop(Context context) {
super(context);
myColor = new Paint();

myColor.setStyle(Paint.Style.STROKE);
myColor.setColor(Color.RED);
}

@Override
protected void onDraw(Canvas canvas) {
rect.set((((rect.left+1000)*1000) / WIDTH_DIVISOR),(((rect.top+1000)*1000) / HEIGHT_DIVISOR),(((rect.right+1000)*1000) / WIDTH_DIVISOR),(((rect.bottom+1000)*1000) / HEIGHT_DIVISOR ));
setRotation(90);
canvas.drawRect(rect, myColor);
}

public void update(Rect rect, Face face) {
this.invalidate();
this.rect = rect;
this.face = face;
}

-------------------------------------------- ------------------------------------------


编辑:我得出的结论是,这是一个罕见但已知的错误,并且到目前为止没有其他解决方案,只能强制应用程序进入横向模式。工作正常,但尺寸看起来有点拉伸(stretch)或固定,具体取决于用户操作的视角。

最佳答案

编辑:我误读了问题并谈到了错误的矩形。这就是我的意思:

基本上,您只需要缩放紫色矩形即可。找到 ut 的定义位置,然后将其放到 Canvas 上并执行以下操作:

float screenWidth = /*get the width of your screen here*/;
float xScale = rect.width / screenWidth;

float screenHeight = /*get the height of your screen here*/;
float yScale = rect.height / screenWidth;

canvas.setScaleX(xScale);
canvas.setScaleY(yScale);

这样,坐标将被正确翻译。

第二次编辑(回应您的评论):如果您愿意,您也可以对 View 执行此操作。玩得开心。

关于android - 我如何处理预览和人脸检测的旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18875260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com