2016-11-05 332 views
1

我正在使用Android的Camera2 api。我能夠捕捉圖像並保存。我正在使用autofocus模式來聚焦和捕捉圖像。使用camera2在預覽中繪製自動對焦矩形android

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

其中captureBuilder是

private CaptureRequest.Builder captureBuilder; 

我要顯示在自動聚焦區域在相機預覽矩形或圓形,因爲它在默認的攝像頭應用程序發生。對於像預覽中間的矩形這樣的實例。

enter image description here

我見過this但我不想在預覽的任何地方碰到任何東西。

我已經通過大量的例子進行了搜索,但大多數例子說明使用過時的Camera類,在Camera2 api上沒有太大的用處。這個焦點矩形怎麼能用新的相機api實現呢?

+0

檢查這個線程的答案:http://stackoverflow.com/questions/31173476/android-sdk-camera2-draw-rectangle-over-textureview。 – user1035292

回答

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>