我目前的图像有问题:
1)我无法将图像保存到存储,因为不支持将其直接存储到存储。我希望用户能够用相机拍照,然后创建的照片必须保存在某个地方,以便我稍后可以再次检索它。
你能告诉我怎么做吗
a) 保存图像
b) 如何检索它
我在 Stackoverflow 中找到了 Shai 编写的这段代码,其中另一个用户询问如何将图像保存到存储中。
InputStream stream = FileSystemStorage.getInstance().openInputStream(i);
OutputStream out = Storage.getInstance().createOutputStream("MyImage");
Util.copy(stream, out);
Util.cleanup(stream);
Util.cleanup(out);
但我不明白。我怎样才能从中得到一个“图像”对象? “图像”对象到底是如何存储在那里的?你能给我一个完整的例子吗?这对于我的应用程序来说非常重要。
2)
这个问题可能很微不足道,但我找不到答案。我想重新缩放拍摄的相机图片并在其周围制作圆形蒙版。我试过这个:
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);
但它没有成功,图像预览甚至不会显示。无论如何,该预览将是原始大小,之后我如何将其调整为用户的小预览?
先感谢您。
Edit:
我已尝试以下方法来保存图像:
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);
}
}
但我收到一个错误,他找不到该文件。 (设备模拟器)。难道我做错了什么?