mylisabox / flutter_mjpeg

Flutter widget to show mjpeg stream from URL
BSD 2-Clause "Simplified" License
30 stars 23 forks source link

How to get the width and height of the jpeg? #20

Closed delphi007 closed 2 years ago

delphi007 commented 2 years ago

I have run this widget well in my project. I need the original width / height of jpeg image. But when I access Mjpeg.width, MJpeg.height, they are always null.

What should I do to retrieve the Size for image?

jaumard commented 2 years ago

Like the readme say, Mjpeg.width, MJpeg.height are to force the size of the widget. I don't care about the original size of the image so I don't have it anywhere. No even sure how to get it.

delphi007 commented 2 years ago

OK, thanks. I have found the workaround, in case anyone who need the width & height.

  1. In mjpeg.dart , add a variable 'MemoryImage? latestImg;' in class Mjpeg.
  2. In "Widget build(BuildContext context)" function, add one line to save the latest frame: latestImg = image.value; The above line should be put right after if (image.value == null) { ... }
  3. In your Flutter App, use the following code to retrieve the width & height
     _mjpeg?.latestImg
                  ?.resolve(ImageConfiguration())
                    .addListener(
                        ImageStreamListener((ImageInfo info, bool _) {
                          final imgWidth = info.image.width;
                          final imgHeight = info.image.height;
                          ... //Do other stuff as you like
                        }));

I am using this plugin to show phone screen stream and need to perform action on the screen. So the width & height is a must to calculate relative ratio for interaction. And the screen orientation could change during the streaming, so that's why I need the realtime width & height. To avoid calculation for each frame. I decided to just save the RawImage for future use and leave the calculation when needed.

Sorry for not clarifying the usage scenario. I agree it's not a necessary function for this widget. Still share my solution for anyone who is interested.

Thank you for this great widget. It's working fine.

delphi007 commented 2 years ago

Work around found.

jaumard commented 2 years ago

nice thanks for sharing. If I may, using build method to populate a field that you used in the app is not a good/clean approach.

One way to do it is to add a callback as parameter on Mjpeg widget where you pass the current MemoryImage each time it changes. This way the widget is telling you when a new image is present and it's not you who check a field of the widget.

If you want to do a PR in that way so you don't have to fork this package please do, I'll be happy to merge it :)