Page 205 - The Definitive Guide to Building Java Robots
P. 205

Preston_5564C06.fm  Page 186  Friday, September 23, 2005  5:13 AM



                 186    CHAPTER 6  ■  VISION



                                } catch (Exception e) {
                                    e.printStackTrace();
                                    System.exit(0);
                                }
                            }
                        }


                        Code Objective
                        The code objective here is to convert an image from an RGB image to a greyscale image.


                        Code Discussion
                        The class containing all the image processing methods is called ImageProcessor. I’ve created a
                        factory method in this class called process(), which takes a FilterParameter called parms, and
                        then depending on its value, it calls the individual processing method. This is very similar to
                        the Java Advanced Imaging ParameterBlock and JAI.create() methods. See Example 6-11.


                        Example 6-11. ImageProcessor.java
                        package com.scottpreston.javarobot.chapter6;

                        import java.awt.Color;
                        import java.awt.Graphics2D;
                        import java.awt.Image;
                        import java.awt.RenderingHints;
                        import java.awt.Toolkit;
                        import java.awt.geom.AffineTransform;
                        import java.awt.image.BufferedImage;
                        import java.awt.image.renderable.ParameterBlock;

                        import javax.media.jai.Histogram;
                        import javax.media.jai.JAI;
                        import javax.media.jai.KernelJAI;
                        import javax.media.jai.PlanarImage;

                        public class ImageProcessor {

                            private BufferedImage lastImage;

                            public static final String FILTER_RGB_TO_GREY = "1";
                            public static final String FILTER_MOTION = "2";
                            public static final String FILTER_COLOR = "3";
                            public static final String FILTER_THRESHHOLD = "4";
                            public static final String FILTER_THRESHHOLD_COLOR = "5";
                            public static final String FILTER_COLOR_RATIO = "6";
                            public static final String FILTER_EDGE = "7";
   200   201   202   203   204   205   206   207   208   209   210