ASKER
ASKER
ASKER
ASKER
private Icon loadImage(int page) throws IOException {
File file = new File(CAT_IMG_DIR + THREE_DIGITS.format(page) + ".jpg");
BufferedImage bi = ImageIO.read(file);
int width = bi.getWidth(null);
int height = bi.getHeight(null);
int new_width;
int new_height;
// Default ratio is 1 (equals 100% of the image size) which results in no
// resizing.
double ratio = 1;
// If the image is larger than the maximum bounds resize it to fit, but
// keep ratio between width and height to avoid distortion.
if (width > MAX_WIDTH &&
height > MAX_HEIGHT) {
// If width > height use width to resize to fit MAX_WIDTH.
if (width > height) {
ratio = MAX_WIDTH / (double) width;
}
// Else use height to resize to fit MAX_HEIGHT.
else {
ratio = MAX_HEIGHT / (double) height;
}
}
else if (width > MAX_WIDTH) {
ratio = MAX_WIDTH / (double) width;
}
else if (height > MAX_HEIGHT) {
ratio = MAX_HEIGHT / (double) height;
}
Icon ico = null;
// If resizing is needes, create a scaled instance.
if(ratio != 1) {
// Calculate new image size
new_width = (int) Math.round(width * ratio);
new_height = (int) Math.round(height * ratio);
// WORK AROUND
// Set new width and height to 1 since sometimes the BufferedImage filter
// is not able to read the width and height right and returns 0, which can
// cause a division by zero error when scaling to the new width and height.
new_width = new_width > 0 ? new_width : 1;
new_height = new_height > 0 ? new_height : 1;
ico = new ImageIcon(bi.getScaledInstance(new_width, new_height, Image.SCALE_DEFAULT));
}
// Else just return the image.
else {
ico = new ImageIcon(bi);
}
// Free resources and return the image.
bi.flush();
bi = null;
System.gc();
return ico;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btn_prev) {
loadPage(current_page - 2);
}
if(e.getSource() == btn_next) {
loadPage(current_page + 2);
}
}
/**
* Every page is indexed from 000 to nnn. Since the page amount can vary
* from catalog to catalog, this methode always determines the highest
* page number when this class is invoked.
*
* @return the highest page number of the catalog
*/
private static int getMaximumPage() {
int max_page = 0;
File img_dir = new File(CAT_IMG_DIR);
if (img_dir.exists() &&
img_dir.isDirectory()) {
File[] files = img_dir.listFiles();
for(File file : files) {
if (file.isFile() &&
file.getName().matches("\\d\\d\\d.jpg")) {
int num = Integer.parseInt(file.getName().substring(0, 3));
if(num > max_page) max_page = num;
}
}
}
System.out.println(max_page);
return max_page;
}
public static void main(String[] args) {
final JFrame f = new JFrame("TestCatalog");
f.getContentPane().add(new TestCatalog());
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
f.dispose();
System.exit(0);
}
});
f.pack();
f.setResizable(false);
// Go!!!
f.setVisible(true);
}
}
ASKER
private Icon loadImage(int page) throws IOException {
File file = new File(CAT_IMG_DIR + THREE_DIGITS.format(page) + ".jpg");
BufferedImage img = ImageIO.read(file);
int width = img.getWidth(null);
int height = img.getHeight(null);
// Default ratio is 1 (equals 100% of the image size) which results in no
// resizing.
double ratio = 1;
// If the image is larger than the maximum bounds resize it to fit, but
// keep ratio between width and height to avoid distortion.
if (width > MAX_WIDTH &&
height > MAX_HEIGHT) {
// If width > height use width to resize to fit MAX_WIDTH.
if (width > height) {
ratio = MAX_WIDTH / (double) width;
}
// Else use height to resize to fit MAX_HEIGHT.
else {
ratio = MAX_HEIGHT / (double) height;
}
}
else if (width > MAX_WIDTH) {
ratio = MAX_WIDTH / (double) width;
}
else if (height > MAX_HEIGHT) {
ratio = MAX_HEIGHT / (double) height;
}
Icon ico = null;
// If resizing is needes, create a scaled instance.
if(ratio != 1) {
// Define tile size for each tile of the original image, so
// each tile has a size of tile_size x tile_size pixels
int tile_size = 50;
// Calculate new image size
int new_width = (int) Math.round(width * ratio);
int new_height = (int) Math.round(height * ratio);
// WORK AROUND
// Set new width and height to 1 since sometimes the BufferedImage filter
// is not able to read the width and height right and returns 0, which can
// cause a division by zero error when scaling to the new width and height.
new_width = new_width > 0 ? new_width : 1;
new_height = new_height > 0 ? new_height : 1;
BufferedImage new_img = new BufferedImage(new_width, new_height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = new_img.createGraphics();
// Count tiles along x and y axis
int y_tile_count = (int) Math.ceil(img.getHeight(null) / tile_size);
int x_tile_count = (int) Math.ceil(img.getWidth(null) / tile_size);
int new_tile_size = (int) Math.round(tile_size * ratio);
// Read each tile and resize it accordingly.
for(int y = 0; y < y_tile_count; y++) {
for(int x = 0; x < x_tile_count; x++) {
int w = x < x_tile_count - 1 ? tile_size : width - (x * tile_size);
int h = y < y_tile_count - 1 ? tile_size : height - (y * tile_size);
// Read tile.
BufferedImage tile = img.getSubimage(x * tile_size, y * tile_size, w, h);
// Resize it.
w = (int) Math.round(w * ratio);
h = (int) Math.round(h * ratio);
Image scaled_tile = tile.getScaledInstance(w, h, BufferedImage.SCALE_SMOOTH);
// Free resources.
tile.flush();
// write tile into new output image.
g.drawImage(scaled_tile, x * new_tile_size, y * new_tile_size, null);
}
}
ico = new ImageIcon(new_img);
new_img.flush();
}
// Else just return the image.
else {
ico = new ImageIcon(img);
}
// Free resources and return the image.
img.flush();
img = null;
return ico;
}
ASKER
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
ASKER
@dejanpazin: There are no problems with the thread handling. I changed the image loading into a for loop,
so that the app is forced to load images one by one for testing.The result was the same.
@CEHJ: The gallery module is meant for creating galleries. So if you want to create one, you specify a source directory with images (preferably a local hard drive or mount in a LAN). After that the module creates a preview of all images inside the source directory. Then you cnn select or deselect images and alter settings for the gallery. If everything is to your liking, the module creates the gallery, resizing the images and writing them to target directory, containing the page displaying the images. Afterwards you can publish you gallery by transferring your gallery to your web folder.
I attached a test class that uses the same loading algorhythm (loadImage(int)) like my galery module. I hope this helps to ease assistance.
Thanks and greeting,
CB
Open in new window