Title: Java 2D Image Manipulation A Brief Overview
1Java 2D Image ManipulationA Brief Overview
Ziv Yaniv
2BufferedImage
- ColorModel Translates from pixel data to
(R,G,B,a), ComponentColorModel, -
IndexColorModel, PackedColorModel . - Raster The image data. Use the raster to access
specific pixels or change their - value (WritableRaster).
- SampleModel How to translate the data found in
the DataBuffer as pixel - values.
ComponentSampleModel, MultiPixelPackedSampleModel
, -
SinglePixelPackedSampleModel - DataBuffer The actual image values which with
the help of the SampleModel - are translated to
pixel data are then using the ColorModel to the - pixels color.
DataBufferByte, DataBufferInt, DataBufferShort, - DataBufferUShort
3Loading an Image (java 2D)
- public BufferedImage loadImage(String fileName)
- Image img Toolkit.getDefaultToolkit().getIma
ge(fileName) - MediaTracker tracker new MediaTracker(new
Component() ) - tracker.addImage(img, 0)
- try
- tracker.waitForID(0)
- catch(InterruptedException e) //error
while loading - BufferedImage res new BufferedImage(img.getW
idth(null), -
img.getHeight(null), - BufferedImage.TYPE_I
NT_RGB) - Graphics2D g res.createGraphics()
- g.drawImage(img, null, null)
- return res
-
4Setting Pixel Values
//draw a red square in the middle of an
image final int halfEdgeSize 5 int redPixel
255, 0, 0 BufferedImage image
loadImage(theFileName) //pixel type is
INT_RGB WritableRaster raster
image.getRaster() int imgWidth
image.getWidth() int imgHeight
image.getHeight() for(int yimgHeight/2
halfEdgeSize ylt imgHeight/2 halfEdgeSize y)
for(int ximgWidth/2 halfEdgeSize ylt
imgWidth/2 halfEdgeSize x)
raster.setPixe(x, y, redPixel)
5BufferedImageOp
- Operations/Filters on BufferedImage objects are
classes which implement the - BufferedImageOp interface.
- Note These filters are single source single
destination filters. - For more advanced filters see Java
Advanced Imaging JAI package. - The available filters in the java 2D package
are - LookupOp, ConvolveOp, AffineTransformOp,
ColorConvertOp, RescaleOp - For examples of using these filters see the
java tutorial.