java – 关于BufferedImage.getSubimage(int x,int y,int w,int
我正在尝试分割图像,我遇到了一个小故障,我不知道它为什么会发生.
这是我的函数的快速伪代码细分 >使用ImageIO.read(文件文件)方法读入图像 bufferedImage.getSubimage(300,300, >使用ImageIO.write()方法将其写入images目录. 问题是程序似乎没有正确读取int x和int y参数.例如,使用300,300作为上面的参数,但它似乎不从坐标300,300中裁剪,而是从0,0而不管您输入什么值. 有什么建议! 谢谢! 顺便说一下,这是我方法中的代码: 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!"); } } 似乎这不是方法的问题,因为它是文件格式的问题.有了GIF,它没有用.使用JPEG,它工作正常. 有人可以解释一下原因吗? 谢谢! 解决方法
这是Java的一个错误.我相信它已在JDK 7中得到修复,但我并不是100%肯定(我认为实际修复程序已在9月某个时候进行了审核)
http://bugs.sun.com/view_bug.do?bug_id=6795544 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |