我如何处理旋转问题与预览&面部检测 [英] How can i handle the rotation issue with Preview & FaceDetection

查看:152
本文介绍了我如何处理旋转问题与预览&面部检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个自定义的相机应用程序,显示一个实时预览。 Aswell我使用FaceDetection更好地关注人民的面孔。当我检查我的图片在画廊我可以看到矩形正确。下一步是使FaceDetection - 矩形在实时预览中可见。所以我决定使用一个canvas,从Preview-Rectangle获取坐标,并将它们转换为可以被canvas使用的坐标。



我的问题是,将预览旋转90度,以正确显示预览。因此,当我在绘制画布之前还旋转视图时,矩形显示正确,并移动右轴。但矩形可以向左和向右移出屏幕,只使用大约一半的可用高度。我认为旋转引起麻烦,但我不能把事情正确。有人得到了一个想法?



截图(我添加了紫色线,显示红色矩形无法到达的顶部/底部):



预览:

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

@Override
public void onFaceDetection(Face [] faces,相机摄像头){

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

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

DrawOnTop: b

  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;
}



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






EDIT:
i得出的结论是,这是一个罕见但已知的错误,并且目前还没有其他解决方案,但强制应用程序为横向模式。

解决方案

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



基本上,你只需要缩放紫色矩形。找到它在哪里定义,然后把它放到画布上,并执行以下操作:

  float screenWidth = / *的屏幕在这里* /; 
float xScale = rect.width / screenWidth;

float screenHeight = / *获取屏幕的高度* /;
float yScale = rect.height / screenWidth;

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

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



第二个编辑(回应您的评论):如果您愿意,您也可以使用检视。
有乐趣。


i got some issue nagging me for quite some time now.

I got my custom Camera-App that shows a live Preview. Aswell i am using the FaceDetection for getting a better focus on peoples faces. When i check my taken Pictures in Gallery i can see the Rectangle correctly. The next step is to make the FaceDetection-Rectangle visible in the Live-Preview. So i decided to use a canvas that gets the coordinates from the Preview-Rectangle and transform them to coordinates that can be used by the canvas.

My problem is that i had to rotate the Preview by 90degrees that it shows the Preview correctly. So when i also rotate the View of the canvas before i draw it, the rectangle shows correctly and moves the right axis aswell. BUT the rectangle can move out of screen on left and right, and only uses about half of the available height. I assume that the rotating causes the trouble, but i can't manage to put things right. Someone got an idea?

Screenshot (i added purple lines to show the top/bottom-parts that cant be reached by red rectangle):

Preview:

        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;
}

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


EDIT: i came to the conclusion that this is a rare but known bug and that there is so far no other solution but forcing the application to landscape-mode. Works ok, but dimensions look a bit stretched or clinched depending on which perspective the user is operating.

解决方案

EDIT: I misread the question and talked about the wrong rectangle. This is what i meant:

Basically, you just need to scale the purple rectangle. Find ut where it is defined, then put it onto a canvas and do the following:

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);

This way, the coordinates will be translated properly.

SECOND EDIT (in response to your comment): You can also do this with views, if you like. Have fun.

这篇关于我如何处理旋转问题与预览&面部检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