This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 6 - Pages: 1 - Last Post: 02-May-2020 17:49 Last Post By: AndreWolff Threads: [ Previous | Next ]
AndreWolff

Posts: 1,609
Registered: 14-Dec-2007
How to get the image width of an image in the output folder?
Posted: 02-May-2020 08:54
 
  Click to reply to this thread Reply
The output folder contains my own theme image ThemeImage.jpg, this is not the theme image folderimage.jpg from the theme image folder panel.

I need to know the width of that image. So I used next code in index.htt:
 System.out.println("themeImagePath: "  + themeImagePath );
  BufferedImage bimg = ImageIO.read(new File(themeImagePath));
  int width          = bimg.getWidth();

But this did result in an crash:
themeImagePath: ThemeImage.jpg
se.datadosen.util.ScriptException: java.lang.ClassNotFoundException: Class: BufferedImage not found in namespace in index.htt at line number 1

What should I do to get the correct result?

Edit:

after I added in the top of index.htt:
<% 
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
%>
the crash message did change into:
themeImagePath: ThemeImage.jpg
se.datadosen.util.ScriptException: javax.imageio.IIOException: Can't read input file! in index.htt at line number 1


What is the best way to get this width?

Edit:
This works:
  System.out.println("themeImagePath: "  + themeImagePath );
  File testFile = new File(outputDirectory, themeImagePath);
  BufferedImage bimg = ImageIO.read(testFile);
  int width          = bimg.getWidth();
Is this the best way to do this?

Edited by: AndreWolff on 02-May-2020 09:21
RobM

Posts: 3,173
Registered: 4-Aug-2006
Re: How to get the image width of an image in the output folder?
Posted: 02-May-2020 13:56   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
That is the way l would do it. Not tried, it depends on jAlbum 20 as a minimum and only if the library is fully implemented, is to use the Commons imaging API. See https://commons.apache.org/proper/commons-imaging/apidocs/index.html if you want to play with that as a potential method.
AndreWolff

Posts: 1,609
Registered: 14-Dec-2007
Re: How to get the image width of an image in the output folder?
Posted: 02-May-2020 14:06   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
I just did release new versions of my skins were this has been used without problems.

I did code it in a function in init.bsh:
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
 
private int widthImageInOutputFolder(String fileName)  {
   File imageFile = new File(outputDirectory, fileName);
   if (imageFile.exists()) {	
	BufferedImage bimg = ImageIO.read(imageFile);
	return bimg.getWidth();
   }
	return -1;
}
ctwist

Posts: 595
Registered: 27-Sep-2003
Re: How to get the image width of an image in the output folder?
Posted: 02-May-2020 15:18   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
To avoid a resource leak, add
bimg.flush();
after you have finished with it.
AndreWolff

Posts: 1,609
Registered: 14-Dec-2007
Re: How to get the image width of an image in the output folder?
Posted: 02-May-2020 15:29   in response to: ctwist in response to: ctwist
 
  Click to reply to this thread Reply
ctwist wrote:
To avoid a resource leak, add
bimg.flush();
after you have finished with it.
?? Where should I add this?
After the return it will not be executed and before the return it will destroy the return value. And after all, this is a function, are all used memory not automatically removed after the return?
ctwist

Posts: 595
Registered: 27-Sep-2003
Re: How to get the image width of an image in the output folder?
Posted: 02-May-2020 15:33   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
Modify your code so that you extract the dimensions, then flush the image, then return.

If you don't understand resource leaks, you will have to do your own research.

AndreWolff wrote:
And after all, this is a function, are all used memory not automatically removed after the return?
Maybe sometimes; I am not sure. I have seen enough resource leaks that I always flush, to be on the safe side.

Edited by: ctwist on 02-May-2020 09:35
AndreWolff

Posts: 1,609
Registered: 14-Dec-2007
Re: How to get the image width of an image in the output folder?
Posted: 02-May-2020 17:49   in response to: ctwist in response to: ctwist
 
  Click to reply to this thread Reply
ctwist wrote:
Modify your code so that you extract the dimensions, then flush the image, then return.
You are the expert, so I did implement your suggestion.

Thanks for your advice!
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums