January 2006Daniel Bryant1 Image Processing in Java: A Technical Guide Daniel Bryant Dept. of Computing University of Surrey

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
January 2006Daniel Bryant1 Coursework Hints (part 2) : Week 4-5 Daniel Bryant Dept. of Computing University of Surrey
Java Tutorial – Building GUIs Java with Added Swing Daniel Bryant
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CS1101: Programming Methodology Aaron Tan.
Java Programming Robert Chatley William Lee
by Chris Brown under Prof. Susan Rodger Duke University June 2012
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Appendix E The EZJava.
Introduction to Java Programming with Forte Y. Daniel Liang.
How to Run a Java Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
COMP Exception Handling Yi Hong June 10, 2015.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction IS Outline  Goals of the course  Course organization  Java command line  Object-oriented programming  File I/O.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Creating a GUI Class An example of class design using inheritance and interfaces.
Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors.
OOP Basics Classes & Methods (c) IDMS/SQL News
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Introduction to Java Import Scanner class to use in our program
C++ Exceptions.
2.5 Another Java Application: Adding Integers
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CompSci 230 Software Construction
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Exception Handling Chapter 9.
How to Run a Java Program
IFS410 Advanced Analysis and Design
Java Tutorial – Application Building
How to Run a Java Program
Java Tutorial – Application Building
Object Oriented Programming in java
CMSC 202 Exceptions.
Corresponds with Chapter 5
Presentation transcript:

January 2006Daniel Bryant1 Image Processing in Java: A Technical Guide Daniel Bryant Dept. of Computing University of Surrey

January 2006Daniel Bryant2 The Purpose of the Tutorial Labs These labs are designed to help you! More information on the technical (Java) concepts that we will be looking at in the regular labs Introduce object-orientated/Java techniques I can also provide help with any problems from the regular labs or coursework If you want information about a specific topic/concept within Java or have any comments please let me know!

January 2006Daniel Bryant3 Resources Please download a copy of these slides from the module website An excellent book to learn Java from the beginning is  Head First Java (Second Edition) by Kathy Sierra and Bert Bates The Head First books contain a unique style (with lots of pictures and funny techniques), but they make learning a lot more enjoyable and really do work!

January 2006Daniel Bryant4 Resources You can download Java and NetBeans free for home use at Download Java 5.0 and NetBeans 5.5 (the newest version of NetBeans)

January 2006Daniel Bryant5 Today’s Lab Running code  Arguments  Working directory Coursework hints  Class variables  Methods (in particular for MirrorXReflection)

January 2006Daniel Bryant6 What happens when you specify an image name in the arguments? Arguments are essentially the parameters that are passed from the operating system into the Java Application that you are running (your Main class). In this example it is ImageViewer. NetBeans allows us to specify arguments in an easy way. If we didn’t have NetBeans we would have to invoke our Java application at the command line like this: java ImageViewer Argument1 Argument2 Argument3 N.B. Remember regardless of whether you use NetBeans or the command line the arguments must separated by spaces (not commas) and try not to use hyphens in the arguments. You would also have to set your classpath to include the two library JAR files

January 2006Daniel Bryant7 The Working Directory The working directory is the location where the Java Virtual Machine (JVM) will by default load or save any input/output from your application  We have seen in the first lab that the argument name ‘mattgrey.jpg’ or ‘l1.jpg’ was the name of an image located in the working directory (the Images directory) You can of course navigate through a file system in Java (and you are not restricted to doing everything in one directory), but we will cover this in a later lab

January 2006Daniel Bryant8 Processing the Arguments So, how is the specified image name argument transformed into a representation of the image in our application? Remember back to last semester – when you run a Java application, which method is run first? The answer is the main method, which always has a method signature like this:  public static void main(String[] argv)

