2017-10-17 92 views
-1

我正試圖在Xamarin上使用Android構建相機應用程序。我想要做的是當用戶點擊「拍攝圖像」按鈕時,相機會自動將後置攝像頭切換到前置攝像頭並拍攝圖像,這意味着「拍攝圖像」按鈕將做2件事:切換相機和捕捉圖像在同一時間。如何添加更多的工作爲1相機應用程序的「拍照」按鈕點擊?

我是新來的Xamarin和開發Android應用程序。我已經搜索了很多教程來構建相機應用程序,但它們看起來很簡單,當用戶點擊「拍攝圖像」按鈕時,我看不到任何覆蓋功能。在這裏我的主要活動代碼(只是爲了建立簡單的攝像頭應用程序):

ImageView imageView; 

    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     var btnCamera = FindViewById<Button>(Resource.Id.btnCamera); 
     imageView = FindViewById<ImageView>(Resource.Id.imageView); 

     btnCamera.Click += BtnCamera_Click; 

    } 

    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) 
    { 
     base.OnActivityResult(requestCode, resultCode, data); 
     Bitmap bitmap = (Bitmap)data.Extras.Get("data"); 
     imageView.SetImageBitmap(bitmap); 
    } 
    // When user tap on the button, open camra app 
    private void BtnCamera_Click(object sender, System.EventArgs e) 
    { 
     Intent intent = new Intent(MediaStore.ActionImageCapture); 
     StartActivityForResult(intent, 0); 
    } 

任何想法會有所幫助,非常感謝。

回答

1

Intent intent = new Intent(MediaStore.ActionImageCapture);

此方法使用系統的相機應用程序,您不能使用此方法更改此處的前/後相機。

您可以閱讀本教程:Display a stream from the camera,它展示瞭如何使用Android.Hardware.Camera構建您自己的相機應用程序。

要將Button添加到視圖,以便用戶可以拍照,你可能會喜歡這個創建例如你的相機預覽視圖:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextureView 
     android:id="@+id/textureView" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" /> 

    <Button 
     android:id="@+id/takephotoBtn" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="take photo" /> 
</RelativeLayout> 

您可以通過Android.Hardware.Camera.Open Method改變相機的前/後攝像頭。

請參閱SO上的此主題:Android: Switch camera when button clicked 。 Xamarin Android的代碼可能與Java中的代碼相同。

這意味着「拍攝圖像」按鈕將做2件事:同時切換攝像頭和拍攝圖像。

順便說一句,我不認爲這是一個好主意,讓您的應用程序有線。你爲什麼想這樣做。

相關問題