【问题标题】:Codename One rounded image from an internet source代号 来自互联网来源的一张圆形图像
【发布时间】:2016-03-12 01:14:10
【问题描述】:

我正在尝试显示我直接从 Internet 获得的圆形图像。 我使用下面的代码创建了一个圆形蒙版,从 Internet 获取图像,然后尝试在图像上设置蒙版或标签本身。这些方法都不起作用。如果我取下遮罩,图像会显示得很好。如果我保留设置掩码的代码,那么我看到的只是一个空的白色圆圈。

我的想法是,如果我在图像本身上应用蒙版,则它可能不会生效,因为在应用蒙版时未下载图像。

但我似乎不明白为什么在标签上调用setMask 也不起作用。

   // Create MASK

    Image maskImage = Image.createImage(w, l);
    Graphics g = maskImage.getGraphics();
    g.setAntiAliased(true);
    g.setColor(0x000000);
    g.fillRect(0, 0, w, l);
    g.setColor(0xffffff);
    g.fillArc(0, 0, w, l, 0, 360);
    Object mask = maskImage.createMask();



    // GET IMAGE
    com.cloudinary.Cloudinary cloudinary = new com.cloudinary.Cloudinary(ObjectUtils.asMap(
            "cloud_name", "REMOVED",
            "api_key", "REMOVED",
            "api_secret", "REMOVED"));
    // Disable private CDN URLs as this doesn't seem to work with free accounts
    cloudinary.config.privateCdn = false;
    Image placeholder = Image.createImage(150, 150);
    EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
    Image img2 = cloudinary.url()
            .type("fetch") // Says we are fetching an image
            .format("jpg") // We want it to be a jpg
            .transformation(
                    new Transformation()
                    .radius("max").width(150).height(150).crop("thumb").gravity("faces").image(encImage, "http://upload.wikimedia.org/wikipedia/commons/4/46/Jennifer_Lawrence_at_the_83rd_Academy_Awards.jpg");

    Label label = new Label(img2);
    label.setMask(mask);  // also tried to do img2.applyMask(mask); before passing img2 

【问题讨论】:

    标签: codenameone parparvm


    【解决方案1】:

    所以我尝试了各种方法:

    1) 移除通过 cloudinary 设置的掩码 - 这不起作用

    2) 将掩码应用于占位符和编码图像(正如预期的那样,这些不应影响即将发布的最终版本)

    3) 这是有效的!我不确定问题是否真的是在应用面具之前或之后下载图片..时间可以告诉我们

        Label label = new Label();
        img2.applyMask(mask);  // If you remove this line , the image will no longer be displayed, I will only see a rounded white circle ! I am not sure what this is doing, it might be simply stalling the process until the image is downloaded? or maybe somehow calling repaint or revalidate
        label.setIcon( img2.applyMask(mask));
    

    如果其他人遇到类似问题,这对我有用:

            //CREATE MASK
        Image maskImage = Image.createImage(w, l);
        Graphics g = maskImage.getGraphics();
        g.setAntiAliased(true);
        g.setColor(0x000000);
        g.fillRect(0, 0, w, l);
        g.setColor(0xffffff);
        g.fillArc(0, 0, w, l, 0, 360);
        Object mask = maskImage.createMask();
    
        //CONNECT TO CLOUDINARY 
        com.cloudinary.Cloudinary cloudinary = new com.cloudinary.Cloudinary(ObjectUtils.asMap(
                "cloud_name", "REMOVED",
                "api_key", "REMOVED",
                "api_secret", "REMOVED"));
        // Disable private CDN URLs as this doesn't seem to work with free accounts
        cloudinary.config.privateCdn = false;
    
        //CREATE IMAGE PLACEHOLDERS 
        Image placeholder = Image.createImage(w, l);
        EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
    
        //DOWNLOAD IMAGE
        Image img2 = cloudinary.url()
                .type("fetch") // Says we are fetching an image
                .format("jpg") // We want it to be a jpg
                .transformation(
                        new Transformation()
                        .crop("thumb").gravity("faces")
                        .image(encImage, url);
    
    
        // Add the image to a label and place it on the form.
        //GetCircleImage(img2);
        Label label = new Label();
        img2.applyMask(mask);   // If you remove this line , the image will no longer be displayed, I will only see a rounded white circle ! I am not sure what this is doing, it might be simply stalling the process until the image is downloaded? or maybe somehow calling repaint or revalidate
        label.setIcon( img2.applyMask(mask));
    

    Shai,非常感谢您的宝贵时间!!非常感谢。如果以后给我带来任何其他问题,将不得不深入研究它,但它现在似乎一直有效。

    【讨论】:

      【解决方案2】:

      Cloudinary API 返回的 URLImage 不适用于 Label.setMask() 方法,因为从技术上讲,URLImage 是一个动画图像(在完成加载之前它是一个占位符图像,然后“动画”到成为目标图像)。

      我刚刚发布了 cloudinary cn1lib 的 new version,它为您提供了解决此问题的几个选项。

      我添加了两个新的image() 方法。一个采用ImageAdapter 参数的方法,您可以使用该参数将蒙版应用于图像本身,然后再将其设置为标签的图标。那么你根本不会使用 Label.setMask()。

      javadocs for this method here

      另一种方法使用下面新的异步图像加载 API 来异步加载图像。您在回调中收到的图像是“真实”图像,因此您可以正常使用它与蒙版。

      javadocs for this method here

      如果您尝试添加“动画”图像并将其遮盖以使其更清晰,我们正在考虑向 Label.setMask() 和 setIcon() 方法添加软警告。

      【讨论】:

      • 这是有道理的。我的印象是这可能是图像下载问题。我会执行你的建议。我认为这是前进的正确方式。谢谢!!
      【解决方案3】:

      我认为您为标签设置的制作代码可能与您从 Cloudinary 获得的屏蔽代码冲突。

      【讨论】:

        猜你喜欢
        • 2016-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-08
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多