Save screenshot from CameraView

hni878hni878 USMember ✭✭

I'm using the CameraView control from XLabs to build a custom view, works fine except when I save a screenshot the image is totally transparent, like the photo doesn't exit. I'm using the follow code:

PhotoPage.xaml:


<Grid.RowDefinitions>



</Grid.RowDefinitions>

    <Frame Grid.Row="1">
        <RelativeLayout>
            <controls:CameraView Camera="{Binding Camera}" x:Name="Photo" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height}"/>
        </RelativeLayout>
    </Frame>

    </Grid>

Answers

  • hni878hni878 USMember ✭✭

    PhotoPage.xaml.cs:

    //camera is Android Dependency
    ICamera camera = DependencyService.Get ();

    byte[] file = camera.GetFoto(Photo); // Photo is controls:CameraView
    camera.SavePhoto ("name", file);

    Camera.cs

    public byte[] GetFoto (Xamarin.Forms.View view)
    {
    Android.Views.View v = GetRenderer (view).ViewGroup;

    Bitmap image = Bitmap.CreateBitmap (v.Width, v.Height, Bitmap.Config.Argb8888);
    Canvas canvas = new Canvas (image);
    v.Draw (canvas);
    
    byte[] bytes;
    using (var stream = new MemoryStream())
    {
        imagem.Compress(Bitmap.CompressFormat.Png, 0, stream);
        bytes = stream.ToArray();
    }
    
    return bytes;
    

    }

    public void SavePhoto(string photoUId, byte[] file)
    {
    string path = System.IO.Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), String.Concat(photoUId,".png"));
    File.WriteAllBytes (path, file);
    }

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Why would you screenshot the camera view instead of just taking a picture?

  • hni878hni878 USMember ✭✭

    Because is custom view, here is recommend to do this way, do you know other way to do that?

  • adamkempadamkemp USInsider, Developer Group Leader mod

    I would suggest that you add the missing feature rather than trying this strange workaround. The actual native camera view is often not a thing you can take a screenshot of.

  • YannRoseauYannRoseau FRMember

    Hi
    I have a problem on line

    IVisualElementRenderer iver = RendererFactory.GetRenderer(view);

    I have an Java exception.
    My CameraView (of Xamarin.Forms) is in my crossplatform projet, but I want retrieve an image of it in my java projet.
    Have you a solution ?
    I already view [https://forums.xamarin.com/discussion/33022/xamarin-form-view-to-native-view-ex-android-view](this post), but I don't work.

  • hni878hni878 USMember ✭✭

    @Yann Roseau I gave up XLabs, I found this solution and worked fine

    https://github.com/yas-mnkornym/XamarinFormsCameraPreview

Sign In or Register to comment.