【问题标题】:Draw autofocus rectangle in preview using camera2 android使用camera2 android在预览中绘制自动对焦矩形
【发布时间】:2016-11-05 18:10:02
【问题描述】:

我正在使用 Android 的 Camera2 api。我能够捕获图像并保存它。我正在使用autofocus 模式来对焦和捕捉图像。

captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

captureBuilder 在哪里

private CaptureRequest.Builder captureBuilder;

我想在相机预览的自动对焦区域中显示一个矩形或圆形,就像默认相机应用程序中发生的那样。例如这里预览中间的矩形。

我看过this,但我不想在预览中触摸任何地方。

我搜索了很多示例,但大多数示例说明了已弃用的 Camera 类的使用,在 Camera2 api 上没有什么用处。如何使用新的相机 api 实现这个焦点矩形?

【问题讨论】:

标签: android preview autofocus camera2 drawrectangle


【解决方案1】:

看起来您只是想要一个位于预览中心的静态矩形。

您可以通过 xml 布局将图像添加到布局中。

以 Camera2Basic 示例 (https://github.com/googlesamples/android-Camera2Basic) 并将其添加到此,下面的示例在侧面添加了矩形、一些文本和控制按钮(用于横向):

<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/control"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimary"
        android:orientation="vertical">

        <Space
            android:layout_width="1dp"
            android:layout_height="0dp"
            android:layout_weight="1" >
        </Space>

        <TextView
            android:id="@+id/label1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/label1_text" />

        <Space
            android:layout_width="1dp"
            android:layout_height="0dp"
            android:layout_weight="1" >
        </Space>

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

        <Space
            android:layout_width="1dp"
            android:layout_height="0dp"
            android:layout_weight="1" >
        </Space>

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

        <Space
            android:layout_width="1dp"
            android:layout_height="0dp"
            android:layout_weight="1" >
        </Space>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/tileview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@+id/control"
        android:background="@android:color/transparent" >

        <ImageView
            android:id="@+id/autofocus_rectangle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/autofocus_landscape_Image"
            android:layout_centerInParent="true" />

        <TextView
            android:id="@+id/bottom_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_alignParentBottom="true"
            android:layout_margin="10sp"
            android:background="@android:color/holo_blue_dark"
            android:textSize="15sp"
            android:padding="5sp"
            android:text="" />

    </RelativeLayout>

</RelativeLayout>

【讨论】:

  • 通过 XML 添加该矩形后,如何仅获取该矩形部分图像?不是完整的图像
猜你喜欢
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 2015-03-26
相关资源
最近更新 更多