Rotate image

I have a problem when rotate image. After i rotate, i save the image and the background of image change to black. I occor with some picture as:

http://i209.photobucket.com/albums/bb132/bocanguoi/ti01.gif

http://i209.photobucket.com/albums/bb132/bocanguoi/db01.gif

import java.awt.*;

import java.awt.geom.*;

import java.awt.image.*;

import java.io.*;

import javax.imageio.*;

import javax.swing.*;

public class RotateImage {

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

String filePath = "c:/ti01.gif";

String fileout = "c:/ti02.gif";

writeImage(rotateImage(readImage(filePath), 45), fileout);

}

private static void writeImage (BufferedImage toWrite, String fileout) throws IOException {

File f = new File(fileout);

String ext = fileout.substring(fileout.length() - 3, fileout.length());

String filetype = "jpeg";

ImageIO.write((RenderedImage)toWrite, filetype, f);

}

private static BufferedImage readImage (String filename) throws IOException {

File imagein = new File(filename);

BufferedImage bi = ImageIO.read(imagein);

return bi;

}

private static BufferedImage rotateImage (BufferedImage bi, int rotations) {

rotations = rotations % 4;

int newWidth = bi.getWidth();

int newHeight = bi.getHeight();

int moveX = 0;

int moveY = 0;

if (rotations % 2 != 0) {

newHeight = bi.getWidth();

newWidth = bi.getHeight();

}

if (rotations > 1)

moveY = newHeight;

if (rotations > 0 && rotations < 3)

moveX = newWidth;

BufferedImage rbi = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_4BYTE_ABGR);

Graphics2D g2d = rbi.createGraphics();

AffineTransform af = new AffineTransform();

af.concatenate(AffineTransform.getTranslateInstance(moveX, moveY));

af.concatenate(AffineTransform.getRotateInstance((rotations * 0.5) * Math.PI));

g2d.drawImage((Image) bi, af, null);

return rbi;

}

}

[2100 byte] By [kietfriendsa] at [2007-11-15]
# 1

Instead of using AffineTransform to simply draw the rotated image, you need to use AffineTransform to rotate the image.

1cMas5_cowa at 2007-7-29 > top of java,Security,Cryptography...
# 2

Note: The angle is in radians, to convert a degree (deg) to radians, use the Math.toRadians(deg) method :).

public static BufferedImage rotate(BufferedImage in,double angle,

RenderingHints rh)

{

//

AffineTransform aff = AffineTransform.getRotateInstance(angle);

// The transform op

AffineTransformOp op = new AffineTransformOp(aff,rh);

// Return the rotated bufferedImage

return op.filter(in,null);

}

And with an anchor point:

public static BufferedImage rotate(BufferedImage in,double angle,

double x,double y,RenderingHints rh)

{

//

AffineTransform aff = AffineTransform.getRotateInstance(angle,x,y);

// The transform op

AffineTransformOp op = new AffineTransformOp(aff,rh);

// Return the rotated bufferedImage

return op.filter(in,null);

}

:D

dfgstga at 2007-7-29 > top of java,Security,Cryptography...
# 3

A newly–created BufferedImage comes with no color in it so its background is black.

So you have to set a background color and fill the background with it. Here you have a

transparent background so you create a color with zero alpha for the background fill.

Or you can skip this step and use BufferedImage.TYPE_4BYTE_ABGR_PRE.

crwooda at 2007-7-29 > top of java,Security,Cryptography...
# 4

1cMas5_cow , dfgstg : It not my problem. The problem is the backgroud picture is change after rotate and save

crwood : the picture i use it background is transparent, so that i do't know the color to set.

kietfriendsa at 2007-7-29 > top of java,Security,Cryptography...
# 5

create a color with zero alpha

Try new Color(0,0,0,0)

the last (7th) constructor in the Color api.

Or you can...use BufferedImage.TYPE_4BYTE_ABGR_PRE

This is easier.

crwooda at 2007-7-29 > top of java,Security,Cryptography...