简体   繁体   English

将图像居中并将其裁剪为固定像素大小(java)

[英]Center an image and the crop it to a fixed pixel size (java)

I'm currently working on a project were the user can draw an image and a small neural network then guesses the drawn(in Java, trust me I wanted to do it in Python).我目前正在做一个项目,用户可以绘制图像和一个小型神经网络,然后猜测所绘制的内容(在 Java 中,相信我,我想用 Python 完成)。 For this, I exported the canvas using snapshot.为此,我使用快照导出了画布。 Now I want to center and crop a to a fixed size, let's say 50x50 pixel.现在我想将 a 居中并裁剪为固定大小,比如 50x50 像素。 Does Java have any methods for this? Java 有没有这方面的方法?

public void guess(ActionEvent actionevent) {

    SnapshotParameters params = new SnapshotParameters();
    params.setFill(Color.TRANSPARENT);
    WritableImage image = new WritableImage((int)canvas.getWidth(),(int)canvas.getHeight());
    image = canvas.snapshot(params, null);
    BufferedImage bufferedImage = new BufferedImage((int)image.getWidth(),(int)image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    bufferedImage = SwingFXUtils.fromFXImage(image, null);
    String str = NNInterface.initNN(bufferedImage);
    outputText.setText(str);
}

} }

Else I'd had to use double arrays or smth.否则我不得不使用双数组或 smth。

Thx谢谢

crop a to a fixed size, let's say 50x50 pixel.将 a 裁剪为固定大小,假设为 50x50 像素。 Does Java have any methods for this? Java 有没有这方面的方法?

Hmm, in your question you use the BufferedImage tag and the Image tag.嗯,在您的问题中,您使用了BufferedImage标签和Image标签。

In the BufferedImage class you find the getSubimage(...) method, which would allow you to crop the image.BufferedImage类中,您可以找到getSubimage(...)方法,该方法允许您裁剪图像。

In the Image class you find the getScaledInstance(...) method, which allows you to scale the image.Image类中,您可以找到getScaledInstance(...)方法,该方法允许您缩放图像。

One would assume you read the API before asking a question.人们会假设您在提出问题之前阅读了 API。 Maybe I don't understand the question?也许我不明白这个问题?

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

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