是否有任何java代码可用于将base64(squre image)字符串转换为服务器端的圆形配置文件图像

时间:2017-01-03 06:15:25

标签: java image mobile base64 cross-platform

我正致力于跨平台移动应用开发。我们正在为Android,ios和Windows手机开发应用程序。现在我们需要为用户个人资料显示圆形图像。我们想从客户端我们将传递实际图像的base64字符串,在服务器端,它们将作为圆形图像处理并作为base64图像发送回客户端。这样我们就可以避免为每个平台创建FFI。

我用Google搜索获取一些代码片段来完成此舍入工作。但我没有得到任何。如果您有任何建议或代码片段,请提供建议。

更新

我有一个代码片段,我可以在我的图像上绘制一个圆圈但是如何裁剪圆圈形状的图像

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

1 个答案:

答案 0 :(得分:0)

我用以下代码完成了这项工作。

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