0

I have array of byte for image , and I want to rotate image by this array , this is my code :

BufferedImage img = ImageUtil.load(inputImagePath);
WritableRaster raster = img .getRaster();
DataBufferByte data   = (DataBufferByte) raster.getDataBuffer();

byte [] pixel = data.getData();

how i can do this ? , thanks

5
  • 1
    Why do you need to rotate the byte array? Could you use This? Commented May 6, 2015 at 20:54
  • @JAtkin , my goal to create a new algorithm for rotating image by using just array , How to get pixels locations at least ? Commented May 7, 2015 at 11:27
  • 1
    If you want to create a new algorithm, then what exactly are asking? Us to create it for you? If you want to implement some algorithm, then try to do it and if you encounter a problems then ask more specific question. Commented May 7, 2015 at 15:53
  • Also working with pixels via byte array is ineffective, see this answer for more information. Commented May 7, 2015 at 16:04
  • @NikolayKondratyev my problems was I haven't any reference to pixels , but now I have by using this : int w = img.getWidth(null); int h = img.getHeight(null); int[] rgbs = new int[w*h]; img.getRGB(0, 0, w, h, rgbs, 0, w); _ and I create my own algorithm for rotation and flipping image - thanks for your comment Commented May 7, 2015 at 21:01

1 Answer 1

2

After some work, I came up with this:

public class ImageRotation {
    public static void main(String[] args) throws IOException {

        BufferedImage img = ImageIO.read(
                ImageRotation.class
                        .getResourceAsStream("Capture.PNG"));

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(
                new JLabel("Original", new ImageIcon(img), JLabel.CENTER),
                BorderLayout.WEST);

        pane.add(
                new JLabel("Rotated", new ImageIcon(rotateClockwise(img)), JLabel.CENTER),
                BorderLayout.EAST);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(pane);
        frame.setVisible(true);
        frame.pack();
    }

    static BufferedImage rotateClockwise(BufferedImage img) {
        int[] origPix = getIntBuff(img);

        int newWidth = img.getHeight();
        int newHeight = img.getWidth();
        int[] buff = new int[newWidth * newHeight];

        // formula for determining pixel mapping
        // (sizeOf(old y) - 1) - old y -> new x
        // old x -> new y

        for (int x = 0; x < img.getWidth(); x++)
            for (int y = 0; y < img.getHeight(); y++) {

                int pix = origPix[x + (y * img.getWidth())];
                int newX = img.getHeight() - 1 - y, newY = x;

                buff[newX + (newWidth * newY)] = pix;
            }
        // we have now rotated the array clockwise, time to place the buffer in an image


        int type = BufferedImage.TYPE_INT_ARGB;
        BufferedImage ret = new BufferedImage(newWidth, newHeight, type);
        WritableRaster wr = ret.getRaster();
        wr.setDataElements(0, 0, newWidth, newHeight, buff);
        return ret;
    }

    // variation of convertTo2DWithoutUsingGetRGB http://stackoverflow.com/a/9470843/4683264
    private static int[] getIntBuff(BufferedImage image) {

        final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        final int width = image.getWidth();
        final int height = image.getHeight();
        final boolean hasAlphaChannel = image.getAlphaRaster() != null;

        int[] result = new int[height * width];

        final int pixelLength = hasAlphaChannel ? 4 : 3;
        for (int pixel = 0, resInd = 0; pixel < pixels.length; pixel += pixelLength) {
            int argb = 0;
            if (hasAlphaChannel)
                argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
            else
                argb += -16777216; // 255 alpha

            argb += ((int) pixels[pixel + 1] & 0xff); // blue
            argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
            argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
            result[resInd++] = argb;
        }

        return result;
    }

}

Result:

enter image description here

Right now it only rotates the image clockwise, but once you find the pixel mappings from the old to new image for, say, counterclockwise, all you need to change is in the nested for loop in the rotateClockwise method to:

int newX = y, newY = img.getWidth() - 1 - x;
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for you , I didn't use your code , but I use this array for rotate image and flipping it and it worked good : int w = img.getWidth(null); int h = img.getHeight(null); int[] rgbs = new int[w*h]; img.getRGB(0, 0, w, h, rgbs, 0, w);
@safana If this answer helped you, please select as "best answer"
The little check mark under the vote count on this post. stackoverflow.com/help/accepted-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.