Java – about bufferedimage Instructions for getsubimage (int x, int y, int w, int h) methods?
I'm trying to segment the image. I encountered a small fault. I don't know why it happened
This is a quick pseudo code breakdown of my function
>Using imageio Read the image using the read method > split the image using the getsubimage () method as follows:
bufferedImage. getSubimage(300,300, bufferedImage.getWidth()/ columns,bufferedImage. getHeight()/ rows);
>Using imageio The write () method writes it to the images directory
The problem is that the program does not seem to read the int x and int y parameters correctly For example, use 300300 as the above parameter, but it doesn't seem to clip from the coordinates 300300, but from 0,0, no matter what value you enter
Any suggestions!
thank you!
By the way, this 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,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],new File("images/" + imageName + "." + format)); } catch (IOException e) { JOptionPane.showMessageDialog(null,"The image file doesn't exist!"); } }
It seems that this is not a problem of method, because it is a problem of file format With GIF, it doesn't work Using JPEG, it works normally
Can anyone explain why?
thank you!
Solution
This is a Java error I believe it has been fixed in JDK 7, but I am not 100% sure (I think the actual fix has been reviewed sometime in September)
http://bugs.sun.com/view_bug.do?bug_id=6795544