aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/util/ImageUtils.java
blob: 43eefd7c58f8e5524bd1ce12d9084060f1ca1844 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.util;

import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;
import javax.swing.*;

/**
 * Utility methods for image manipulation.
 *
 * @author Sebastien Mazy
 * @author Yana Stamcheva
 */
public class ImageUtils
{
    private static final Logger logger = Logger.getLogger(ImageUtils.class);

    /**
     * Returns a scaled image fitting within the given bounds
     * while keeping the aspect ratio.
     *
     * @param image the image to scale
     * @param width maximum width of the scaled image
     * @param height maximum height of the scaled image
     * @return the scaled image
     */
    public static ImageIcon
        scaleIconWithinBounds(Image image, int width, int height)
    {
        ImageIcon scaledImage;
        int scaleHint = Image.SCALE_SMOOTH;
        double originalRatio =
            (double) image.getWidth(null) / image.getHeight(null);
        double areaRatio = (double) width / height;

        if(originalRatio > areaRatio)
        {
            scaledImage = new ImageIcon(image.
                    getScaledInstance(width, -1,scaleHint));
        }
        else
        {
            scaledImage = new ImageIcon(image.
                    getScaledInstance(-1, height, scaleHint));
        }
        return scaledImage;
    }

    /**
     * Creates a rounded avatar image.
     * 
     * @param avatarBytes The bytes of the initial avatar image.
     * 
     * @return The rounded corner image.
     */
    public static ImageIcon getScaledRoundedImage(  Image image,
                                                    int width,
                                                    int height)
    {
        BufferedImage destImage = null;

        ImageIcon scaledImage = ImageUtils.scaleIconWithinBounds(   image,
                                                                    width,
                                                                    height);

        destImage
            = new BufferedImage(scaledImage.getIconWidth(),
                                scaledImage.getIconHeight(),
                                BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = destImage.createGraphics();

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.WHITE);
        g.fillRoundRect(0, 0, 
                        scaledImage.getIconWidth(),
                        scaledImage.getIconHeight(),
                        10, 10);
        g.setComposite(AlphaComposite.SrcIn);

        g.drawImage(scaledImage.getImage(), 0, 0, null);

        return new ImageIcon(destImage);
    }

    /**
     * Creates a rounded corner scaled image.
     * 
     * @param imageBytes The bytes of the image to be scaled.
     * @param width The maximum width of the scaled image.
     * @param height The maximum height of the scaled image.
     * 
     * @return The rounded corner scaled image.
     */
    public static ImageIcon getScaledRoundedImage(  byte[] imageBytes,
                                                    int width,
                                                    int height)
    {
        if (imageBytes == null || imageBytes.length < 1)
            return null;

        ImageIcon imageIcon = null;

        try
        {
            InputStream in = new ByteArrayInputStream(imageBytes);
            BufferedImage image = ImageIO.read(in);

            imageIcon = getScaledRoundedImage(image, width, height);
        }
        catch (Exception e)
        {
            logger.error("Could not create image.", e);
        }

        return imageIcon;
    }
}