Java BufferedImage Operation weaveInto(BufferedImage bufferedImage, String message)

Here you can find the source of weaveInto(BufferedImage bufferedImage, String message)

Description

Weave the given secret message into the given BufferedImage .

License

Apache License

Parameter

Parameter Description
bufferedImage the buffered image
message the secret message

Return

the buffered image with the secret message weaved in.

Declaration

public static BufferedImage weaveInto(BufferedImage bufferedImage, String message) 

Method Source Code

//package com.java2s;
/**/*from   w ww .  java  2  s  .c o m*/
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Weave the given secret message into the given {@link BufferedImage}.
     *
     * @param bufferedImage
     *            the buffered image
     * @param message
     *            the secret message
     * @return the buffered image with the secret message weaved in.
     */
    public static BufferedImage weaveInto(BufferedImage bufferedImage, String message) {

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

Related

  1. unweaveFrom(BufferedImage bufferedImage)
  2. updateImageSpecifications( BufferedImage bufferedImage)
  3. validateSelectionRectangle(BufferedImage image, Rectangle selection)
  4. verticalGradient(BufferedImage image, Graphics2D g2d, Color... colors)
  5. waterMarkImage(final BufferedImage image, final String text)