views:

230

answers:

1

I need to create and store thumbnails (of images from the web) and store them on the server. Can I use the awt libraries (as listed below) on a linux server running in a datacenter (without a monitor)? I do not know if the server has a graphics card or needs one for these libraries to work...

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.net.URL;

import javax.imageio.ImageIO;

import org.springframework.stereotype.Service;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Color;
+1  A: 

You can as long as you run java in headless mode:

java -Djava.awt.headless=true your.MainClass
ChssPly76
I tried using the headless option but ran into java.awt.HeadlessException for function getDefaultScreenDevice(). Is there a way around this?
sammichy
What are you actually trying to do? Why do you need a GraphicsDevice?
Jonathan Feinberg
If you need a head, but don't have a real display device, you should be able to use Xvfb (X virtual framebuffer). http://en.wikipedia.org/wiki/Xvfb
Tom Hawtin - tackline
@Jonathan : I'm trying to create a thumbnail from an image file/URL.
sammichy
You do not need a screen device to create a thumbnail. Take a look at this: http://jcsnippets.atspace.com/java/gui-graphics/create-thumbnail.html for a basic example
ChssPly76
@ChssPlys76 The example works fine, thank you. The next issue I have to solve is is performance, it takes almost 1 second to transform a 40k image (mostly transformation, the download time is minimal). I'm building a web app that takes a bunch of links and transforms them into thumbnails...
sammichy
I'm no image resizing expert to be honest, though 1 second seems a lot (did you profile it across a reasonable sampling size? like couple hundred images?). There were quite a few questions asked on SO on this topic, perhaps you'll find some pointers there: http://stackoverflow.com/search?q=%5Bjava%5D+thumbnail
ChssPly76
@ChssPlys I looked through the docs and found that using drawImage() instead of getScaledInstance() is significantly faster. I recommend using drawImage()
sammichy