How to Split an Image into Chunks - Java ImageIO

    技术2022-05-19  19

    http://kalanir.blogspot.com/2010/02/how-to-split-image-into-chunks-java.html

     

    Image splitting and concatenating and other image manipulation techniques are important in parallel computing. Say we are receiving small chunks of an Image which is being manipulated parallely. In such scenarios, we need to concatenate those chunks together and vice versa.There is a pretty straight forward way to split an image using Java imageio package. Say you need to split following image into several chunks (you should decide the no. of rows and columns needed).

    Now I am going to split this image into 16 chunks (4 rows and 4 columns). I have shown the code snippet below. view plain print ? import javax.imageio.ImageIO;  import java.awt.image.BufferedImage;  import java.io.*;  import java.awt.*;    public class ImageSplitTest {      public static void main(String[] args) throws IOException {            File file = new File("bear.jpg"); // I have bear.jpg in my working directory          FileInputStream fis = new FileInputStream(file);          BufferedImage image = ImageIO.read(fis); //reading the image file            int rows = 4//You should decide the values for rows and cols variables          int cols = 4;          int chunks = rows * cols;            int chunkWidth = image.getWidth() / cols; // determines the chunk width and height          int chunkHeight = image.getHeight() / rows;          int count = 0;          BufferedImage imgs[] = new BufferedImage[chunks]; //Image array to hold image chunks          for (int x = 0; x < rows; x++) {              for (int y = 0; y < cols; y++) {                  //Initialize the image array with image chunks                  imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());                    // draws the image chunk                  Graphics2D gr = imgs[count++].createGraphics();                  gr.drawImage(image, 00, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);                  gr.dispose();              }          }          System.out.println("Splitting done");            //writing mini images into image files          for (int i = 0; i < imgs.length; i++) {              ImageIO.write(imgs[i], "jpg"new File("img" + i + ".jpg"));          }          System.out.println("Mini images created");      }  }    Parameter list of "drawImage()" (See line 28) -- image - The source image -- 0, 0 - X,Y coordinates of the 1st corner of destination image -- chunkWidth, chunkHeight - X,Y coordinates of the opposite corner of destination image  -- chunkWidth * y, chunkHeight * x - X,Y coordinates of the 1st corner of source image block -- chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight - X,Y coordinates of  the opposite corner of source image block Now you'll see 16 image chunks formed in your working directory. You might also like: How to merge multiple images into one image - Java ImageIOHow to Replace Strings in Java - Using java.util.regex packageHow to Print from Java (JPS) - javax.print PackageHow to Write a Custom Class Loader to Load Classes from a Jar


    最新回复(0)