Codename One: Save Image to Storage and create small rounded preview

801 views Asked by At

I have to problem with Images currently:

1) I can't save an Image to Storage because it is not supported to store it directly to the storage. I want users to be able to take a photo with the camera and then the created Photo has to be saved somewhere, so I can retrieve it later on again.

Could you tell me how a) To save an Image b) How to retrieve it

I found this Code piece made by Shai in Stackoverflow where another user asked how to save Images to Storage also.

InputStream stream = FileSystemStorage.getInstance().openInputStream(i);
OutputStream out = Storage.getInstance().createOutputStream("MyImage");
Util.copy(stream, out);
Util.cleanup(stream);
Util.cleanup(out);

But I don't understand it. How can I get an "Image" Object out of this? And how exactly is the "Image" Object stored there? Could you give me a full example here? This is really essential for my app.

2)

This question might be trivial, but I couldn't find an answer to it though. I want to rescale the taken camera picture and make a rounded mask around it. I tried this:

        Image capturedImage = Image.createImage(Capture.capturePhoto());
        int width = capturedImage.getWidth();
        int hight = capturedImage.getHeight();
        Image rounded = Image.createImage(width, capturedImage.getHeight(), 0xff000000);
        Graphics gr = rounded.getGraphics();
        gr.setColor(0xffffff);
        gr.fillArc(0, 0, width, hight, 0, 360);
        Object mask = rounded.createMask();
        Image ImagePreview = capturedImage.applyMask(mask);

But it did not work out, the ImagePreview wont even show up. This preview would be original-sized anyway, how would I resize it to a small preview for the user afterwards?

Thank you in advance.

Edit:

I have tried the following to save the image:

            Image capturedImage = Image.createImage(Capture.capturePhoto());
String imageFile = FileSystemStorage.getInstance().getAppHomePath() + "test.png";
            try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(imageFile)) {
                ImageIO.getImageIO().save(capturedImage, os, ImageIO.FORMAT_PNG, 1);
            } catch(IOException err) {
                Log.e(err);
            }
            songList.setImage(imageFile); 

And to retrieve it later on I tried:

if(songList.getImage() != null){
        try {
            Image img = Image.createImage(Storage.getInstance().createInputStream(songList.getImage()));
            Label test = new Label();
            test.setIcon(img);
            listCont.add(test);
        } catch(Exception ex){
            Dialog.show("Error", "Error during image loading: " + ex, "OK", null);
        }
    }

But I am getting an error that he can't find the file. (Device Simulator). Am I doing something wrong?

2

There are 2 answers

0
Withtaker On BEST ANSWER

Here is a complete working example of storing and loading an image for anyone who also had trouble:

Saving Image:

        String filePath = Capture.capturePhoto();
        if (filePath != null) {
            try {
                String pathToBeStored = FileSystemStorage.getInstance().getAppHomePath() + System.currentTimeMillis() +  ".jpg");
                Image img = Image.createImage(filePath);
                OutputStream os = FileSystemStorage.getInstance().openOutputStream(pathToBeStored );
                ImageIO.getImageIO().save(img, os, ImageIO.FORMAT_JPEG, 0.9f);
                os.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

Loading Image:

        if(pathToImage != null){
            try {
                Image img = Image.createImage(FileSystemStorage.getInstance().openInputStream(pathToImage));
            } catch(Exception ex){
                Dialog.show("Error", "Error during image loading: " + ex, "OK", null);
            }
        }
3
Shai Almog On

Use:

Image img = Image.createImage(Storgage.getInstance().createInputStream("ImageName"));

The image preview is an image not a component so you won't see anything unless you place an image within a component of some sort to show it.