CMT107 Visual Computing
CMT107 Visual Computing
Copyright By PowCoder代写 加微信 powcoder
Image Processing in Java
School of Computer Science and Informatics
Cardiff University
• Images in Java
• Read (Load) an image
• Draw an image
• Process an image
• Write (Save) an image
CMT107 Visual Computing 2
Images in Java
• An image is typically a rectangular two-dimensional array of pixels
• Each pixel represents the colour at that position
• Dimensions represents the horizontal extent (width) and vertical extent (height) as it is
displayed.
• Most important image class in Java 2D API
• java.awt.image.BufferedImage
• Image programming tasks:
• Load an external image file
• Draw an image onto a drawing surface
• Manipulate the pixels of an image
• Save the contents of an image to an external image file
CMT107 Visual Computing 3
Read (Load) an Image
• Use javax.imageio package
BufferedImage img = null;
img = ImageIO.read(new File(“Daffodil.jpg”));
} catch (IOException e) {
CMT107 Visual Computing 4
Draw an Image
• Use Graphics function
boolean Graphics.drawImage(Image img, int x, int y,
ImageObserver observer);
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
CMT107 Visual Computing 5
Process an Image
• The width and height of an image can be obtained by
width = img.getWidth();
height = img.getHeight();
• The pixel colour at (x,y) can be retrieved and set by
Color pixel = new Color(img.getRGB(x, y));
img.setRGB(x, y, pixel.getRGB());
CMT107 Visual Computing 6
Process an Image
• Example: convert a colour image to a grayscale image
for (int y = 0; y < height; y++){ for (int x = 0; x < width; x++) { Color pixel = new Color(img.getRGB(x, y)); int r = pixel.getRed(); int g = pixel.getGreen(); int b = pixel.getBlue(); r = g = b = (int) (0.299*r + 0.587*g + 0.114*b); //grayscale img_out.setRGB(x, y, (new Color(r, g, b)).getRGB(); CMT107 Visual Computing 7 Write (Save) an Image • Use javax.imageio package ImageIO.write(img_out, "jpg", new File("DaffodilG.jpg")); } catch (IOException ex) { CMT107 Visual Computing 8 • What is an image? • What is a pixel? • How to load and save an image? • How to draw an image? • How to access and set the pixels of an image? CMT107 Visual Computing 9 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com