Image questions
Q: What image-file formats does Java 2D support?
A: Through its Image I/O package (javax.imageio) Java 2D supports reading and writing GIF, JPEG, PNG, BMP and WBMP images. The Image I/O package includes support for easily plugging in additional image format support: JAI (Java Advanced Imaging) in particular has developed Image I/O plugins that add reading and writing support for formats such as TIFF and JPEG2000 in its "JAI Image I/O Tools" package, which has no dependency on the core JAI libraries.
Q: Which ColorModel should I use if I need optimum performance?
A: The best way to improve your performance with ColorModel optimization is to use an opaque ColorModel. If you are using an IndexColorModel, make sure you have no transparent pixel values and no alpha values in cmap arrays, and set hasAlpha to false. If you are using a ComponentColorModel, set Transparency to OPAQUE and hasAlpha to false. If you are using a DirectColorModel, make sure you have no alpha mask, or set the mask to zero. If you are using a BufferedImage, construct it only with one of the types that do not contain alpha.
Q: What image types should I use to get the best performance?
A: If you are creating an image whose data will be copied to the screen, create the image with the same depth and type of the screen. This way, no translation between the image and the screen is necessary before copying the image. To ensure that the depth and type of your image match the screen, use the createImage(w, h) method of your on-screen component which returns an image that has the same depth as the screen. Alternatively, if you are using a BufferedImage, create it with one of the pre-defined types or use GraphicsConfiguration.createCompatibleImage(w, h) to match the image with the screen format.
Q: How do I create a resized copy of an image?
A: Here's one way to do it: create a BufferedImage of the desired size and draw the original image into it, scaling on the fly. Note that depending on whether your original image is opaque or non-opaque (that is, if it's translucent or transparent), you may need to create an image with an alpha channel.
BufferedImage createResizedCopy(Image originalImage,
int scaledWidth, int scaledHeight,
boolean preserveAlpha)
{
int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
Graphics2D g = scaledBI.createGraphics();
if (preserveAlpha) {
g.setComposite(AlphaComposite.Src);
}
g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
g.dispose();
return scaledBI;
}
You can control the quality of the scaled copy with the Graphics2D.setRenderingHint() method.
See http://java.sun.com/j2se/1.5.0/docs/api/java/awt/RenderingHints.html#KEY_INTERPOLATION
Add the following before the drawImage() call:
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_[NEAREST_NEIGHBOR
BILINEAR
BICUBIC]);
Q: There are several types of images in Java2D; how do I know which one is appropriate in a particular situation?
A: This article attempts to address that general question: http://java.sun.com/developer/technicalArticles/Media/imagestrategies/index.html. You might also look at other articles in this blog, particularly the ones on VolatileImage and BufferedImage objects: http://weblogs.java.net/blog/chet
Q: Can I use Java2D to generate dynamic images from my servlet (or other server-side Java application)?
A: Certainly! Java2D isn't just for desktop applications. You can use the Java2D API to dynamically generate images in your server application, and then use the Image I/O API to write the image to a File or to an OutputStream (so that it can be viewed in a browser). For example, you can generate a weather map using real-time weather data, or create a pie chart using data from your accounting database. The possibilites are endless...
Here is a simple example of a servlet that generates a smiley face using Java2D, and then writes a JPEG image that can be viewed in a browser:
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ImageServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("image/jpeg");
// Create image
int width=200, height=200;
BufferedImage image =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Get drawing context
Graphics2D g2d = image.createGraphics();
// Fill background with white
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
// Draw a smiley face
g2d.setColor(Color.YELLOW);
g2d.fillOval(10, 10, 180, 180);
g2d.setColor(Color.BLACK);
g2d.fillOval(40, 40, 40, 40);
g2d.fillOval(120, 40, 40, 40);
g2d.fillRect(50, 150, 100, 10);
// Dispose context
g2d.dispose();
// Write image to the output stream
ServletOutputStream os = response.getOutputStream();
ImageIO.write(bi, "jpeg", os);
}
}
Q: I have an application that reads, writes, and processes images (but does not display them) using Java2D. When I run my application on Solaris or Linux, it complains that X11 is not available. What can I do to make my application work in this environment?
A: When AWT is initialized, it expects to find an Xserver, regardless of whether it is needed for actual display.
Although many image operations using the Image I/O APIs or the JAI optional package might not have any obvious need for display, they often invoke code that needs an AWT resource. For example, calling getGraphics() on a BufferedImage initializes AWT and causes these error messages seen by developers. There is no way to say that a particular API does or does not have this problem; it depends on what particular operations are being invoked, and might also depend on what the application does with the images that is not strictly related to any of the APIs cited above.
There are two possible solutions. As of J2SE 1.4 (and above), the preferred solution is to use the "headless AWT toolkit". This feature allows you to use the Java2D API in a server-side application without the need for a display environment. To specify the headless environment, run your application with the following system property:
-Djava.awt.headless=true
For releases prior to J2SE 1.4 you can provide a "pseudo X-server" to emulate a display environment. One of these Xserver emulators is Xvfb, available for download at http://www.x.org. Another possibility is to start a VNC server (http://www.realvnc.com) and then run your application in that environment.
Xvfb does still have one possible use in J2SE 1.4: A server application may need to display to an Xserver, but requires no user interaction. The headless toolkit won't support this situation, but xvfb will. This is a completely hypothetical scenario and it is not clear if any real world application exhibits such a behavior.
Thursday, August 21, 2008
Posted by Khushi at 12:39 AM
Labels: Java 2D Qns_3
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment