如何在 Xamarin 中拍照?

How to take a picture in Xamarin?

我正在尝试使用来自 Android.Hardware 的相机 class 拍照。我正在获取提要,并且相机正在自动对焦。问题是不知道怎么拍照

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

这篇link可能会对你有所帮助https://developer.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/

但是您可以使用此代码拍照(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);
}

祝您编程愉快! :)

TakePicture-method 就是您要找的人。要使用它,您需要实现一些接口。特别是在OnPictureTaken中接收图片的IPictureCallback。

示例实现如下所示:

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

为防止应用程序挂起,您需要停止预览并(重新)启动它。