【问题标题】:Scale image with Vaadin and Java使用 Vaadin 和 Java 缩放图像
【发布时间】:2015-10-05 22:11:02
【问题描述】:

我有以下代码可以上传图片并在网页上显示

// Show uploaded file in this placeholder
final Embedded image = new Embedded("Uploaded Image");
image.setVisible(false);

// Implement both receiver that saves upload in a file and
// listener for successful upload
class ImageUploader implements Receiver, SucceededListener {
    public File file;

    public OutputStream receiveUpload(String filename, String mimeType) {
        // Create upload stream
        FileOutputStream fos = null; // Stream to write to
        try {
            // Open the file for writing.
            file = new File(tmp_dir + "/" + filename);
            fos = new FileOutputStream(file);
        } catch (final java.io.FileNotFoundException e) {                  
            return null;
        }
        return fos; // Return the output stream to write to
    }

    public void uploadSucceeded(SucceededEvent event) {     
        // Show the uploaded file in the image viewer
        image.setVisible(true);             
        image.setSource(new FileResource(file));                
    }
};
ImageUploader receiver = new ImageUploader(); 

// Create the upload with a caption and set receiver later
Upload upload = new Upload("Upload Image Here", receiver);
upload.setButtonCaption("Start Upload");
upload.addSucceededListener(receiver);  

final FormLayout fl = new FormLayout();
fl.setSizeUndefined();
fl.addComponents(upload, image);

问题是,它显示了全分辨率,我想将其缩放(因此保持成比例)到 180 像素宽度。图片还需要保存为原始文件名_resized.jpg,但我似乎无法按比例缩放。网络上的一些指南讨论了调整大小(但随后图片被扭曲)或者它给 Vaadin 带来了一些问题。

更新: 添加了 Scarl jar(来自 this answer)),因为使用以下代码会很容易:

BufferedImage scaledImage = Scalr.resize(image, 200);

但这会产生以下错误:

The method resize(BufferedImage, int, BufferedImageOp...) in the type Scalr is not applicable for the arguments (Embedded, int)

我无法投射,因为 Cannot cast from Embedded to BufferedImage 错误

更新:使用以下代码,我可以转换为正确的类型

File imageFile = (((FileResource) (image.getSource())).getSourceFile());
BufferedImage originalImage = ImageIO.read(imageFile) ;

BufferedImage scaledImage = Scalr.resize(originalImage, 200);

但现在我无法显示图像..

final FormLayout fl = new FormLayout();
fl.setSizeUndefined();       
fl.addComponents(upload, scaledImage);

因为错误The method addComponents(Component...) in the type AbstractComponentContainer is not applicable for the arguments (Upload, BufferedImage)

【问题讨论】:

    标签: java vaadin


    【解决方案1】:

    您不能直接将 Vaadin 对象与第三方工具(如 Scalr)一起使用,而无需调整其中的一个。 “Embedded”是一个 Vaadin 类,而 SclaR 需要一个“BufferedImage”。

    所以,首先需要从 Embedded 对象中提取 File 对象:

    File imageFile = ((FileResource)(image.getSource()).getSourceFile();
    

    然后,使用 ImageIO 将其加载到 BufferedImage 中,如您指向的链接中所述(What is the best way to scale images in Java?

    BufferedImage img = ImageIO.read(...); // load image
    

    然后,您就有了您正在寻找的 BufferedImage 对象。

    【讨论】:

    • 必须添加一些括号,使其变为 File imageFile = (((FileResource) (image.getSource())).getSourceFile());现在我可以转换为 BufferedImage 但现在我的 fl.addComponents(upload, scaledImage);给出 AbstractComponentContainer 类型中的方法 addComponents(Component...) 不适用于参数(Upload、BufferedImage)。我会更新问题
    猜你喜欢
    • 2021-07-25
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 2016-10-17
    • 2012-03-09
    相关资源
    最近更新 更多