开发者

Crop image in Java with a class?

I have been attempting to do this for about a week. Every single time I have tried something it failed. So I turned to copying others code... they said the code worked for them... yet it failed for me.

The piece of code that I ended up liking came from the following.

How To Crop Image in Java (StackOverflow)

So then from that I basically copied / made this.

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public class ImageEditor {

    public BufferedImage crop(BufferedImage src, Rectangle rect) {
        B开发者_StackOverflow中文版ufferedImage dest = new BufferedImage(rect.getWidth(), rect.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = dest.getGraphics();
        g.drawImage(src, 0, 0, rect.getWidth(), rect.getHeight(), rect.getX(), rect.getY(), rect.getX() + rect.getWidth(), rect.getY() + rect.getHeight(), null);
        g.dispose();
        return dest;
    }
}

I got the following errors with this code.

Crop image in Java with a class?

Crop image in Java with a class?

Thanks for the help in advance!


The first error says it can't find method drawImage(BufferedImage,int,int,double,double,double,double,double,double,<nulltype>). All those double values are coming from a Rectangle, right?

Graphics has a drawImage(BufferedImage,int,int,int,int,int,int,int,int,ImageObserver) method. That's probably the one you are trying to use. You should use int values instead.

The second error says it can't find constructor BufferedImage(double, double, int). This is a similar problem.

Rectangle exposes int precision fields x, y, height, and width. Can you use them? E.g., rect.x instead of rect.getX()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