-2

i am working on a cross-platform mobile app development. we are developing app for android, ios and windows phone. Now we have a requirement for showing a rounded image for user profile. we are thinking like from client side we will be passing the base64 string of the actual image and at server side they will processed as rounded image and sent it back to client as base64 image. So that we can avoid creating FFI for each platforms.

i googled to get some code snippet for doing this rounding work. but I didn't get any. Please advise or if you have any suggestion or code snippets available.

update

I have a code snippet where I am able to draw a circle over my image but how can i crop image in circle shape

public class OneRing {
OneRing(BufferedImage imageBG, BufferedImage imageFG) {
    // presumes the images are identical in size BNI
    int w = imageBG.getWidth();
    int h = imageBG.getHeight();
    Ellipse2D.Double ellipse1 = new Ellipse2D.Double(
            w/16,h/16,7*w/8,7*h/8); 
    Ellipse2D.Double ellipse2 = new Ellipse2D.Double(
            w/4,h/4,w/2,h/2);
    Area circle = new Area(ellipse1);
    circle.subtract(new Area(ellipse2));
    Graphics2D g = imageBG.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setClip(circle);
    //g.drawImage(imageFG, 0, 0, null);
    g.setClip(null);
    Stroke s = new BasicStroke(2);
    g.setStroke(s);
    g.setColor(Color.WHITE);
    g.draw(circle);
    g.dispose();
    JLabel l = new JLabel(new ImageIcon(imageBG));
    JOptionPane.showMessageDialog(null, l);
}
Kris
  • 891
  • 2
  • 18
  • 41
  • Doesn't [this](http://stackoverflow.com/questions/17506428/convert-base64-string-to-image-in-java) work? – StackUseR Jan 03 '17 at 06:42

1 Answers1

0

i have done this with below code.

    public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
    System.out.print("inside makeRoundCorner");
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = output.createGraphics();
    g2.setComposite(AlphaComposite.Src);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.WHITE);
    g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));
    g2.setComposite(AlphaComposite.SrcIn);
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
    return output;
}
public static void main(String[] args) throws IOException {
    BufferedImage imageOnDisk2 = ImageIO.read(new File("D:\\billpayment.png"));
    BufferedImage biBG = makeRoundedCorner(imageOnDisk2, 100);
    ImageIO.write(biBG, "png", new File("D:\\icon.rounded.png"));    
}
Kris
  • 891
  • 2
  • 18
  • 41