New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
decodePng returns empty buffer #422
Comments
I'm not sure why you would be getting all zeros. The PNG unit tests are working. Is it a particular image that's failing? |
The png loads fine when I just tested it. I'm having a hard time thinking what a Flutter update might be doing to zero out the image bytes. Do all pngs have the same behavior for you? |
I loaded one of your test PNGs and got different results but it still failed same way: And it fails later when I want to load it to MemoryImage: |
I think problem is with data type. I pass image as UInt8 but decodeImage(Png) takes List which is 64 bit long. |
The library should definitely take a Uint8List. List is just the abstract base class. It seems to be decoding the PNG ok; the resolution is set, it's not crashing. But why the image pixels are all 0 in your image is strange. The pixels are not all 0 in the other test image. When you say "load it to MemoryImage", are you re-encoding the image back to a PNG? |
So I am trying to add images to PDF using the pdf package. This was working perfectly until I updated packages and flutter.
Then I am trying to create a PDF-based image, so I can add it to PDF. It used to accept Image now needs ImageProvider:
But this fails as MemoryImage can't detect image type as the buffer is full of "0". I tried to step into the image package, but it was very slow. Maybe I need to write some test app to check it? If any PNG fails to decode, I will try JPG etc too. I have a feeling that there is some mixing of packages happening. |
It probably doesn't matter because the numbers will be the same, but you should use a Uint8List and not a Int64List for bytesList. In the very least, it will use less memory. I haven't used Flutter, or the PDF package, so I don't know thoise APIs very well, but I believe MemoryImage probably should be an encoded PNG. logoImage.getBytes() is the raw bytes of the image. it doesn't have any of the other information such as format or resolution. So I would think you'd want to do something like Or, if you're not doing anything to modify the image, do you even need to use the image library? ByteData bytes = await rootBundle.load('images/pestscope_tm.png');
// ...
pw.Image(pw.MemoryImage(bytes)), |
Just out of curiosity what are you using Dart for ?
|
I wrote this library a long time ago, 2013 or so, when I was using Dart for some web projects. Dart is still a great language, but I don't use it for anything anymore, I just try to maintain the couple of libraries I published that people use. It is curious why the image is decoding black. I'm glad you got unstuck. Not decoding/re-encoding the image will save you considerable performance. |
This is a great library and is used by TFLITE package, so I need to use it in my project. I found out that images from my phone are decoded empty, and images from the internet or screenshots work just fine. |
If you can send me an example image file that doesn't work, I can get it to work. The ICC profile or the EXIF data should impact the PNG pixels decoding or not, so there must be something else about the images from the phone, but I have no idea what. |
Thanks a lot! Here is the working image: https://photos.app.goo.gl/k4uwrZgtKHutPX1a7 |
I got a chance to look into this, but both images stored on google photos are stored as JPEG, and I didn't have a problem loading either. I assume these are not the actual photos you're using in the app, since you were talking about PNG images. |
So I solved the issue with PNG images as mentioned above by skipping the use of decode completely (as you suggested). |
The decodePng used to work nicely with my PNG file in previous versions of Flutter.
ByteData bytes = await rootBundle.load('images/pestscope_tm.png'); logoImage = image.decodePng(bytes.buffer.asUint8List());
Suddenly it decodes PNG file, the size is set, and some other data too, but buffer is full of zeros:
Image blendMethod: BlendMode (BlendMode.over) channels: Channels (Channels.rgba) data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... disposeMethod: DisposeMode (DisposeMode.clear) duration: 0 exif: ExifData height: 82 iccProfile: ICCProfileData textData: null width: 480 xOffset: 0 yOffset: 0 hashCode: 780885460 length: 39360 numberOfChannels: 4 runtimeType: Type (Image)
I tried different methods and still getting zeros.
The text was updated successfully, but these errors were encountered: