package imagetools; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.imageio.ImageIO; /** * Converts an Image to Grey Scale * * @asevans 7/29/2012 */ public class Greyscale implements Proximity{ private Image image=Image.getInstance(); /** * Empty constructor */ public Greyscale() { // TODO Empty Constructor (need a path and an image) } /** * Constructor with Buffered Image * @param img */ public Greyscale(BufferedImage img) { // TODO constructor with only buffered image if (img != null) { image.setImage(img); } else { // without an image provided, throw a new exception try { throw new NoImgException(); } catch (NoImgException e) { e.printStackTrace(); } } } /** * Constructor with BufferedImage and file path * @param inimg * @param inpath */ public Greyscale(BufferedImage inimg, String inpath) { // TODO takes in a buffered image and converts it if (inimg != null) { image.setImage(inimg); image.setPath(inpath); convert(); } else { // if no image is provided, throw exception try { throw new NoImgException(); } catch (NoImgException e) { e.printStackTrace(); } } } /** * Constructor with fpath * @param image_path */ public Greyscale(String inpath) { // TODO takes in a path, creates a buffered image, converts the image Pattern p=Pattern.compile("(?mi)(\\.jpg|\\.jpeg|\\.gif|\\.bmp)"); Matcher m=p.matcher(inpath); if(m.find()){ image.setPath(inpath); try { image.setImage(ImageIO.read(new File(image.getPath()))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ try{ throw new FileTypeException("Image must be a jpg, gif, bmp, or jpeg"); }catch(FileTypeException e){ e.printStackTrace(); } } } /** * Set a Buffered Image * @param inimg */ @Override public void setImage(BufferedImage inimg) { // TODO set the image using a buffered image if (inimg != null) { image.setImage(inimg); } else { // without an image, throws an exception try { throw new NoImgException(); } catch (NoImgException e) { e.printStackTrace(); } } } /** * Create an image from a file path */ @Override public void setImage(String inpath) { // TODO set the image using a path Pattern p=Pattern.compile("(?mi)(\\.jpg|\\.jpeg|\\.gif|\\.bmp)"); Matcher m=p.matcher(inpath); if(m.find()){ image.setPath(inpath); try { image.setImage(ImageIO.read(new File(image.getPath()))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ try{ throw new FileTypeException("Image must be a jpg, gif, bmp, or jpeg"); }catch(FileTypeException e){ e.printStackTrace(); } } } /** * Set the image file path */ @Override public void setPath(String inpath){ image.setPath(inpath); } /** * Return the image path */ @Override public String getPath(){ return image.getPath(); } /** * Get the Buffered Image * @return */ @Override public BufferedImage getBufferedImage() { // TODO return the image for viewing return image.getImage(); } /** * Get the Image object */ @Override public Image getImage(){ return image; } /** * */ public void convert_image() { convert(); } /** * Convert an image to greyscale */ private void convert() { // TODO convert the image to greyscale BufferedImage proxyimage=image.getImage(); if(proxyimage != null){ // conver the image to greyscale by rendering it to the image buffer // via drawImage BufferedImage img = new BufferedImage(proxyimage.getWidth(),proxyimage.getHeight(), BufferedImage.TYPE_BYTE_GRAY); proxyimage.getGraphics().drawImage(proxyimage, 0, 0, null); image.setImage(img); } } /** * Save the Image */ @Override public void save() { // TODO save the image image.save(); } /** * Delete the image */ @Override public void delete(){ //TODO delete the image image.delete(); } }