January 2006Daniel Bryant9 Processing the Arguments Hopefully you can remember what the public static void part means, but the main item of interest here is the String[] argv This creates an array of Strings that contain all of the arguments specified at the command line or in NetBeans in the order they were supplied. For example if we supplied the arguments “matthew2.jpg output.txt” then the first position in the String array (argv[0]) would contain “matthew2.jpg” and the second (argv[1]) would contain “output.txt”. This is equivalent to the statements:  argv[0] = “matthew2.jpg”;  argv[1] = “output.jpg”;

January 2006Daniel Bryant10 The Examples The remaining slides assume that you have set up your NetBeans environment correctly and have loaded in a project that:  Contains the two JAR file Libraries specified in the first regular lab  Includes the Chap05 directories Remember in NetBeans the top toolbar contains useful icons: Run your Main Class Build and Clean Save all files

January 2006Daniel Bryant11 The First Coursework I would recommend creating a new Java class for each image operation that you have to implement So, for the ShrinkBySkipping operation I would create a new class called Shrink and copy the MirrorXReflection code into this class You will then have to alter the class name and the name of the constructor to the same name as the new Java file you have created (i.e. Shrink) You will then have to modify the code as described over the remainder of the slides

January 2006Daniel Bryant12 Coursework Hints I thought it would be useful to examine the MirrorXReflection class (which was adapted from Nick Efford’s Dither code) in more detail to help you incorporate the image operations as specified in the regular lab sheet MirrorXReflection contains 2 class variables and 4 methods (a constructor, a main method and two general methods)

January 2006Daniel Bryant13 Class Variables Recall from last semester’s labs class variables represent “state” in an object (such as a persons age or the current gear in a car) and can be accessed by all methods in the class Class variables can be marked private (only code in this class can access the variable) or public (code from any class/package can access the variable)

January 2006Daniel Bryant14 Class Variables In the MirrorXReflection class the two class variables are private BufferedImage sourceImage private ImageView[] views The type of the variable is BufferedImage – an internal representation of an image The name of the variable The type of the variable is an array (indicated by [ ]) of ImageView. More detail is included later in the slides

January 2006Daniel Bryant15 Class Methods The MirrorXReflection contains four methods - public MirrorXReflection(String imageFile) throws IOException, ImageDecoderException public void readImage(String filename) throws IOException, ImageDecoderException public BufferedImage xReflectionInPlace(BufferedImage image) public static void main(String[] argv)

January 2006Daniel Bryant16 Anatomy of a Method public BufferedImage xReflectionInPlace(BufferedImage image) { … } Access modifier – public, private etc. This determines if the method can be called outside of class it belongs to Return type – This is the type of the variable that will be returned to the calling code when the method completes. This example shows that when a call to the xReflectionInPlace method completes the statement that called the method will have access to a new BufferedImage variable. E.g. BufferedImage myImage = readImage(fileName); Parameters that are passed into the method. These parameters must be specified when calling the method E.g. xReflectionInPlace(sourceImage); You can include any number of parameters for a method e.g. public BufferedImage average(BufferedImage image, int n) must be passed a BufferedImage and an int (such as 5) i.e. BufferedImage image = average(image,5);

January 2006Daniel Bryant17 Anatomy of another Method public void readImage(String filename)throws IOException, ImageDecoderException Access modifier – public, private etc. This determines if the method can be called outside of class it belongs to Return type – void means no variable is returned and so when calling this method you use the call the method without loading the result into a variable readImage(filename); and NOT void voidValue = readImage(filename); Parameters that are passed into the code in the method. These parameters must be specified when calling the method E.g. readImage(“myImageFile.jpg”); Which Exceptions are thrown. These two specified Exceptions must be caught when calling the method using try/catch blocks like last semester try { readImage(fileName); } catch(IOException ioe) { … } catch(ImageDecoderException idx) {… }

