Android SDK-camera2-在TextureView上绘制矩形 [英] Android SDK - camera2 - Draw rectangle over TextureView

查看:744
本文介绍了Android SDK-camera2-在TextureView上绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手,但我发现很难在camera2 api上找到好的示例.

Im new to android development, and I'm finding it hard to find good examples on the camera2 api.

在处理大多数问题时,我的工作进展缓慢,但是在这一问题上,我陷入了困境. 在默认相机中,当您触摸屏幕进行对焦时,它会暂时显示对焦区域的矩形.我想做类似的事情(或者在这种情况下,从一开始就完全一样,以便我能弄清楚).

Im working my way slowly through most issues, but on this one I am stuck. In the default camera, when you touch the screen to focus, it shows a rectangle of the focus area for a moment. I want to do something similar (Or in this case, the exact same thing to start off with so i can figure it out).

我读到某处(我认为SDK文档中的TextureView页面),您在将其用作相机预览时无法在Textureview上绘制-并调用lock方法将返回null而不是画布.

I read somewhere (I think the TextureView page in the SDK docs) that you cant draw on a textureview while its being used as a camera preview - and calling the lock method will return null rather than a canvas.

我在网上找到了它: https://github.com/commonsguy/vidtry/ 但是我不能让它工作.我或者收到错误消息说我的主视图无法转换为可绘制视图,反之亦然-或者我的可绘制视图在顶部并使屏幕变黑-或其在底部并不能响应触摸事件(并试图强制上方视图中的performClick会导致崩溃.)

I found this online: https://github.com/commonsguy/vidtry/ But i cant get it to work. I either get errors saying my my main view cant be cast to my drawable view, or vice versa - Or my drawable view is on top and makes the screen black - Or its on the bottom and wont respond to touch events (and trying to force the performClick from the view above it casues crashes.)

我被卡住了!谁能给我一个解释或示例,说明如何在事件位置上绘制几秒钟的矩形?

Im stuck! Can anyone give me an explanation or example of how i can draw my rectangle over the event position for a few sconds?

谢谢!

推荐答案

  1. 首先,对于camera2 api android示例,有开源的google示例代码. https://github.com/googlesamples/android-Camera2Basic
  2. 第二,对于您要绘制矩形(触摸以聚焦)的部分,请按照以下步骤操作-
    • 创建一个扩展SurfaceView的自定义类.
    • 在此自定义类中调用onTouchListener方法以检测手指坐标并使用android中的paint类绘制矩形. -在显示来自camera2的预览的TextureView上方添加此自定义类.
    • 将自定义视图透明化.
    • 当您在设备屏幕上的某个位置触摸时,将调用onTouchListener,它将在您添加在相机预览上方的自定义视图的画布上绘制.
    • 最后,清除画布,这样就不必在自定义视图中继续添加矩形,因为在某个时间点只需要一个矩形即可.
    • 此外,如果一段时间不触摸矩形,该矩形也应消失.使用自定义视图中的处理程序执行此操作.
  1. First, for the camera2 api android example there is open source google sample code. https://github.com/googlesamples/android-Camera2Basic
  2. Second, for the part where you want to draw a rectangle(touch to focus), follow these steps -
    • Create a custom class extending SurfaceView.
    • Call the onTouchListener method in this custom class to detect finger coordinates and draw your rectangle using paint class in android. -Add this custom class above your TextureView that is displaying preview from camera2.
    • Turn the custom view transparent.
    • When you touch at some point on device screen then onTouchListener will be called which will draw on the canvas of custom view you added above you camera preview.
    • Finally, clear your canvas so you don't keep adding rectangles to the custom view since you only need one rect at one point of time.
    • Also, if not touched for some time the rectangle should disappear. Do this using a handler from custom view.

我正在为您提供一些经过测试的代码.希望对您有帮助.祝你好运.

I am giving you some tested code to do this. I hope it helps you. BEST OF LUCK.

 private class CustomView extends SurfaceView {

    private final Paint paint;
    private final SurfaceHolder mHolder;
    private final Context context;

    public CustomView(Camera2BasicFragment context) {
        super(context.getActivity().getBaseContext());
        mHolder = getHolder();
        mHolder.setFormat(PixelFormat.TRANSPARENT);
        this.context = context.getActivity().getBaseContext();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            invalidate();
            if (mHolder.getSurface().isValid()) {
                final Canvas canvas = mHolder.lockCanvas();
                Log.d("touch", "touchReceived by camera");
                if (canvas != null) {
                    Log.d("touch", "touchReceived CANVAS STILL Not Null");
                    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                    canvas.drawColor(Color.TRANSPARENT);
                    canvas.drawCircle(event.getX(), event.getY(), 100, paint);
                    mHolder.unlockCanvasAndPost(canvas);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Canvas canvas1 = mHolder.lockCanvas();
                            if(canvas1 !=null){
                                canvas1.drawColor(0, PorterDuff.Mode.CLEAR);
                                mHolder.unlockCanvasAndPost(canvas1);
                            }

                        }
                    }, 1000);

                }
                mHolder.unlockCanvasAndPost(canvas);
            }
        }

        return false;
    }
}

这篇关于Android SDK-camera2-在TextureView上绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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