package imagetools; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.imageio.ImageIO; public class Crop implements Proximity{ private Image image=Image.getInstance(); private ArrayList<byte[]> letters = new ArrayList<byte[]>(); private byte[] bimage; /** * empty constructor */ public Crop() { // TODO empty constructor } /** * crop from a byte array * @param img */ public Crop(byte[] img) { // TODO constructor with image byte array and sets an image bimage = img; createFromBytes(); } /** * Crop from an image path * @param ip */ public Crop(String inpath) { // TODO constructor with image byte array and sets an image createFromImagePath(inpath); } /** * Return the byte array arraylist containing the set of images to crop * @return */ public ArrayList<byte[]> getSet() { // TODO return the letter set return letters; } /** * Set a byte array arraylist of images to crop * @param inset */ public void setSet(ArrayList<byte[]> inset) { // TODO set the letter set letters = inset; } /** * Set the Buffered Image * @param inimg */ @Override public void setImage(BufferedImage inimg) { // TODO set image image.setImage(inimg); } /** * Get the Image Object */ @Override public Image getImage(){ return image; } /** * Get the Buffered Image * @return */ public BufferedImage getBufferedImage() { // TODO return image return image.getImage(); } /** * get the image as a byte array * @return */ public byte[] getByteArrImage() { // TODO return image bytes return bimage; } /** * Create a Buffered Image from a byte array * * @param bi */ public void setByteArrImage(byte[] bi) { // TODO set bimage bimage = bi; createFromBytes(); } /** * Set an image from a given path * @param inpath */ @Override public void setImage(String inpath) { // TODO set image path and image from string createFromImagePath(inpath); } /** * Set the image path */ @Override public void setPath(String path) { // TODO Auto-generated method stub image.setPath(path); } /** * Get the Image Path * @return */ @Override public String getPath() { // TODO return image path return image.getPath(); } /** * Create an image from an imag path */ private void createFromImagePath(String inpath) { // TODO create 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(); } } } private void createFromBytes() { // TODO set img from byte array using byte stream try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(bimage); image.setImage(ImageIO.read(bis)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void crop(int x, int y, int cropwidth, int cropheight) { // TODO call private crop with default rgb boolean set at true cropPic(x, y, cropwidth, cropheight, true); } public void crop(int x, int y, int cropwidth, int cropheight, Boolean rgb) { // TODO call cropPic with custom RGB value cropPic(x, y, cropwidth, cropheight, rgb); } private void cropPic(int x, int y, int width, int height, Boolean rgb) { // TODO crop the picture // the copied and buffered images BufferedImage proxyimage=image.getImage(); BufferedImage cimage = null; // copy image width and height (x,y) int i = 0; int j = 0; // setup the copy if (rgb == true) { // create rgb copy cimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); } else { // create greyscale copy cimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); } // perform copy while (x < (x + width)) { while (y < (y + height)) { if (x < proxyimage.getWidth() & x > 0 & y < proxyimage.getHeight() & y > 0) { cimage.setRGB(i, j, proxyimage.getRGB(x, y)); } } } // img reset image.setImage(cimage); } public void save() { // TODO save the image if the path and image are set image.save(); } @Override public void delete() { // TODO Auto-generated method stub image.delete(); } }