代号一个圆形的URLImage不显示

时间:2016-08-24 15:22:00

标签: java image codenameone mask

我正在尝试使用URLImage.createToStorage从网址中提取图片。但是,我希望该图片显示为圆形,因此我在图像中添加了一个蒙版。但是,当我运行标签时,只显示占位符图像,而不是网址图像。当我注释掉将圆形蒙版添加到图像时显示图像的代码。我的圆形图像代码有问题吗。我使用了Display.getInstance()。callSerially()。

//Where I display the image. 
public void setUpProfile(Form f) {
    Label imageLabel = findMyImage(f);
    Image img = getImageFromRes("myprofile.png");
    Image scaled = img.scaledWidth(f.getWidth() / 2);
    EncodedImage enc = EncodedImage.createFromImage(scaled, false);

    Display.getInstance().callSerially(new Runnable() {
        @Override
        public void run() {
            imageLabel.setIcon(getRoundedImage(URLImage.createToStorage(enc, 
            "profileImage8", me.getPicture(), URLImage.RESIZE_SCALE_TO_FILL)));
            f.revalidate();
        }
    });

    findProfNameLabel(f).setText(me.getName());
    findProfAgeLabel(f).setText(me.getAge() + " Years old");
    findProfPrefLabel(f).setText("Interested in " + me.getPref());
}

public Image getRoundedImage(Image img) {

    int w = img.getWidth();
    int h = img.getHeight();

    Image maskImage = Image.createImage(w, h);
    Graphics g = maskImage.getGraphics();
    g.setColor(0xffffff);
    g.fillArc(0, 0, w, h, 0, 360);

    Object mask = maskImage.createMask();
    Image ret = img.applyMask(mask);
    return ret;
}

在Form的beforeShow中调用setUpProfile()方法。

编辑:我在使用URLImage.createMaskAdapter的工作setUpProfile()方法中编辑。并实现圆润的图像。

public void setUpProfile(Form f) {
    Label imageLabel = findMyImage(f);
    Image mask = getImageFromRes("rounded-mask.png");
    Image placeholder = getImageFromRes("myprofile.png").scaled(mask.getWidth(), mask.getHeight());
    EncodedImage enc = EncodedImage.createFromImage(placeholder.applyMask(mask.createMask()),   
false);
    System.out.println("SetUpProfile picture " + me.getPicture());

    imageLabel.setIcon(URLImage.createToStorage(enc, "profileImage8",
    me.getPicture(), URLImage.createMaskAdapter(mask)));

    findProfNameLabel(f).setText(me.getName());
    findProfAgeLabel(f).setText(me.getAge() + " Years old");
    findProfPrefLabel(f).setText("Interested in " + me.getPref());
}

1 个答案:

答案 0 :(得分:1)

