I'm currently trying to split the image, and I came across a snitch, and I have no idea why this is happening.
Here's a quick breakdown of the pseudo codes of my function
- Read on the image using the ImageIO.read method (File)
- Separate the images using the getSubimage () method as follows:
bufferedImage.getSubimage (300, 300, bufferedImage.getWidth () / columns, bufferedImage.getHeight () / rows);
- Write it to the image directory using the ImageIO.write () method.
The problem is that the int x and int y parameters do not seem to be read correctly by the program. For example, with 300, 300 as arguments above, but it does not seem to be clipped from the coordinates 300, 300, but rather from 0, 0 no matter what values you entered.
Any suggestions!
Thanks!
Btw, here is the code in my method:
public static void splitImage(String imageFileName, String format, int rows, int columns) { // Load the image file File imageFile = new File(imageFileName); try { BufferedImage bufferedImage = ImageIO.read(imageFile); // Split the image up into corresponding number of sub-images BufferedImage[][] splitImages = new BufferedImage[rows][columns]; for (int i = 0; i < splitImages.length; i++) { for (int j = 0; j < splitImages[i].length; j++) { splitImages[i][j] = bufferedImage.getSubimage(bufferedImage.getWidth() / columns * i, bufferedImage.getHeight() / rows * j, bufferedImage.getWidth() / columns, bufferedImage.getHeight() / rows); } } System.out.println(bufferedImage.getWidth() / columns + "\n" + bufferedImage.getHeight() / rows); splitImages[0][0] = bufferedImage.getSubimage(300, 300, bufferedImage.getWidth() / columns * 2, bufferedImage.getHeight() / rows * 2); // Write that into the images directory for (int i = 0; i < splitImages.length; i++) { for (int j = 0; j < splitImages[i].length; j++) { imageName++; ImageIO.write(splitImages[i][j], format, new File("images/" + imageName + "." + format)); } } ImageIO.write(splitImages[0][0], format, new File("images/" + imageName + "." + format)); } catch (IOException e) { JOptionPane.showMessageDialog(null, "The image file doesn't exist!"); } }
It seems that the problem is not in the method, but in the file format problem. With GIF, this did not work. With JPEG files, it worked fine.
Can someone explain why?
Thanks!