January 2006Daniel Bryant18 MirrorXReflection public MirrorXReflection(String ImageFile)  This is the constructor method. Remember that the constructor name must match the name of the class exactly and also have no return type (i.e. not public void MirrorXReflection() or public String MirrorXReflection() )  The constructor method is called whenever a new object with this class type is created using the statement new MirrorXReflection(filename)  (This is located in the main method of our example)

January 2006Daniel Bryant19 Modifications Needed public MirrorXReflection(String imageFile) throws IOException, ImageDecoderException { super("xReflect: " + imageFile); readImage(imageFile); views = new ImageView[2]; views[0] = new ImageView(sourceImage); views[1] = new ImageView(xReflectionInPlace(sourceImage)); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.add(new JScrollPane(views[0]), "input"); tabbedPane.add(new JScrollPane(views[1]), "xReflect"); getContentPane().add(tabbedPane); addWindowListener(new WindowMonitor()); } You will need to change the name of the method being called (highlighted) for each image operation and you may also need to pass in different parameters to this method (i.e. shrink takes a BufferedImage and an int, such as 2) Create a tabbed pane and add the two ImageView variables from the views array

January 2006Daniel Bryant20 ImageView The ImageView class is included as part of the standard Java libraries and is intended to act in a manner similar to the image tag in HTML You can load a BufferedImage (named sourceImage in this example) in an ImageView variable  ImageView view = new ImageView(sourceImage); You can then add the ImageView variable to a window created in Java, much like including the tag in a piece of HTML code.

January 2006Daniel Bryant21 readImage public void readImage(String filename)  This method reads the image file (specified as a String variable filename parameter) from the file system and loads an internal representation of the image into the BufferedImage sourceImage class variable  This method currently includes code that converts the specified image into a grayscale picture. Can you identify this code?

January 2006Daniel Bryant22 xReflectionInPlace This is the method where the image processing takes place  Therefore when you create a new class for each image operation (shrink, average, etc…) you will have to replace this method Be aware that each method for an operation may require different parameters. For example, the average method requires that you pass in an array of BufferedImages and not just a single image  You will need to find out what the average operation does, how many images you will need to pass in and how to create an array (Hint look at how the ImageView array named views is created and loaded in the constructor)

January 2006Daniel Bryant23 public static void main(String[] argv) { if (argv.length > 0) { try { JFrame frame = new MirrorXReflection(argv[0]); frame.pack(); frame.setVisible(true); } catch (Exception e) { System.err.println(e); System.exit(0); } } else { System.err.println("usage: java MirrorXReflectionion "); System.exit(1); } main Check if the argv array contains more than one element (i.e. the number of arguments you have specified in NetBeans separated by a space) Try and create a JFrame (a GUI window) and load a new MirrorXReflection (with the argv[0] filename passed as a parameter) into that window Catch any exceptions (errors) and print the error to the terminal. Then exit the application If the argv array contains no elements print an error message to the terminal and shutdown the application

January 2006Daniel Bryant24 public static void main(String[] argv) { if (argv.length > 0) { try { JFrame frame = new MirrorXReflection(argv[0]); frame.pack(); frame.setVisible(true); } catch (Exception e) { System.err.println(e); System.exit(0); } } else { System.err.println("usage: java MirrorXReflectionion "); System.exit(1); } main – modifications needed You will need to modify this statement to load the results of the correct image operation into the JFrame variable Remember you may also have to pass in additional arguments to certain image operations (for example the extra int required for Shrink) and two or more images for the average operation

January 2006Daniel Bryant25 The remainder of the lab Have a go at the coursework… Hopefully, you have everything you need to complete Shrink and Enlarge You will require slightly different techniques for Average and Subtract due to processing multiple images

January 2006Daniel Bryant26 Extra Work You could also combine all the image operations (i.e. methods into a single class file)  To do this you will need to pass in appropriate arguments from NetBeans that will enable you to do all the operations (Hint: you may need to pass more than one image filename in the arguments) You could also ask the user to specify any values required (such as the n value on the Shrink methods) using a JOptionPane Dialogue Box like we used last semester