1

I'm looking for a working sample Xamarin.Forms solution for Visual Studio that will show how to use a DependencyService interface to access camera controls for Android and iOS devices. I am calling the ability to take a picture with a button click event in the Portable Class Library.

I need to be able to restrict use of certain camera controls on the device while allowing access to other camera controls to users that will be using an app I am working on before they are able to take a picture and save it.

I've tried several solutions including the one found at: Camera access with Xamarin.Forms but it did not work.

Community
  • 1
  • 1
  • How did you try the solution that you found? Can you show some error message of your project and some key code? – Mike Ma Dec 09 '16 at 07:44

1 Answers1

1

The Media plugin for Xamarin is a good example of accessing built-in camera features of a device.

It sounds like your requirement is slightly different. You need more control over the UI of the camera page. You might consider making your own custom camera page instead. There is a great example of this here. It implements a camera preview page where you create the UI controls. This uses custom renderers.

// The control used in shared code
public class CameraPreview : View
{
  public static readonly BindableProperty CameraProperty = BindableProperty.Create (
    propertyName: "Camera",
    returnType: typeof(CameraOptions),
    declaringType: typeof(CameraPreview),
    defaultValue: CameraOptions.Rear);

  public CameraOptions Camera {
    get { return (CameraOptions)GetValue (CameraProperty); }
    set { SetValue (CameraProperty, value); }
  }
}

// Renderer for iOS, platform specific project
[assembly: ExportRenderer (typeof(CameraPreview), 
typeof(CameraPreviewRenderer))]
namespace CustomRenderer.iOS
{
    public class CameraPreviewRenderer : ViewRenderer<CameraPreview, UICameraPreview>
    {
        UICameraPreview uiCameraPreview;

        protected override void OnElementChanged (ElementChangedEventArgs<CameraPreview> e)
        {
            base.OnElementChanged (e);

            if (Control == null) {
                uiCameraPreview = new UICameraPreview (e.NewElement.Camera);
                SetNativeControl (uiCameraPreview);
            }
            if (e.OldElement != null) {
                // Unsubscribe
                uiCameraPreview.Tapped -= OnCameraPreviewTapped;
            }
            if (e.NewElement != null) {
                // Subscribe
                uiCameraPreview.Tapped += OnCameraPreviewTapped;
            }
        }

        void OnCameraPreviewTapped (object sender, EventArgs e)
        {
            if (uiCameraPreview.IsPreviewing) {
                uiCameraPreview.CaptureSession.StopRunning ();
                uiCameraPreview.IsPreviewing = false;
            } else {
                uiCameraPreview.CaptureSession.StartRunning ();
                uiCameraPreview.IsPreviewing = true;
            }
        }
        ...
    }
}

Take a look at the links above for the other platforms. There is another sample of making a custom camera page and capturing the images in the Moments sample. You can see in each renderer that a button is used for handling capturing an image. This gives you better control on what the user can/can't do.

If you include more specific information on what you need to limit in your Camera I might be able to edit this with more specifics.

therealjohn
  • 2,378
  • 3
  • 23
  • 39
  • Hi John! Thank you for your reply! I decided to go with a custom page I found at [Full Camera Page](https://github.com/ThatCSharpGuy/Forms-FullCameraPage). I am still needing help with adding a slider type control in the android project for the camera to zoom, setting the aspect ratio to 4:3 and making the camera take picture in landscape mode only. – Madam Matrix Dec 14 '16 at 21:28