How can I handle rotation problem with Preview & FaceDetection

I got some kind of problem that rocked me for quite some time.

I got my own Camera-App that shows a live view. How do I use FaceDetection to better focus on people's faces. When I view the captured images in the 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 coordinates from Preview-Rectangle and converts them to coordinates that can be used by the canvas.

My problem is that I had to rotate the Preview 90 degrees in order for it to show the Preview correctly. So when I also rotate the canvas view before drawing it, the rectangle will display correctly and move the right axis. BUT the rectangle can move from the screen to the left and right and only uses half of the available height. I'm guessing the rotation is causing problems, but I can't seem to fix everything correctly. Someone got an idea?

Screenshot (I added purple lines to show the tops / bottoms that cannot be reached by the red rectangle): enter image description here

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've come to the conclusion that this is a rare but known bug and that there is still no solution other than a landscape mode application. Works fine, but the dimensions look a little stretched or pinched depending on which perspective the user is working with.

+1


source to share


1 answer


EDIT: I misunderstood the question and talked about the wrong rectangle. That's what I meant:

Basically, you just need to scale the purple rectangle. Find ut where it is defined, then put it on the canvas and follow these steps:

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 correctly.

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

+1


source







All Articles