Noosrep
Noosrep

Reputation: 416

Scale image with Vaadin and Java

I have following code to upload an image and show it on the webpage

// 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);

The problem is, it shows the full resolution and I want to scale (so it remains proportional) it down to 180px width. The picture also needs to be saved as the original filename_resized.jpg but I can't seem to get it to scale. Several guides on the web talk about resizing (but then the picture gets distorted) or it gives some issues with Vaadin.

Update: Added the scarl jar (from this answer)) because it would be easy-peasy then by using following code:

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

but that gives following error:

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

and I cannot cast because Cannot cast from Embedded to BufferedImage error

Update: with following code I can cast to the right type

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

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

but now I can't show the image..

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

because of error The method addComponents(Component...) in the type AbstractComponentContainer is not applicable for the arguments (Upload, BufferedImage)

Upvotes: 2

Views: 2602

Answers (1)

Joel
Joel

Reputation: 2404

You cannot use Vaadin objects directly with a third-party tool such as Scalr without adapting one to the other. "Embedded" is a Vaadin class whereas SclaR expects a "BufferedImage".

So, you first need to extract the File object from the Embedded object:

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

Then, load it into the BufferedImage using ImageIO, such as explained in the link you pointed at ( What is the best way to scale images in Java? )

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

Then, you have the BufferedImage object you were looking for.

Upvotes: 1

Related Questions