![](/img/trans.png)
[英]Android SDK - camera2 - Draw rectangle over TextureView
[英]Draw rectangle over camera2 preview android
我正在尝试使用此链接中的基本 Camera2 在 camera2 预览上绘制一个矩形: https : //github.com/googlesamples/android-Camera2Basic
我在论坛上遵循了这个问题的答案:
用于在相机预览上绘制的 android Camera2 API + TextureView 叠加层
但我无法让它工作。 如果您能帮我弄清楚如何解决我的问题,我将不胜感激。 这是用于绘制矩形的类: Rectangle.java
package com.example.android.camera2basic;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class Rectangle extends View {
Paint paint = new Paint();
public Rectangle(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.FILL);
Rect rect = new Rect(20, 56, 200, 112);
canvas.drawRect(rect, paint );
}
}
相机activity_camera.xml 的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" />
</FrameLayout>
这是我将矩形添加到相机预览Camera2BasicFragment.java的类中使用的方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera2_basic, container,
false);
LinearLayout linearLayout = (LinearLayout)
view.findViewById(R.id.surface);
linearLayout.addView(new Rectangle(getActivity()));
return view;
}
最后,应用程序在启动时崩溃后出现错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.camera2basic/com.example.android.camera2basic.CameraActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
at com.example.android.camera2basic.Camera2BasicFragment.onCreateView(Camera2BasicFragment.java:431)
at android.app.Fragment.performCreateView(Fragment.java:2361)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1000)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1178)
at android.app.BackStackRecord.run(BackStackRecord.java:815)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1585)
at android.app.FragmentController.execPendingActions(FragmentController.java:371)
at android.app.Activity.performStart(Activity.java:6929)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2756)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
所以感谢@cricket_007 评论,我发现我的错误。 我在错误的 xml 文件中初始化了 LinearLayout。 它应该在fragment_camera2_basic.xml
此外,要设法在相机预览上获得矩形,您应该在此代码之后添加它:
<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" />
所以最终的 xml 文件看起来像这个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" />
<LinearLayout
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<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>
PS:要设法使矩形未填充,请在Rectangle.class 中使用它: paint.setStyle(Paint.Style.STROKE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.