Java Utililty Methods BufferedImage Operation

List of utility methods to do BufferedImage Operation

Description

The list of methods to do BufferedImage Operation are organized into topic(s).

Method

voidtileStretchPaint(Graphics g, Component component, BufferedImage image, Insets insets)
Draws an image on top of a component by doing a 3x3 grid stretch of the image using the specified insets.
int left = insets.left;
int right = insets.right;
int top = insets.top;
int bottom = insets.bottom;
g.drawImage(image, 0, 0, left, top, 0, 0, left, top, null);
g.drawImage(image, left, 0, component.getWidth() - right, top, left, 0, image.getWidth() - right, top,
        null);
g.drawImage(image, component.getWidth() - right, 0, component.getWidth(), top, image.getWidth() - right, 0,
...
BufferedImagetilt(BufferedImage image, double angle)
tilt
double sin = Math.abs(Math.sin(angle));
double cos = Math.abs(Math.cos(angle));
int w = image.getWidth();
int h = image.getHeight();
int neww = (int) Math.floor(w * cos + h * sin);
int newh = (int) Math.floor(h * cos + w * sin);
GraphicsConfiguration gc = getDefaultConfiguration();
BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
...
BufferedImagetintImage(BufferedImage src, Color color, float tintOpacity)
tint Image
BufferedImage img = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TRANSLUCENT);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(src, null, 0, 0);
g2d.setColor(
        new Color(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, tintOpacity));
Raster data = src.getData();
for (int x = data.getMinX(); x < data.getWidth(); x++) {
    for (int y = data.getMinY(); y < data.getHeight(); y++) {
...
StringunweaveFrom(BufferedImage bufferedImage)
Unweave a secret message from the given BufferedImage .
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int messageLength = bufferedImage.getRGB(0, 0) & 0xff;
StringBuilder sb = new StringBuilder();
for (int row = 0, j = 0, i = 1; row < height; row++) {
    for (int column = 0; column < width && j < messageLength; column++, i++) {
        if (i % 11 == 0) {
            int result = bufferedImage.getRGB(column, row);
...
voidupdateImageSpecifications(BufferedImage bufferedImage)
Updated the local (static) variables holding the image, the color-model of the image, the raster of the image, and the height and width of the image-raster.
image = bufferedImage;
imageColorModel = image.getColorModel();
imageRaster = image.getRaster();
imageHeight = imageRaster.getHeight();
imageWidth = imageRaster.getWidth();
booleanvalidateSelectionRectangle(BufferedImage image, Rectangle selection)
validate the selection rectangle against the current image position and size
return selection != null && selection.x >= 0 && selection.y >= 0 && selection.width > 0
        && selection.height > 0 && selection.x + selection.width <= image.getWidth()
        && selection.y + selection.height <= image.getHeight();
voidverticalGradient(BufferedImage image, Graphics2D g2d, Color... colors)
vertical Gradient
int miniHeight = image.getHeight() / (colors.length - 1);
for (int i = 1; i < colors.length; i++) {
    Point start = new Point(0, (i - 1) * miniHeight);
    Point end = new Point(0, i * miniHeight);
    gradientPaint(g2d, start, colors[i - 1], end, colors[i], start.x, start.y, image.getWidth(), end.y);
BufferedImagewaterMarkImage(final BufferedImage image, final String text)
Watermarks the given image with given text.
if (image == null) {
    throw new IllegalArgumentException("\"image\" param cannot be null.");
final BufferedImage waterMarked = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
final Graphics2D imageG = waterMarked.createGraphics();
imageG.drawImage(image, null, 0, 0);
imageG.dispose();
final Graphics2D g = waterMarked.createGraphics();
...
BufferedImageweaveInto(BufferedImage bufferedImage, String message)
Weave the given secret message into the given BufferedImage .
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if (message.length() > 255) {
    throw new IllegalArgumentException("Given message is to large(max 255 characters)");
if (message.length() * 11 > width * height) {
    throw new IllegalArgumentException("Given image is to small");
byte[] messageBytes = message.getBytes();
int messageLengthDecode = bufferedImage.getRGB(0, 0) >> 8 << 8;
messageLengthDecode |= message.length();
bufferedImage.setRGB(0, 0, messageLengthDecode);
for (int i = 1, messagePosition = 0, row = 0, j = 0; row < height; row++) {
    for (int column = 0; column < width && j < messageBytes.length; column++, i++) {
        if (i % 11 == 0) {
            int rgb = bufferedImage.getRGB(column, row);
            int a = rgb >> 24 & 0xff;
            int r = (rgb >> 16 & 0xff) >> 3 << 3;
            r = r | messageBytes[messagePosition] >> 5;
            int g = (rgb >> 8 & 0xff) >> 3 << 3;
            g = g | messageBytes[messagePosition] >> 2 & 7;
            int b = (rgb & 0xff) >> 2 << 2;
            b = b | messageBytes[messagePosition] & 0x3;
            rgb = 0;
            rgb = rgb | a << 24;
            rgb = rgb | r << 16;
            rgb = rgb | g << 8;
            rgb = rgb | b;
            bufferedImage.setRGB(column, row, rgb);
            messagePosition++;
            j++;
return bufferedImage;