May 23
// Load the image

image = new ImageIcon(image).getImage();

// Determine transparency for BufferedImage
// http://helpdesk.objects.com.au/java/how-to-determine-if-image-supports-alpha

boolean hasAlpha = hasAlpha(image);
int transparency = hasAlpha ? Transparency.BITMASK : Transparency.OPAQUE;

// Create the buffered image

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
BufferedImage bufferedImage = gc.createCompatibleImage(image.getWidth(null),
		image.getHeight(null), transparency);

if (bufferedImage == null) {
	
	// if that failed then use the default color model
	
	int type = hasAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
	bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}

// Copy image

Graphics g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();

written by objects \\ tags: , , ,

May 23

The PixelGrabber can be used to get a ColorModel instance that has a method to tell if alpha is supported. You just need to load a single pixel from the image to get the ColorModel.

PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
pg.grabPixels();
boolean hasAlpha = pg.getColorModel().hasAlpha();

written by objects \\ tags: , , ,

Apr 15

Basically you stream the image to the http response stream. Easier to explain with code so here’s a simple example.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ImageServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, 
    		HttpServletResponse response) throws IOException
    {
    	ServletContext application = getServletContext();
    	
    	// Get the path to image we want to return
    	
        String filepath = application.getRealPath("image.gif");
    
        // Get image MIME type
        
        String mimeType = application.getMimeType(filepath);
        if (mimeType == null)
        {
            application.log("Could not get MIME type of "+filepath);
            response.setStatus(
               HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
    
        // Set content type
        
        response.setContentType(mimeType);
    
        // Set content size
        
        File file = new File(filepath);
        response.setContentLength((int)file.length());
        
        // Copy the contents of the file to the responses output stream
        	
        FileInputStream in = new FileInputStream(file);
        OutputStream out = response.getOutputStream();
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = in.read(buf)) >= 0)
        {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

}

See also: