-1

I'm trying to build a camera app on Android using Xamarin. What I am trying to do is when user hits the "take image" button, camera will automatically switch the back camera to front camera and capture the image, which means the "take image" button will do 2 things: switch camera and capture image at the same time.

I am new to Xamarin and developing Android app. I've searched for many tutorials to build camera app, but they seem to be simple and I do not see any override function when user hit the "take image" button. Here my code in Main activity (just to build the simple camera app):

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);
    }

Any idea would help, thanks a lot.

Blurie
  • 148
  • 1
  • 2
  • 13

1 Answers1

1

Intent intent = new Intent(MediaStore.ActionImageCapture);

This method uses the system's camera app, you cannot change the the front/ back camera here using this method.

You can read this tutorial: Display a stream from the camera, it shows how to use Android.Hardware.Camera to build your own camera app.

To add a Button to the view so users can take photo, you may create your camera preview view for example like this:

<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>

You can change the Camera's front/ back camera by Android.Hardware.Camera.Open Method.

Please refer to this thread on SO: Android: Switch camera when button clicked . The code for Xamarin Android is likely the same as it is in Java.

which means the "take image" button will do 2 things: switch camera and capture image at the same time.

By the way, I don't think it's a good idea, makes your app wired. Why you want to do this.

Grace Feng
  • 16,564
  • 2
  • 22
  • 45