简体   繁体   English

旋转缓冲图像

[英]Rotate BufferedImage

I'm following a textbook and have become stuck at a particular point.我正在学习教科书,但在某个特定点上卡住了。

This is a console application.这是一个控制台应用程序。

I have the following class with a rotate image method:我有以下带有旋转图像方法的类:

public class Rotate {
    public ColorImage rotateImage(ColorImage theImage) {
        int height = theImage.getHeight();
        int width = theImage.getWidth();
        //having to create new obj instance to aid with rotation
        ColorImage rotImage = new ColorImage(height, width);
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                Color pix = theImage.getPixel(x, y);
                rotImage.setPixel(height - y - 1, x, pix);
            }
        }
        //I want this to return theImage ideally so I can keep its state
        return rotImage;
    }
}

The rotation works, but I have to create a new ColorImage (class below) and this means I am creating a new object instance (rotImage) and losing the state of the object I pass in (theImage).旋转有效,但我必须创建一个新的 ColorImage(下面的类),这意味着我正在创建一个新的对象实例(rotImage)并丢失我传入的对象(theImage)的状态。 Presently, it's not a big deal as ColorImage does not house much, but if I wanted it to house the state of, say, number of rotations it has had applied or a List of something I'm losing all that.目前,这不是什么大问题,因为 ColorImage 容纳的内容不多,但是如果我想让它容纳状态,例如,它已应用的旋转次数或我正在丢失所有内容的列表。

The class below is from the textbook.下面的课来自课本。

public class ColorImage extends BufferedImage {
    public ColorImage(BufferedImage image) {
        super(image.getWidth(), image.getHeight(), TYPE_INT_RGB);
        int width = image.getWidth();
        int height = image.getHeight();
        for (int y = 0; y < height; y++)
            for (int x = 0; x < width; x++)
                setRGB(x, y, image.getRGB(x, y));
    }

    public ColorImage(int width, int height) {
        super(width, height, TYPE_INT_RGB);
    }

    public void setPixel(int x, int y, Color col) {
        int pixel = col.getRGB();
        setRGB(x, y, pixel);
    }

    public Color getPixel(int x, int y) {
        int pixel = getRGB(x, y);
        return new Color(pixel);
    }
}

My question is, how can I rotate the image I pass in so I can preserve its state?我的问题是,如何旋转传入的图像以便保留其状态?

Unless you limit yourself to square images or to 180° rotations, you need a new object, as the dimensions would have changed.除非您将自己限制为方形图像或 180° 旋转,否则您需要一个新对象,因为尺寸会发生变化。 The dimensions of a BufferedImage object, once created, are constant. BufferedImage 对象的尺寸一旦创建,就是常数。

If I wanted it to house the state of, say, number of rotations it has had applied or a List of something I'm losing all that如果我希望它包含它所应用的旋转次数的状态,或者我正在失去所有东西的列表

You can create another class to hold that other information along with the ColorImage/BufferedImage, then limit the ColorImage/BufferedImage class itself to holding only the pixels.您可以创建另一个类来与 ColorImage/BufferedImage 一起保存其他信息,然后将 ColorImage/BufferedImage 类本身限制为仅保存像素。 An example:一个例子:

class ImageWithInfo {
    Map<String, Object> properties; // meta information
    File file; // on-disk file that we loaded this image from
    ColorImage image; // pixels
}

Then you can replace the pixels object freely, while preserving the other state.然后你可以自由地替换像素对象,同时保留其他状态。 It's often helpful to favor composition over inheritance . 倾向于组合而不是继承通常是有帮助的。 In brief that means, instead of extending a class, create a separate class that contains the original class as a field.简而言之,这意味着,不要扩展类,而是创建一个包含原始类作为字段的单独类。

Also note that the rotation implementation from your book seems to be mainly for learning purposes.另请注意,您书中的轮换实现似乎主要用于学习目的。 It's fine for that, but will show its performance limitations if you manipulate very big images or for continuous graphics rotation at animation speeds.这很好,但如果您操作非常大的图像或以动画速度连续旋转图形,则会显示其性能限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM