如何在 java 中裁剪图像?

How to crop image in java?

我在上传文件时使用 java 来裁剪图像,我设置了值并尝试裁剪但没有像我预期的那样得到正确的图像大小

这是我的代码:(已更新

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;
}

我的照片:

我想像上图(红线)那样裁剪图像,但我的代码似乎不正确。

如何按预期裁剪图像?

代码对我来说工作正常。

public class Test {

    public static void main( String[] args ) throws IllegalAccessException, InstantiationException {

        try{
              BufferedImage image = ImageIO.read(new File("C:\Users\guptab\Pictures\American.png"));
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              ImageIO.write(image, "png", baos);
             byte[] res=baos.toByteArray();
             image = new Test().cropImageSquare(res);

        } 
        catch(Exception e) {
             e.printStackTrace();
        System.out.println("Error");
        }
        }



    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, 300, 600);
        System.out.println("Cropped Image Dimension: "+croppedImage.getWidth()+"x"+croppedImage.getHeight());


         return croppedImage;
    }
}

输出为:

Original Image Dimension: 1279x1023
Cropped Image Dimension: 300x600

getSubImage方法的定义:

BufferedImage java.awt.image.BufferedImage.getSubimage(int x, int y, int w, int h)


Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.
Parameters:x the X coordinate of the upper-left corner of the specified rectangular regiony the Y coordinate of the upper-left corner of the specified rectangular regionw the width of the specified rectangular regionh the height of the specified rectangular regionReturns:a BufferedImage that is the subimage of this BufferedImage.

所以 int x 和 int y(前两个参数是图像的坐标,而不是尺寸),只有 int w,int h(最后两个参数)是图像的尺寸,工作正常。

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

因此,您的输入图像是 1024x811,您的 "target" 图像是 928x690,大致是 0.906x0.8509 reduction/difference - 所以真正的问题是...哪一个是正确的值?

根据我的测试,基于此图像,0.8509 产生了最好的结果

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;
    }

}

现在,这不会进行任何检查 (xc + targetWidth > imageWidth),但我相信您可以填写