代号一:将图像保存到存储并创建小的四舍五入预览 [英] Codename One: Save Image to Storage and create small rounded preview
问题描述
我现在必须对图像有疑问:
I have to problem with Images currently:
1)我无法将图像保存到存储中,因为不支持将图像直接存储到存储中.我希望用户能够使用相机拍摄照片,然后将创建的照片保存在某个地方,以便稍后再次检索.
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.
你能告诉我如何a)保存图像b)如何找回
Could you tell me how a) To save an Image b) How to retrieve it
我在Stackoverflow中找到了Shai制作的这段代码,另一个用户询问如何将图像保存到存储中.
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);
但是我不明白.如何从中获得图像"对象?以及"Image"对象到底存储在哪里呢?你能在这里给我一个完整的例子吗?这对于我的应用程序确实至关重要.
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);
但是它没有成功,ImagePreview甚至都不会显示.无论如何,此预览都是原始大小的,之后我如何将其调整为较小的预览大小?
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?
谢谢.
我尝试了以下方法来保存图像:
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);
并在以后尝试检索它:
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?
推荐答案
这是为遇到麻烦的任何人存储和加载图像的完整工作示例:
Here is a complete working example of storing and loading an image for anyone who also had trouble:
保存图像:
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();
}
}
加载图像:
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);
}
}
这篇关于代号一:将图像保存到存储并创建小的四舍五入预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!