Android Question using pngtastic in B4A

Robert Valentino

Well-Known Member
Licensed User
Longtime User
https://github.com/depsypher/pngtastic/

I would like to use PNGTastic to compress PNG files generated by my program (need to EMail them).

The ones I generate are in the 190k range but after something like TinyPNG they become 75k Big difference.

This (according to the posting is a Small standalone Java lib) that will do the same thing for me.

I am embarrassed to say I have NO idea where to start (other than a #AddionalJar statement which I am not completely such what I should say afterwards)

ANY help would be greatly appreciated

BobVal
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
If your images do not need to be in PNG format you may consider saving in JPG format instead of PNG? That way you can increase the image compression and create smaller images initially, something like:
B4X:
Public Sub SaveBitmapAsJPG(Dir As String, FileName As String, Picture As Bitmap, QualityPercent As Int)
    Dim Out As OutputStream = File.OpenOutput(Dir, FileName, False)
    If QualityPercent < 0 OR QualityPercent > 100 Then
        QualityPercent = 50
    End If
     Picture.WriteToStream(Out, QualityPercent, "JPEG")
     Out.Close
End Sub
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I have tried all the image types JPEG, PNG and GIF

For some reason my screen shots do not compress well with JPEG at 50% the file size is 154.5KB with PNG the files size is 94.8KB

Tiny PNG makes the shrinks the full PNG file down to 33KB and it looks a little better then what the JPEG does with a 50% setting.

Getting a 120KB saving bonus makes it Easy to Email the file to someone (in this case the file is a Players Golf Score Card)

Based on it's write up I am assuming I would get similar compression with PNGTastic

I have included the Files so you can see.

NOTE: These are not real score cards just typed in numbers for testing

JPEG at 50%
GolfScoreRound-20150405 - 13_02.jpeg


PNG
GolfScoreRound-20150405 - 13_02.png


TinyPNG
GolfScoreRound-20150405 - 13_02 - Tiny.png
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I have tried all the image types JPEG, PNG and GIF

For some reason my screen shots do not compress well with JPEG at 50% the file size is 154.5KB with PNG the files size is 94.8KB

Tiny PNG makes the shrinks the full PNG file down to 33KB and it looks a little better then what the JPEG does with a 50% setting.

Getting a 120KB saving bonus makes it Easy to Email the file to someone (in this case the file is a Players Golf Score Card)

Based on it's write up I am assuming I would get similar compression with PNGTastic

I have included the Files so you can see.

NOTE: These are not real score cards just typed in numbers for testing

JPEG at 50%
View attachment 33375

PNG
View attachment 33376

TinyPNG
View attachment 33377
Hi, I actually started playing with the JavaObject and the #AdditionalJar methods today, and i got this working, however I don't really see a big difference when compressing a png file, for example, I tried compressing a 316kb picture and the lowest i can get it to is 237kb.
If you want to play around here's the code i'm using, you need to somehow make a jar file from the project file from the link you provided in the first post.

B4X:
'You need to first copy any image from your dirAssets folder to your DirDefaultExternal folder
File.Copy(File.DirAssets, "screenshot.png", File.DirDefaultExternal, "screenshot.png")

'Initialize the pngOptimizer class
Dim optimizer As JavaObject
optimizer.InitializeNewInstance("com.googlecode.pngtastic.core.PngOptimizer", Array("error"))

'Then you need to instatiate the pngImage class and pass the filename path
Dim png As JavaObject
png.InitializeNewInstance("com.googlecode.pngtastic.core.PngImage", Array(File.Combine(File.DirDefaultExternal, "screenshot.png")))

'You then need to run the method optimize, and give it the parameters described in the documentation
optimizer.RunMethod("optimize", Array(png, File.DirDefaultExternal & "/screenshot_Processed.png", True, 9))

Keep in mind that this is my first time playing with the JavaObject and the #additionaljar methods.

You can download the image I used, the first link is the original png image, and the second link is the processed png image, you can see the size difference.

https://www.dropbox.com/s/83df358l7f192zp/screenshot.png?dl=0

https://www.dropbox.com/s/f48nysrew8bf696/screenshot_Processed.png?dl=0

Good luck
 
Upvote 0
Top