您可以通过创建自定义#include <gtk/gtk.h> /* There are two reasonable sizes for a rotated image-- Either the minimum */ /* bounding box which contains all rotated pixels (and a bunch of white space)*/ /* or the maximum rectangle where all pixels come from the source image (but */ /* where we lose some of the corners) */ /* The first is easy to calculate: The minimum bounding box will have the corners */ /* of the rotated image on its edges, this leaves us with four triangles in */ /* the corners of the bb. Two triangles have edges width*sin(theta), width*cos(theta) */ /* and two have edges height*sin(theta), height*cos(theta) */ /* so the new width height will be the sum of two adjacent triangle edges: */ /* width" = width*cos + height*sin */ /* height"= width*sin + height*cos */ /* Now for the maximum inscribed rectangle we draw a similar picture (except */ /* the unknown rectangle is internal now) and get similar triangles. Here the*/ /* equations are: */ /* width = width'*cos + height'*sin */ /* height= width'*sin + height'*cos */ /* solving for height'... */ /* height' = (width-width'*cos)/sin */ /* height' = (height-width'*sin)/cos */ /* (width-width'*cos)/sin = (height-width'*sin)/cos */ /* width*cos - width'*cos^2 = height*sin - width'*sin^2 */ /* width' * (sin^2-cos^2) = height*sin-width*cos */ /* width' = (height*sin - width*cos)/(sin^2-cos^2) */ /* height'= (width*sin - height*cos)/(sin^2-cos^2) */ /* Note this produces garbage (0/0) when rotated by 45 degrees (135,225,...) */ /* A little experimentation shows that at 45 degrees the only thing with */ /* an internal rectangle is a square, all other aspect ratios have a height */ /* of 0. A square, however, has an internal square with sides 1/sqrt(2) of the original */ /* When creating a full_size image (minimum bounding box) we should return */ /* an image with an alpha channel (whether the original had one or no). */ /* otherwise we should create an alpha channel only if the original had one */ /* A pixel at (x,y) will be rotated to: */ /* ((x-width/2)*cos + (y-height/2)*sin + width'/2 , */ /* =(x-width/2)*sin + (y-height/2)*cos + height'/2 ) */ /* A pixel at (x',y') will have come from: */ /* ((x'-width'/2)*cos - (y'-height'/2)*sin + width/2 , */ /* (x'-width'/2)*sin + (y'-height'/2)*cos + height/2 ) */ static GdkPixbuf *gdk_pixbuf_rotate(GdkPixbuf *src,double radian,gboolean full_size) { double s = sin(radian), c = cos(radian); double as= s<0 ? -s : s, ac= c<0 ? -c : c; int width, height, nwidth, nheight; int hasalpha, nhasalpha; GdkPixbuf *ret; int nr,nc,r,col; double nmodr, nmodc; int alpha=0; guchar *pixels, *npixels, *pt, *npt; int rowstride, nrowstride, pixellen; if ( src==NULL ) return( NULL ); width = gdk_pixbuf_get_width(src); height = gdk_pixbuf_get_height(src); hasalpha = gdk_pixbuf_get_has_alpha(src); rowstride = gdk_pixbuf_get_rowstride(src); pixels = gdk_pixbuf_get_pixels(src); pixellen = hasalpha ? 4 : 3; if ( full_size ) { nwidth = round( ac*width + as*height ); nheight= round( as*width + ac*height ); nhasalpha = TRUE; } else { double denom = as*as - ac*ac; if ( denom<.1e-7 && denom>-1.e-7 ) { if ( width!=height ) return( NULL ); nwidth = nheight = round( width/sqrt(2.0) ); } else { nwidth = round( (height*as - width*ac)/denom ); nheight = round( (width*as - height*ac)/denom ); } if ( nwidth<=0 || nheight<=0 ) return( NULL ); nhasalpha = hasalpha; } ret = gdk_pixbuf_new(GDK_COLORSPACE_RGB,nhasalpha,8,nwidth,nheight); if ( ret==NULL ) return( NULL ); nrowstride = gdk_pixbuf_get_rowstride(ret); npixels = gdk_pixbuf_get_pixels(ret); for ( nr=0; nr<nheight; ++nr ) { nmodr = nr-nheight/2.0; npt = npixels + nr*nrowstride; for ( nc=0; nc<nwidth; ++nc ) { nmodc = nc-nwidth/2.0; /* Where did this pixel come from? */ r = round( height/2 - nmodc*s + nmodr*c ); col = round( width/2 + nmodc*c + nmodr*s ); if ( r<0 || col<0 || r>=height || col>=width ) { alpha = 0; if ( r<0 ) r=0; else if ( r>=height ) r = height-1; if ( col<0 ) col = 0; else if ( col>=width ) col = width-1; } else alpha = 0xff; pt = pixels + r*rowstride + col*pixellen; *npt++ = *pt++; *npt++ = *pt++; *npt++ = *pt++; if ( hasalpha && alpha!=0 ) alpha = *pt; if ( nhasalpha ) *npt++ = alpha; } } return( ret ); }来实现此目的,该自定义ImageAdapter会在下载图片时自动为您生成round-mask

public static final URLImage.ImageAdapter RESIZE_SCALE_WITH_ROUND_MASK = new URLImage.ImageAdapter() {
    @Override
    public EncodedImage adaptImage(EncodedImage downloadedImage, EncodedImage placeholderImage) {
        Image tmp = downloadedImage.scaledLargerRatio(placeholderImage.getWidth(), placeholderImage.getHeight());
        if (tmp.getWidth() > placeholderImage.getWidth()) {
            int diff = tmp.getWidth() - placeholderImage.getWidth();
            int x = diff / 2;
            tmp = tmp.subImage(x, 0, placeholderImage.getWidth(), placeholderImage.getHeight(), true);
        } else if (tmp.getHeight() > placeholderImage.getHeight()) {
            int diff = tmp.getHeight() - placeholderImage.getHeight();
            int y = diff / 2;
            tmp = tmp.subImage(0, y, Math.min(placeholderImage.getWidth(), tmp.getWidth()),
                    Math.min(placeholderImage.getHeight(), tmp.getHeight()), true);
        }
        Image roundMask = Image.createImage(tmp.getWidth(), tmp.getHeight(), 0xff000000);
        Graphics gr = roundMask.getGraphics();
        gr.setColor(0xffffff);
        gr.fillArc(0, 0, tmp.getWidth(), tmp.getHeight(), 0, 360);
        Object mask = roundMask.createMask();
        tmp = tmp.applyMask(mask);
        return EncodedImage.createFromImage(tmp, false);
    }

    @Override
    public boolean isAsyncAdapter() {
        return true;
    }
};

然后以这种方式应用:

public void setUpProfile(Form f) {
    Label imageLabel = findMyImage(f);
    Image img = getImageFromRes("myprofile.png");
    Image scaled = img.scaledWidth(f.getWidth() / 2);
    EncodedImage enc = EncodedImage.createFromImage(scaled, false);

    Display.getInstance().callSerially(new Runnable() {
        @Override
        public void run() {
            imageLabel.setIcon(URLImage.createToStorage(enc, 
            "profileImage8", me.getPicture(), RESIZE_SCALE_WITH_ROUND_MASK));
            f.revalidate();
        }
    });

    findProfNameLabel(f).setText(me.getName());
    findProfAgeLabel(f).setText(me.getAge() + " Years old");
    findProfPrefLabel(f).setText("Interested in " + me.getPref());
}