dimonovdd / ImageFromXamarinUI

Extension methods for capturing images from UI
MIT License
37 stars 3 forks source link

CaptureImageAsync used on an OpenGLView produces blank results #3

Open pfafft33 opened 3 years ago

pfafft33 commented 3 years ago

I've got a Xamarin.Forms / Android app that displays graphics in an OpenGLView. I'm trying to use the NuGet "ImageFromXamarinUI" to capture an instantaneous screenshot of the graphics at a point in time, and save it to a file.

My "capture" code currently looks like:

                Stream imageStream = await myPage.myOpenGLView.CaptureImageAsync();

                string myCaptureFilename = $"MLM - {ParamsEqSetName} - {System.DateTime.Now.ToString("yyyy_MM_d_HH_mm")}";

                using (Stream stream = File.Open($"/storage/emulated/0/Pictures/{myCaptureFilename}", FileMode.Create))
                {
                    imageStream.CopyTo(stream);
                    stream.Close();
                    stream.Dispose();
                }

The file is being created successfully. But, when I try to open it as an image with the Photos app, I get a blank, black screen.

I expect to see the graphics that were being displayed at the moment that I executed the capture code. Instead, I get a blank, black screen.

Any help would be appreciated.

dimonovdd commented 3 years ago

@pfafft33 Hi Could you please send a sample project?

dimonovdd commented 3 years ago

Does your code work correctly with other views? (ContentView, StackLayout, Button and etc.)

pfafft33 commented 3 years ago

It works correctly with a Button view. So, the issue seems to be specific to OpenGLView.

dimonovdd commented 3 years ago

I added more samples to this project. Yes, there is this problem with OpenGLView on iOS and android. I will try to fix this issue.

pfafft33 commented 3 years ago

Thanks for the update! Let me know if there is anything that I can do to help with fixing this issue. This functionality will add nicely to my app, when it is working.

dimonovdd commented 3 years ago

@pfafft33 Hi. I'm currently reading about capturing an image from the OpenGLView. It looks scary, I'll have to add extra dependencies to the plugin. Maybe right way would be to create a new plugin for this issue. Something like that the names ImageFromXamarinUI.OpenGLView

https://stackoverflow.com/questions/5514149/capture-screen-of-glsurfaceview-to-bitmap

pfafft33 commented 3 years ago

Hi:

I could see creating a new plugin might create less risk to your current code. But, you might also end up duplicating a fair amount of work.

Let me know what you decide. Give me a shout-out any time, to bounce ideas around.

Sincerely,

Terence A. Pfaff

TAP Brands LLC

