Codenameone 代号一:将图像保存到存储器并创建小的圆形预览

Codenameone 代号一:将图像保存到存储器并创建小的圆形预览,codenameone,Codenameone,我目前对图像有问题: 1) 我无法将图像保存到存储器,因为不支持将其直接存储到存储器。我希望用户能够用相机拍摄照片,然后创建的照片必须保存在某个地方,这样我以后可以再次检索它 你能告诉我怎么做吗 a) 保存图像的步骤 b) 如何检索它 我在Stackoverflow中发现了Shai编写的这段代码,另一个用户询问如何将图像保存到存储器中 InputStream stream = FileSystemStorage.getInstance().openInputStream(i); OutputSt

我目前对图像有问题:

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);
但我不明白。如何从中获取“图像”对象?“图像”对象究竟是如何存储在那里的?你能给我举个完整的例子吗?这对我的应用程序来说非常重要

(二)

这个问题可能很琐碎,但我找不到答案。我想重新缩放相机拍摄的照片,并在其周围制作一个圆形遮罩。我试过这个:

        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);
但是没有成功,图像预览甚至不会出现。不管怎样,这个预览都是原始大小的,我以后如何将它调整为一个小的预览

先谢谢你

编辑:

我已尝试以下方法来保存图像:

            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);
        }
    }
但我得到一个错误,他找不到文件。(设备模拟器)。我做错什么了吗?

使用:

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

图像预览是图像而不是组件,因此除非将图像放置在某种类型的组件中以显示它,否则您将看不到任何内容。

以下是一个完整的存储和加载图像的工作示例,适用于有问题的人:

保存图像:

        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);
            }
        }
正在加载图像:

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

嗨,谢谢你的帮助。我已经编辑了我的帖子,请查看编辑。您已保存到
文件系统存储
应用程序主页,然后尝试从
存储
加载。它们是不同的东西。用一个或另一个。请注意,您将捕获的JPEG保存为PNG,这在设备上可能非常大,您可以复制文件输入流,而不是加载图像…工作正常!非常感谢。