简体   繁体   English

如何使用 Android CameraX 显示相机焦点矩形

[英]How to show camera focus rectangle with Android CameraX

I use android camerax in my app.我在我的应用程序中使用 android camerax。

The focus-on-tap is implemented as in this answer: https://stackoverflow.com/a/60095886/978067水龙头的焦点是在这个答案中实现的: https://stackoverflow.com/a/60095886/978067

            cameraPreview.setOnTouchListener((view, event)  -> {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        return true;
                    case MotionEvent.ACTION_UP:
                        MeteringPoint point = cameraPreview.getMeteringPointFactory().createPoint(event.getX(), event.getY());
                        FocusMeteringAction action = new FocusMeteringAction.Builder(point).build();
                        camera.getCameraControl().startFocusAndMetering(action);

                        // HOW TO SHOW RECTANGLE SIGHT HERE? Thanx!

                        return true;
                    default:
                        return false;
                }
            });

But I need to show rectangle sight around focus point (as in standard camera app)但我需要在焦点周围显示矩形视线(如标准相机应用程序中)

Have any ideas how to do it?有什么想法吗?

Using CameraX the tap to focus is done using the touch point - x, y coordinates.使用 CameraX,点击对焦是使用触摸点 - x、y 坐标完成的。 You can set the focus rectangle x and y as the motion event x and y.您可以将焦点矩形 x 和 y 设置为运动事件 x 和 y。 The focus rectangle should be a custom view that you can create.焦点矩形应该是您可以创建的自定义视图。

/** Represents the x coordinate of the tap event */
val x = motionEvent.x
/** Represents the y coordinate of the tap event */
val y = motionEvent.y
// Setting the focus rectangle view x coordinate as the motion event x coordinate
focusRectangleView.x = x
// Setting the focus rectangle view y coordinate as the motion event y coordinate
focusRectangleView.y = y

This is how you can show the focus rectangle view where the tap occurred, and also simply change the view visibility whenever you want to show it or hide it.这是您可以显示点击发生的焦点矩形视图的方式,并且还可以在您想要显示或隐藏它时简单地更改视图可见性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM