简体   繁体   中英

How to take a picture in Xamarin?

I am trying to take a picture using Camera class from Android.Hardware. I am getting the feed and the camera is getting auto-foucs. The problem is that I do not know how to take the photo.

_camera = Camera.Open();
Camera.Parameters param = _camera.GetParameters();
param.FocusMode = Camera.Parameters.FocusModeContinuousPicture;
_camera.SetParameters(param);


var previewSize = _camera.GetParameters().PreviewSize;
_textureView.LayoutParameters =
    new FrameLayout.LayoutParams(previewSize.Width,
        previewSize.Height, GravityFlags.Center);

try
{
    _camera.SetPreviewTexture(surface);
    _camera.StartPreview();
}
catch (Java.IO.IOException ex)
{
    System.Console.WriteLine(ex.Message);
}

// this is the sort of thing TextureView enables
_textureView.Rotation = 90.0f;

This link may help you on this https://developer.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/

But you may use this code for taking picture (TakeAPicture).

private void TakeAPicture (object sender, EventArgs eventArgs)
{
    Intent intent = new Intent (MediaStore.ActionImageCapture);
    App._file = new File (App._dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));
    intent.PutExtra (MediaStore.ExtraOutput, Uri.FromFile (App._file));
    StartActivityForResult (intent, 0);
}

Happy Coding! :)

The TakePicture-method is the one you are looking for. To use this you need to implement some interfaces. Especially the IPictureCallback to receive the picture in OnPictureTaken.

A sample implementation would look like this:

public void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
{
    camera.StopPreview();
    Toast.MakeText(this, "Cheese", ToastLength.Short).Show();
    camera.StartPreview();
}

To prevent that the app hangs you need to stop the preview and (re-)start it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM