Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

swing - How to crop image in java?

I'm using java to crop image when upload file, I set value and to try to crop but is not get correct size of image as I expected

This my code: (updated)

private BufferedImage cropImageSquare(byte[] image) throws IOException {        
    InputStream in = new ByteArrayInputStream(image);
    BufferedImage originalImage = ImageIO.read(in);

    System.out.println("Original Image Dimension: "+originalImage.getWidth()+"x"+originalImage.getHeight());            

    BufferedImage croppedImage = originalImage.getSubimage(300, 150, 500, 500);
    System.out.println("Cropped Image Dimension: "+croppedImage.getWidth()+"x"+croppedImage.getHeight());


     return croppedImage;
}

my photo:

enter image description here

I want to crop image as above image (red line) but my code is seem incorrect.

How to crop image as expect?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I want to crop image as above image (red line) but my code is seem incorrect.

So, your input image is 1024x811 and your "target" image is 928x690, which is roughly 0.906x0.8509 reduction/difference - so the real question is ... which one of those is the right value?

Through my testing, based on this image, 0.8509 produces the best result

FromTo

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Test {

    public static void main(String[] args) throws IOException {
        BufferedImage crop = new Test().crop(0.8509);
        System.out.println(crop.getWidth() + "x" + crop.getHeight());
        ImageIO.write(crop, "jpg", new File("Square.jpg"));
    }

    public BufferedImage crop(double amount) throws IOException {
        BufferedImage originalImage = ImageIO.read(Test.class.getResource("Cat.jpg"));
        int height = originalImage.getHeight();
        int width = originalImage.getWidth();

        int targetWidth = (int)(width * amount);
        int targetHeight = (int)(height * amount);
        // Coordinates of the image's middle
        int xc = (width - targetWidth) / 2;
        int yc = (height - targetHeight) / 2;

        // Crop
        BufferedImage croppedImage = originalImage.getSubimage(
                        xc, 
                        yc,
                        targetWidth, // widht
                        targetHeight // height
        );
        return croppedImage;
    }

}

Now, this doesn't do any checks (xc + targetWidth > imageWidth), but I'm sure you can fill that out


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...