【问题标题】:How to crop image in java?如何在java中裁剪图像?
【发布时间】:2018-11-06 19:29:47
【问题描述】:

我在上传文件时使用 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;
}

我的照片:

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

如何按预期裁剪图像?

【问题讨论】:

  • 您的代码以正方形形式裁剪,但图像中的红线清楚地显示了非正方形形式。你能澄清一下你真正想要什么吗?另外,您期望的正确尺寸是多少?你正在经历什么?
  • @Ben,是的,我明白了,图片只是我的代码的插图..
  • 那么这是一个非常糟糕的插图,除了混淆之外没有添加任何内容。还请添加我提到的其余要点。 “但尺寸不正确”并不是真正有用,因为我们不知道什么对您来说是正确的。
  • 您的代码使用较短的边作为正方形大小。您将只裁剪 2 面。这和你的想象不符。

标签: java swing image-processing crop


【解决方案1】:

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

所以,您的输入图像是1024x811,而您的“目标”图像是928x690,大致是0.906x0.8509 减少/差异 - 所以真正的问题是......其中哪一个是正确的值?

通过我的测试,基于这张图片,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),但我相信你可以填写它

【讨论】:

  • Tks,你太准确了,我可以按预期裁剪..如何增加图像的高度(无宽度)更多一点,例如:0.8509
  • 根据您的需要同时传递 widthheight
  • 我不明白,你能给我详细的身高值吗?
  • 所以,根据我的计算,输入和输出的差是0.906x0.8509,所以,假设你想单独修改宽和高,那么你需要为两者传递一个修饰符
  • 是的,我得到了你的解释^^,只是我的问题我不知道如何获得高度或宽度的值,我可以在哪里获得这个值?
【解决方案2】:

代码对我来说工作正常。

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(后两个参数)是工作正常的图像的尺寸。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2012-11-16
    • 2021-07-24
    相关资源
    最近更新 更多