19998

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

Question:

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.

<strong>Edit:</strong>

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?

Answer1:

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

Answer2:

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.

Recommend

  • Colorize a grayscale image
  • Obtaining unstandardized factor scores from factor analysis
  • convert 16 bit grayscale DICOM image to 8 bit: the correct procedure
  • Why does x86 program segfault without .data section? [duplicate]
  • Detecting single one-bit streams within an integer
  • How to define matching axis notches from existing “step list”
  • Looking for datastructure that maintains a size & purges older elements in the process
  • different results with and without SSE ( float arrays multiplication)
  • Replace any string in columns with 1
  • Difficulties implementing the Hysteresis step of Canny Algorithm in Halide without define_extern fun
  • Why do GeoJSON features appear like a negative photo of the features themselves?
  • write text on image and show it to a imageview
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • How to change placeholder text in an autocomplete activity of android google place?
  • Thread synchronization with syncwarp
  • Using same constraints in multiple classes
  • Z3: Convert between FP and BitVector?
  • Keep this build forever option - Jenkins
  • Converting a WriteableBitmap image ToArray in UWP
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Content-Length header not returned from Pylons response
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Is there any way to access browser form field suggestions from JavaScript?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Hazelcast - OperationTimeoutException
  • Jquery - Jquery Wysiwyg return html as a string
  • AT Commands to Send SMS not working in Windows 8.1
  • Arrays break string types in Julia
  • How to format a variable of double type
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • WPF Applying a trigger on binding failure
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is it possible to post an object from jquery to bottle.py?
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?