From: Dimov Dima notifications@github.com Sent: Sunday, February 28, 2021 3:35 PM To: dimonovdd/ImageFromXamarinUI ImageFromXamarinUI@noreply.github.com Cc: Terence A. Pfaff terence.pfaff@gmail.com; Mention mention@noreply.github.com Subject: Re: [dimonovdd/ImageFromXamarinUI] CaptureImageAsync used on an OpenGLView produces blank results (#3)

@pfafft33 https://github.com/pfafft33 Hi. I'm currently reading about capturing an image from the OpenGLView. It looks scary, I'll have to add extra dependencies to the plugin. Maybe right way would be to create a new plugin for this issue. Something like that the names ImageFromXamarinUI.OpenGLView

https://stackoverflow.com/questions/5514149/capture-screen-of-glsurfaceview-to-bitmap

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dimonovdd/ImageFromXamarinUI/issues/3#issuecomment-787527086 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ALKPGROFFX4KCV3QP7ER3JLTBKZIFANCNFSM4YGOSPCA .

dimonovdd commented 3 years ago

How do you add a reference to OpenGLView in your android project?

<ItemGroup>
  <Reference Include="Mono.Android" />
  <Reference Include="OpenTK-1.0" />
</ItemGroup>
pfafft33 commented 3 years ago

OpenGLView is a Xamarin Forms construct.

You should start off by creating your initial project using the Xamarin.Forms template in the Add New Project dialog choose: “Mobile App (Xamarin.Forms)”.

From: Dimov Dima notifications@github.com Sent: Monday, March 01, 2021 2:06 PM To: dimonovdd/ImageFromXamarinUI ImageFromXamarinUI@noreply.github.com Cc: Terence A. Pfaff terence.pfaff@gmail.com; Mention mention@noreply.github.com Subject: Re: [dimonovdd/ImageFromXamarinUI] CaptureImageAsync used on an OpenGLView produces blank results (#3)

How do you add a reference to OpenGLView in your android project?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dimonovdd/ImageFromXamarinUI/issues/3#issuecomment-788234063 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ALKPGROZMWAQPLCKXEDVFXDTBPXTDANCNFSM4YGOSPCA .

dimonovdd commented 3 years ago

This is understandable, but you still need platform-dependent code to draw graphics. Is my sample different from yours? Can you send me a small sample project where you use the OpenGL? OpenGLViewSharedCode

pfafft33 commented 3 years ago

I have Mono.Android referenced in my Android project, and OpenTK-1.0 referenced in my Xamarin shared-code project.

From: Dimov Dima notifications@github.com Sent: Monday, March 01, 2021 2:16 PM To: dimonovdd/ImageFromXamarinUI ImageFromXamarinUI@noreply.github.com Cc: Terence A. Pfaff terence.pfaff@gmail.com; Mention mention@noreply.github.com Subject: Re: [dimonovdd/ImageFromXamarinUI] CaptureImageAsync used on an OpenGLView produces blank results (#3)

This is understandable, but you still need platform-dependent code to draw graphics. Is my sample different from yours? Can you send me a small sample project where you use the OpenGL? OpenGLViewSharedCode https://github.com/dimonovdd/ImageFromXamarinUI/blob/main/Sample/Sample.Android/OpenGLViewSharedCode.cs

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dimonovdd/ImageFromXamarinUI/issues/3#issuecomment-788240932 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ALKPGROBW4P6HJ6IJSQIHM3TBPYZNANCNFSM4YGOSPCA .

dimonovdd commented 3 years ago

this is very easy to do on iOS

But on android, this is very strange. We need to call image capture in GLSurfaceView.onDrawFrame() method. To do this, we will have to write a custom renderer class for OpenGLView. This is beyond the scope of this plugin.

Perhaps you have some ideas on this?

pfafft33 commented 3 years ago

I’ve never written a custom renderer. As a matter of fact, the App that I’ve just put up on the Google Play Store is the first Android App that I’ve ever written.

I have also contacted the people at Microsoft, because they have a NuGet called Xamarin.Essentials that is supposed to do screen capture also, but has the same issue with the OpenGLView.

Maybe we’ll both get lucky and they will decide to fix this issue in Xamarin.Essentials.

By the way, if you would like to see my App, do a search on the Google Play Store for “Meditate-Lemniscate”. There’s a 30-day free trial, so you can take a look at it. I would really appreciate any feedback.

Sincerely,

Terence A. Pfaff

TAP Brands LLC

From: Dimov Dima notifications@github.com Sent: Tuesday, March 02, 2021 12:21 PM To: dimonovdd/ImageFromXamarinUI ImageFromXamarinUI@noreply.github.com Cc: Terence A. Pfaff terence.pfaff@gmail.com; Mention mention@noreply.github.com Subject: Re: [dimonovdd/ImageFromXamarinUI] CaptureImageAsync used on an OpenGLView produces blank results (#3)

this is very easy to do on iOS https://github.com/dimonovdd/ImageFromXamarinUI/blob/576e4fb756d5ec1565de244939c53c68e90e266b/ImageFromXamarinUI.OpenGLView/OpenGLViewExtension.ios.cs#L50

But on android, this is very strange. We need to call image capture in GLSurfaceView.onDrawFrame() method. To do this, we will have to write a custom renderer class for OpenGLView. This is beyond the scope of this plugin.

Perhaps you have some ideas on this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dimonovdd/ImageFromXamarinUI/issues/3#issuecomment-789112290 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ALKPGRNKXZTKGAJSES446JLTBUUA3ANCNFSM4YGOSPCA .

dimonovdd commented 3 years ago

@pfafft33 I think this problem does not apply to Essentials. This behavior has ios and android. Where did you create the new issue? I don't see your issue here https://github.com/xamarin/Essentials/issues

pfafft33 commented 3 years ago

https://github.com/xamarin/Essentials/issues/1702

From: Dimov Dima notifications@github.com Sent: Tuesday, March 02, 2021 1:23 PM To: dimonovdd/ImageFromXamarinUI ImageFromXamarinUI@noreply.github.com Cc: Terence A. Pfaff terence.pfaff@gmail.com; Mention mention@noreply.github.com Subject: Re: [dimonovdd/ImageFromXamarinUI] CaptureImageAsync used on an OpenGLView produces blank results (#3)

@pfafft33 https://github.com/pfafft33 I think this problem does not apply to Essentials. This behavior has ios and android. Where did you create the new problem? I don't see your problem here https://github.com/xamarin/Essentials/issues

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dimonovdd/ImageFromXamarinUI/issues/3#issuecomment-789152489 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ALKPGRKVBYQ27W2XWOR33FTTBU3IFANCNFSM4YGOSPCA .