1 / 93

Java and Imaging

Java and Imaging. Paint. paint() method AWT Coordinate System Color & ColorModel the java.awt.Color class the java.awt.image.ColorModel class. paint() method. Java Peer to draw, inherit Canvas, override paint redraw screen WINDOW_

isolde
Download Presentation

Java and Imaging

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java and Imaging

  2. Paint • paint() method • AWT Coordinate System • Color & ColorModel • the java.awt.Color class • the java.awt.image.ColorModel class

  3. paint() method Java Peer to draw, inherit Canvas, override paint redraw screen WINDOW_ paint() EXPOSED update() Event repaint() clear component window

  4. AWT Coordinate System

  5. The java.awt.Color class • Encapsulate RGB colors • have static final Color object of frequent-used colors public static final Color black; public Color(int red, int green, int blue); public Color brighter(); public Color darker(); public int getRed();

  6. java.awt.image.ColorModel • encapsulate the METHODS of TRANSLATION from PIXEL VALUES • get alpha, red, green, blue value from pixel • implemented by DirectColorModel, IndexColorModel public abstract class ColorModel; public colorModel(int nbits); public abstract getAlpha(int pixelvalue); public static ColorModel getRGBdefault(); // return default ColorModel 0xAARRGGBB

  7. Drawing • The Graphics class • Simple Drawing • Working With Text • Font • FontMetrics

  8. java.awt.Graphics class • contain current graphics context and methods for drawing • graphics context : fgcolor, bgcolor, font, etc. • abstract class - actual java implementation and native library is provided by virtual machine

  9. Drawing Methods of Graphics

  10. Simple Drawing public void mouseClicked(MouseEvent e) { pt = e.getPoint(); repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void paint(Graphics g) { g.setColor(Color.blue); g.drawRect(pt.x, pt.y, 10, 10); } } import java.awt.*; import java.awt.event.*; public class DrawingCanvas extends Canvas implements MouseListener { protected Point pt; public DrawingCanvas() { pt = new Point(50, 50); addMouseListener(this); } public Dimension getPreferredSize() { return new Dimension(100, 100); }

  11. java.awt.Font • load Font object from font name public Font(String name, int style, int size); public static Font getFont(String nm);

  12. java.awt.FontMetrics • has methods to get information from Font object public FontMetrics(Font fn); public getAscent(); public getDescent();

  13. Using images • The Image class • Loading images • Loading • Keep Track of Image Loading • With the MediaTracker class • With the ImageObserver interface* • Displaying images

  14. Using images - • Manipulating images* • imagefilter

  15. java.awt.Image class • encapsulate image • programmer can’t know about internal data structure representaing image public abstract Graphics getGraphics(); public abstract int getWidth(); public abstract int getHeight(); public Image createImage(ImageProducer); public Image createImage(int w, int h);

  16. Loading • automatic loading about JPEG and GIF images • background loading • use methods in java.applet.Applet or java.awt.Toolkit • load via filename or url public abstract Image Toolkit.getImage(URL url); public abstract Image Toolkit.getImage(String file) Toolkit Component.getToolkit() or static Toolkit Toolkit.getDefaultToolkit()

  17. java.awt.MediaTracker • keep track of image loading public MediaTracker(Component comp); public addImage(Image img, int id); public boolean checkAll(); public boolean checkID(int id); public boolean isErrorAny(); public void waitForAll();

  18. java.awt.MediaTracker - MediaTracker tracker; tracker = new MediaTracker(this); Toolkit tk = Toolkit.getDefaultToolkit(); for (int i = 1; i <= 10; i++) { images[i-1] = tk.getImage("image" + i + ".gif"); tracker.addImage(images[i-1], i); } try { //Start downloading the images. Wait until they're loaded. tracker.waitForAll(); } catch (InterruptedException e) {} // in paint(); if (!tracker.checkAll()) { g.clearRect(0, 0, d.width, d.height); g.drawString("Please wait...", 0, d.height/2); } //If all images are loaded, draw. else { ...//same code as before...

  19. java.awt.image.ImageObserver interface loading imageUpdate() ImageObserver • image width known height known loading

  20. java.awt.image.ImageObserver public interface ImageObserver { public abstract boolean imageUpdate(image img, int infoflags, int x, int y, int width, int height); } If image knows width and height, or loading pixels, call this methods with infoflags. if return true, no more call this methods Component implements ImageObserver if not completely loaded yet, repaint()

  21. java.awt.image.ImageObserver • public int Image.getWidth(ImageObserver observer) • If the width is not known yet then the ImageObserver will be notified later and -1 will be returned. • public boolean drawImage ( Image img, int x, int y, int width, int height, Color bgColor, ImageObserver observer ) • return immediately. return ture if image is fully initialized.if not, return false, and let img notify observer

  22. Displaying images • public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer); • public abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer); • public abstract boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer); • public abstract boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer);

  23. ImageFilter - startProduction() setPixels() ImageProducer ImageConsumer

  24. ImageFilter - ImageFilter ImageProducer ImageConsumer ImageFilter filter = new RotateFilter(angle); ImageProducer producer = new FilteredImageSource(souceImage.getSource(), filter); Image resultImage = createImage(producer);

  25. Improving Animation • Eliminating Flashing • Overrideing update() method • Clipping • Double Buffering • Speed up Image Loading with Clipping

  26. Overriding update() method • default update() • clear all component's Background and call paint() • Component’s member method public void update(Graphics g) { DoActualDrawing(); } public void paint(Graphics g) { update(g); }

  27. Clipping • public abstract void clipRect(int x, int y, int width, int height); • Clipping the Drawing Area public void update(Graphics g) { Graphics g2 = g.create(x, y, w, h); paint(g2); }

  28. Double Buffering • update drawing area one time Dimension offDimension; Image offImage; Graphics offGraphics; public void update(Graphics g) { if( (offGraphics == null) || ( d.width != offDimension.width) || (d.height != offDimension.height) ) { offDimension = d; offImage = createImage(d.width, d.height); offGraphics = offImage.getGraphics(); } DoActualDrawing(offGraphics); drawImage(offGraphics, 0, 0, this); }

  29. Speed up Image Loading with Clipping • when loading images using URLs, most of the time is taken up by initiating HTTP connections. • make an image strip int stripWidth = imageStrip.getWidth(this); int stripHeight = imageStrip.getHeight(this); int imageWidth = stripWidth / numImages; g.clipRect(0, 0, imageWidth, stripHeight); g.drawImage(imageStrip, -imageNumber*imageWidth, 0, this);

  30. New Features of Java1.1 • Enhancement on Graphics Clip • Cropping, Scaling, and Flipping • MemoryImageSource • PixelGrabber

  31. Enhancement on Clipping • New methods of Graphics Shape getClip(); void setClip(Shape clip); void setClip(int x, int y, int w, int h); void getClipBounds();

  32. Enhancement on Clipping Graphics g2 = g.create(); g2.clipRect(10, 10, 100, 100); g2.drawImage(img2, 10, 10, this); g2.dispose(); Shape oldclip = g.getClip(); g.clipRect(10, 10, 100, 100); g.drawImage(img2, 10, 10, this); g.setClip(oldclip);

  33. Java Media APIs • Java2D • Java Advanced Imaging (JAI) • Java Media Framework (JMF) • Java3D

  34. java.awt.image • Image is an abstract class • Image objects are constructed in a platform specific manner • Two types of image objects: • Images created from an image source • Images created by an AWT component • Component.createImage()

  35. Processing Images in AWT ImageProducer PixelGrabber MemoryImageSource MemoryImageSource

  36. JAI models • The producer/consumer model • 基本的 AWT 影像 model • The immediate mode model • 進階的 AWT 影像 model • The pipeline model • The JAI 所使用的影像 model Push model Pull model

  37. BufferedImage java.awt.image.BufferedImage Color Model Raster ColorSpace SampleModel DataBuffer Encapsulates the methods for translating a pixel value to color components (ex. red/green/blue) and an alpha component for rendering

  38. DataBuffer • java.awt.image.DataBuffer • DataBuffer stores pixel data as one or more arrays of primitive data types. • Java2D buffer types: • Byte • Integer • Short • Unsigned short • JAI buffer types: • Double • Float

  39. Sample Model • SampleMode describes how pixels are stored in the DataBuffer. • Packed Models • Single-pixel packed model • Multiple-pixel packed sample model • Component Models • Component sample model

  40. ColorModel • Converts raster pixel samples into color components for displaying • Pixel samples are packed into a short integer (16 bits). Each sample would be 5 bits. • If displayed on a color monitor (0-255) per channel, the resulting image would appear dark. • Color model handles the conversion.

  41. ColorSpaces • Device independent/dependent • Converts between devices • Supports many color spaces including: • CMYK • HSV (HSB) • CIEXYZ • sRGB • ICC Color Profile • Create ColorSpace via factory

  42. Code Example Creating an Image

  43. Alpha Channel • A mixing factor to control the linear interpolation of foreground and background colors. • Varies from 0 to 1 • 0 indicates no coverage (clear) • 1 indicates full coverage • Each sample is multiplied by the alpha channel

  44. Transformations • Definitions: • Transformation is a function that maps an object from one point to another. • Affine transform preserves: • Finiteness • Parallelism • Transformations: • Translation • Scaling • Rotation • Reflection • Shear

  45. Translation y x

  46. Scaling y x

  47. Rotation y x

  48. Reflection y x

  49. Shear y x

  50. Transformations • Transformations can be concatenated: • C = Translate * Scale * Rotate * Shear • Java Classes: • AffineTransform ( java.awt.geom ) • AffineTransformOp ( java.awt.image )

More Related