有关BufferedImage.getSubimage(int x,int y,int w,int h)方法的指导? [英] Guidance on the BufferedImage.getSubimage(int x, int y, int w, int h) method?

查看:1704
本文介绍了有关BufferedImage.getSubimage(int x,int y,int w,int h)方法的指导?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在试图分割一张图片,我遇到了一个小游戏,我不知道它为什么会发生。



下面是我的功能的一个快速的伪代码分解


  1. 阅读图片使用ImageIO.read(File file)方法

  2. 使用getSubimage()方法分割图像,如下所示:

bufferedImage.getSubimage(300,300,
bufferedImage.getWidth()/ columns,bufferedImage.getHeight()/ rows);


  1. 使用ImageIO.write()方法将它写入images目录。

问题在于程序似乎没有正确读取int x和int y参数。例如,使用300,300作为上面的参数,但不管您输入的是什么值,它似乎都不会从坐标300,300,而是从0,0裁剪。

任何建议!



谢谢!

顺便说一句,这是我的方法中的代码:

pre $ public static void splitImage(String imageFileName,String format,int rows,int columns) {
//载入图像文件
File imageFile = new File(imageFileName);

尝试{
BufferedImage bufferedImage = ImageIO.read(imageFile);
//将图像分成相应数量的子图像
BufferedImage [] [] splitImages = new BufferedImage [rows] [columns]; (int j = 0; j< splitImages [i] .length; j ++){
$ b for(int i = 0; i< splitImages.length; i ++) b $ b 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 +\\\
+ bufferedImage.getHeight()/ rows);

splitImages [0] [0] = bufferedImage.getSubimage(300,300,
bufferedImage.getWidth()/ columns * 2,bufferedImage.getHeight()/ rows * 2);

//写入图像目录

(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,图像文件不存在!);


$ / code $ / pre

$ hr

看起来这不是方法的问题,因为它是文件格式的问题。使用GIF,它不起作用。用JPEG,它工作正常。



有人可以解释为什么吗?

谢谢!

解决方案

这是Java的一个bug。我相信它已经在JDK 7中得到了修复,但我并不是100%确定的(我认为实际的修复在9月的某个时候会被重新审查)



http://bugs.sun.com/view_bug.do?bug_id=6795544


I'm currently attempting to split an image, and I ran into a snitch and I have no idea why it's happening.

Here's a quick pseudo code breakdown of my function

  1. Read in the image using the ImageIO.read(File file) method
  2. Split the images up using the getSubimage() method as follows:

bufferedImage.getSubimage(300, 300, bufferedImage.getWidth() / columns, bufferedImage.getHeight() / rows);

  1. Write it to the images directory using the ImageIO.write() method.

The problem is that the int x and int y parameters don't seem to be read correctly by the program. For example, with 300, 300 as the arguments above, but it doesn't seem to crop from the coordinates 300, 300, but rather from 0, 0 regardless of what values you input.

Any suggestions!

Thanks!

Btw, here's 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 it wasn't the method's problem as it was the file format's problem. With GIFs, it didn't work. With JPEGs, it worked fine.

Can somebody explain why?

Thanks!

解决方案

This is a bug with Java. I believe it is fixed in JDK 7 but I am not 100% sure (I think the actually fix was reviewed sometime in september)

http://bugs.sun.com/view_bug.do?bug_id=6795544

这篇关于有关BufferedImage.getSubimage(int x,int y,int w,int h)方法的指导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