Reputation: 11
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.
Upvotes: 1
Views: 977
Reputation: 2398
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.
Upvotes: 1