diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1510c24..94ed8f4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + @@ -18,24 +19,38 @@ - - + + + + + + + + + + - - + + - - + + + + + + + + @@ -181,11 +196,12 @@ + - @@ -209,13 +225,12 @@ - + - + - @@ -227,6 +242,7 @@ + @@ -246,18 +262,25 @@ + - - + + - - + + + + + + + + @@ -268,6 +291,7 @@ + @@ -278,8 +302,14 @@ - - + + + + + + + + @@ -290,6 +320,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -301,22 +361,34 @@ - - + + - - + + + + + + + + + + + + + - - + + + diff --git a/collage.jpeg b/collage.jpeg new file mode 100644 index 0000000..eb0a374 Binary files /dev/null and b/collage.jpeg differ diff --git a/src/com/company/Main.java b/src/com/company/Main.java index d0d1fd0..f7998bf 100644 --- a/src/com/company/Main.java +++ b/src/com/company/Main.java @@ -3,38 +3,75 @@ import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.awt.*; +import java.util.ArrayList; public class Main { public static void main(String[] args) throws IOException { - File file = new File("C:\\Users\\Elliot\\Desktop\\bear.jpeg"); + File file = new File("C:\\Users\\Elliot\\Desktop\\bear.jpg"); FileInputStream fis = new FileInputStream(file); BufferedImage image = ImageIO.read(fis); //reading the image file - int rows = 20; - int cols = 30; - int chunks = rows * cols; + ArrayList repPaths = getAllImages(new File("C:\\Users\\Elliot\\Desktop\\test\\")); + + int rows = 3; + int cols = 3; int chunkWidth = image.getWidth() / cols; // determines the chunk width and height int chunkHeight = image.getHeight() / rows; int count = 0; - chunk[] imgs = new chunk[chunks]; + + chunk[] chunks = new chunk[rows * cols]; + chunk[] replacements = new chunk[rows * cols]; + for (int x = 0; x < rows; x++) { for (int y = 0; y < cols; y++) { //Initialize the image array with image chunks BufferedImage tempimg = new BufferedImage(chunkWidth, chunkHeight, image.getType()); - imgs[count] = new chunk(tempimg); - imgs[count].average(); - + chunks[count] = new chunk(tempimg); // draws the image chunk - Graphics2D gr = imgs[count++].getImg().createGraphics(); + Graphics2D gr = chunks[count++].getImg().createGraphics(); gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null); gr.dispose(); + + chunks[count-1].average(); } } - ImageIO.write(imgs[2].getImg(), "jpg", new File("collage.jpg")); + + for (int i = 0; i < cols; i++) { + // read file from path + File tempfile = new File(repPaths.get(i)); + FileInputStream tempfis = new FileInputStream(tempfile); + + // fill array of chunks with read images + BufferedImage tempimg = ImageIO.read(tempfis); + replacements[i] = new chunk(tempimg); + } + + ImageIO.write(chunks[8].getImg(), "jpeg", new File("collage.jpeg")); } + + /** + * Returns all jpg images from a directory in an array. + * Source: http://www.java2s.com/Code/Java/2D-Graphics-GUI/Returnsalljpgimagesfromadirectoryinanarray.htm + * @param directory the directory to start with + * @return an ArrayList containing all the file paths or nul if none are found.. + * @throws IOException + */ + public static ArrayList getAllImages(File directory) throws IOException { + ArrayList resultList = new ArrayList(256); + File[] f = directory.listFiles(); + for (File file : f) { + if (file != null && file.getName().toLowerCase().endsWith(".jpg")) { + resultList.add(file.getCanonicalPath()); + } + } + if (resultList.size() > 0) + return resultList; + else + return null; + } } diff --git a/src/com/company/chunk.java b/src/com/company/chunk.java index 66f7da7..dd893cd 100644 --- a/src/com/company/chunk.java +++ b/src/com/company/chunk.java @@ -3,23 +3,40 @@ package com.company; import java.awt.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; + public class chunk { private BufferedImage img; + private int[] average; chunk(BufferedImage Img){ - this.img = Img; + this.img = Img; + this.average = this.average(); } +/* +* GETTER AND SETTER +*/ public BufferedImage getImg() { return img; } public void setImg(BufferedImage img) { this.img = img; } + + public int[] getAverage() { + return average; + } + public void setAverage(int[] average) { + this.average = average; + } + + /* +* OTHER METHODS +*/ public int[] average(){ - int r = 0, g = 0, b = 0, a = 0, x= 0, y = 0; + int r = 0, g = 0, b = 0, a = 0, x= 0, y = 0, pc = 0; for(; y < this.img.getHeight(); y++){ for(; x < this.img.getWidth(); x++){ @@ -27,27 +44,21 @@ public class chunk { //get pixel value int p = this.img.getRGB(x,y); - //get alpha - a = (p>>24) & 0xff; - - //get red - r = (p>>16) & 0xff; - - //get green - g = (p>>8) & 0xff; - - //get blue - a = p & 0xff; - + a += (p>>24) & 0xff; + r += (p>>16) & 0xff; + g += (p>>8) & 0xff; + b += p & 0xff; + pc++; } } - r /= x*y; - g /= x*y; - b /= x*y; - a /= x*y; + r /= pc; + g /= pc; + b /= pc; + a /= pc; - System.out.println(r + " , " + g + " , " + b + " , " + a); - return new int[]{r,g,b}; + //System.out.println(r + " , " + g + " , " + b + " , " + a); + return new int[]{r,g,b,a}; } + }