用于在相机预览上绘制的 android Camera2 API + TextureView 叠加层 [英] android Camera2 API + TextureView overlay for drawing on camera preview

查看:57
本文介绍了用于在相机预览上绘制的 android Camera2 API + TextureView 叠加层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我需要叠加camera2预览并通过在顶部分层透明叠加在预览视频图像上绘制一个矩形.我从这里开始使用基本的 Camera2 代码:https://github.com/googlesamples/android-Camera2Basic

So, I need to overlay the camera2 preview and draw a rectangle on the preview video image by layering a transparent overlay on top. I started with a basic Camera2 code here: https://github.com/googlesamples/android-Camera2Basic

以上使用 TextureView 进行相机预览.

the above use TextureView for camera preview.

接下来,我将以下类添加到项目中

Next, I added the following class to project

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", "touchRecieved by camera");
                if (canvas != null) {
                    Log.d("touch", "touchRecieved 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;
    }
}

我需要一些帮助来完成这项工作.. 显然新类尚未使用.我还需要更新覆盖 xml 以在相机预览顶部添加第二个透明 TextureView.这是我的原始布局:如果有人能告诉我如何使新类工作,并告诉我要添加到布局中的内容,将不胜感激.

I need some help in making this work.. Obviously the new class is not used yet. I also need to update the overlay xml to add a second transparent TextureView on top of the camera preview one. Here is my original layout: Would be very appreciated if anyone can tell me how to make the new class work, and tell me what to add to the layout.

这里是 fragment_camera2_basic.xml

here is fragment_camera2_basic.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.android.camera2basic.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <FrameLayout
        android:id="@+id/control"
        android:layout_width="match_parent"
        android:layout_height="112dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@color/control_background">

        <Button
            android:id="@+id/picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/picture" />

        <ImageButton
            android:id="@+id/info"
            android:contentDescription="@string/description_info"
            style="@android:style/Widget.Material.Light.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:padding="20dp"
            android:src="@drawable/ic_action_info" />

    </FrameLayout>

</RelativeLayout>

和activity_camera.xml

and activity_camera.xml

<?xml version="1.0" encoding="utf-8"?><!--

-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    tools:context="com.example.android.camera2basic.CameraActivity" />

推荐答案

添加

<LinearLayout
    android:id="@+id/surface"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" />

在片段内.我在 AutoFitTextureView 和 FrameLayout 周围添加了一个 RelativeLayout,但不确定是否需要.

Within the fragment. I've added a RelativeLayout around AutoFitTextureView and FrameLayout, but not sure if that is needed.

将 onCreateView 更改为

Change onCreateView to

View view = inflater.inflate(R.layout.fragment_camera2_basic, container, false);

LinearLayout surface = (LinearLayout)view.findViewById(R.id.surface);
surface.addView(new CustomView(this));

return view;

这篇关于用于在相机预览上绘制的 android Camera2 API + TextureView 叠加层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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