B4J Question Create image thumbnails

AHilton

Active Member
Licensed User
Longtime User
I have a list of images and would like to create thumbnails of those images to then save to disk. What do I use to scale the images ?
 

AHilton

Active Member
Licensed User
Longtime User
The following subroutine saves the original file just fine, and saves the thumbnail, too, but it's only 1 pixel x 1 pixel in size. I've tried all sorts of different settings on the DrawImage to try to get it to be around 100x100 pixels but can't get there.


B4X:
Sub SavePicture (SourceDir As String, PicFilename As String, SaveDir As String)
    ' Copy the whole image to final dir
    File.Copy(SourceDir, PicFilename, SaveDir, PicFilename)
    ' Make a thumbnail and save it to the \thumbs dir
    Dim img As Image
    Dim cvs As Canvas
    cvs.Initialize("")
    img.Initialize(SourceDir, PicFilename)
    cvs.Height = img.Height
    cvs.Width = img.Width
    cvs.DrawImage(img, 0, 0, 200dip, 200dip)
    Dim out As OutputStream = File.OpenOutput(SaveDir & "\thumbs", PicFilename, True)
    cvs.snapshot.WriteToStream(out)        ' Outputs as a PNG
    out.Close
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Not tried using Canvas, but imageview works just as well
B4X:
    Dim im As Image
    Dim cells As ImageView
    im.Initialize("c:/temp/","jelly.png")' the image (150 x 150)
    cells.Initialize("")
    Dim jo As JavaObject = cells
    cells.SetImage(im)
    jo.RunMethod("setFitWidth",Array(75.0))     ' width I want
    jo.RunMethod("setFitHeight",Array(75.0))    ' height I want
    jo.RunMethod("setPreserveRatio",Array(False))
    Dim out As OutputStream = File.OpenOutput("c:/temp/", "jellyGrab.png", True)' now its 75 x 75
    cells.snapshot.WriteToStream(out)        
    out.Close
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
Thanks, Daestrum. That works (with a minor change ... add: " as object " after the "Array".

The problem I have now is that these don't work with JPG images. These are email attachments that I need to intercept, save and thumbnail. Any ideas?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Try
B4X:
    Dim im As Image
    Dim can As Canvas
    im.Initialize("c:/temp/","jelly.png")' the image (150 x 150)
    Dim jo As JavaObject
    can.Initialize("")
    jo = can
    jo.RunMethod("setWidth",Array(75.0))
    jo.runmethod("setHeight",Array(75.0))
    can.DrawImage(im,0.0,0.0,75.0,75.0)
    Dim out As OutputStream = File.OpenOutput("c:/temp/", "jellyGrab.png", True)
    can.snapshot.WriteToStream(out)        
    out.Close

** the array difference is because I am using latest B4J (v 2.2) which allows Array(...) instead of Array As Object(...)
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
That works with JPG files. Many thanks, Daestrum.

For some reason, sometimes, an images' properties are null. I'm trying to read the width and height of the original image in order to scale the picture proportionately for the thumbnails. Not sure why some images do that. I guess their EXIF information is getting lost somehow.

Anyway, this works and I'm continuing on...
 
Last edited:
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
Unfortunately, after getting back to this project, this isn't working consistently. The thumbnails are getting saved in the correct proportions but *SOME* only have file sizes under 1k where the thumbnails that do work are ranging around 50k. All pictures are JPG files and I can't find a reason why some do but some don't.
This thread ... https://www.b4x.com/android/forum/threads/obtain-image-height-and-width.49737/#post-310673 ... fixed the problem of obtaining a height and width consistently (which I thought was the problem all along) but didn't solve the ultimate problem of the saved thumbnail files being "bad".

In that thread, you can see a link to 2 pictures, one that creates a 'good' thumbnail and one that creates a 'bad' thumbnail with the code in that thread.

Should I be using something other than Image and Canvas to do the drawing and writing to disc?
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
I updated my Java from 8.11 to 8.31 and it made no difference.

Here's another "bad" image link. You have to download it from there and not just "save picture as" in the browser. Downloading gives you a filesize of 810kb and dimensions of 1565 x 2782. This results in a "bad" thumbnail picture. Doing a "save picture as" gives you a smaller image and a thumbnail gets created just fine.

https://drive.google.com/open?id=0B0oh6QknY6i-UUZpNmQ0S1JjOEU&authuser=0
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
Prism pipeline init order: d3d j2d
Using t2k for text rasterization
Using dirty region optimizations
Opting in for HiDPI pixel scaling
Prism pipeline name = com.sun.prism.d3d.D3DPipeline
Loading D3D native library ...
succeeded.
Direct3D initialization succeeded
(X) Got class = class com.sun.prism.d3d.D3DPipeline
Initialized prism pipeline: com.sun.prism.d3d.D3DPipeline
Maximum supported texture size: 8192
Maximum texture size clamped to 4096
OS Information:
Windows 7 build 7601
D3D Driver Information:
Intel(R) HD Graphics 4600
\\.\DISPLAY1
Driver nvumdshimx.dll, version 9.18.13.4066
Pixel Shader version 3.0
Device : ven_8086, dev_0416, subsys_1909103C
Program started.
QuantumRenderer: shutdown
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
Neither works.


-Dprism.order=j2d

Results in the log ...

Prism pipeline init order: j2d
Using t2k for text rasterization
Using dirty region optimizations
Opting in for HiDPI pixel scaling
*** Fallback to Prism SW pipeline
Prism pipeline name = com.sun.prism.j2d.J2DPipeline
(X) Got class = class com.sun.prism.j2d.J2DPipeline
Initialized prism pipeline: com.sun.prism.j2d.J2DPipeline
Program started.
Type :2
Height :2782
Width :1565
Glass native format: 1



-Dprism.order=sw

Results in the log ...

Prism pipeline init order: sw
Using t2k for text rasterization
Using dirty region optimizations
Opting in for HiDPI pixel scaling
Prism pipeline name = com.sun.prism.sw.SWPipeline
GraphicsPipeline.createPipeline failed for com.sun.prism.sw.SWPipeline
java.lang.ClassNotFoundException: com.sun.prism.sw.SWPipeline
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.sun.prism.GraphicsPipeline.createPipeline(GraphicsPipeline.java:150)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.init(QuantumRenderer.java:72)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:107)
at java.lang.Thread.run(Thread.java:745)
Graphics Device initialization failed for : sw
Error initializing QuantumRenderer: no suitable pipeline found
java.lang.RuntimeException: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
at com.sun.javafx.tk.quantum.QuantumRenderer.getInstance(QuantumRenderer.java:281)
at com.sun.javafx.tk.quantum.QuantumToolkit.init(QuantumToolkit.java:239)
at com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:208)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:125)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:163)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.init(QuantumRenderer.java:75)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:107)
... 1 more
java.lang.RuntimeException: No toolkit found
at com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:220)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:125)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:163)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:745)
 
Upvote 0
Top