package imagetools; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.imageio.ImageIO; /** * Darken an Image. Beneficial if Gaussian blur has a large radius. * @author aevans 8/7/2013 */ public class Darken implements Proximity{ // the image private Image image=Image.getInstance(); /** * Empty Constructor */ public Darken() { // TODO empty constructor } /** * Constructor with an image * @param img */ public Darken(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 Buffered Image and path * @param img * @param inpath */ public Darken(BufferedImage img, String inpath) { // TODO set image with BufferedImage and run blur 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(); } } } /** * Constructor with only an fpath * @param inpath */ public Darken(String inpath) { // TODO constructor that takes in the path and attempts to create an // image and run blur 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 */ @Override public void setImage(BufferedImage img) { // TODO set image from buffered image image.setImage(img); } /** * Get the Buffered Image * @return */ public BufferedImage getBufferedImage() { // TODO returns a buffered image return image.getImage(); } /** * Get the Image Object */ @Override public Image getImage(){ return image; } /** * Set an image from a file path */ @Override public void setImage(String inpath) { // TODO set image from 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 path */ @Override public void setPath(String inpath) { // TODO set path and get file image.setPath(inpath); } /** * Get the image path */ @Override public String getPath(){ return image.getPath(); } /** * Set an image from a byte array * @param bytes */ public void setImage(byte[] bytes) { // TODO set image from bytes (warning: meta data will be stripped) // byte array to read in image ByteArrayInputStream bis = new ByteArrayInputStream(bytes); // attempt to read in image try { image.setImage(ImageIO.read(bis)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Darken the image */ public void darkenImage() { // TODO calls the brighten script do_darken(); } /** * Private darken method */ private void do_darken() { // TODO brightens the image BufferedImage proxyimage=image.getImage(); // picture width and height int width = proxyimage.getWidth(); int height = proxyimage.getHeight(); if (image == null) { // no image throw exception try { throw new NoImgException(); } catch (NoImgException e) { e.printStackTrace(); System.exit(-1); } } else { // brighten each pixel for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color c = new Color(proxyimage.getRGB(x, y)); proxyimage.setRGB(x, y, c.darker().getRGB()); } } } } /** * Get the image bytes * @return */ public byte[] getImageBytes() { // TODO returns a jpeg turned to bytes BufferedImage proxyimage=image.getImage(); // the byte array byte[] ibytes = null; // the byte array outputstream ByteArrayOutputStream baos = new ByteArrayOutputStream(); // convert image to byte array try { ImageIO.write(proxyimage, "jpg", baos); ibytes = baos.toByteArray(); baos.flush(); baos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // return byte array unless failure return ibytes; } /** * Save image */ @Override public void save() { // TODO save the image image.save(); } /** * Delete an image */ @Override public void delete(){ image.delete(); } }