import com.sun.media.jai.codec.FileSeekableStream; import javax.media.jai.widget.ScrollingImagePanel; /** * This program decodes an image file of any JAI supported * formats, such as GIF, JPEG, TIFF, BMP, PNM, PNG, into a * RenderedImage, scales the image by 2X with bilinear * interpolation, and then displays the result of the scale * operation. */ public class JAISampleProgramRotate{ /** The main method. */ public static void main(String[] args) { /* Validate input. */ if (args.length != 1) { System.out.println("Usage: java JAISampleProgram " + "input_image_filename"); System.exit(-1); } /* * Create an input stream from the specified file name * to be used with the file decoding operator. */ FileSeekableStream stream = null; try { stream = new FileSeekableStream(args[0]); } catch (IOException e) { e.printStackTrace(); System.exit(0); } /* Create an operator to decode the image file. */ RenderedOp image = JAI.create("stream", stream); /* * Create a standard bilinear interpolation object to be * used with the "scale" operator. */ Interpolation interp = Interpolation.getInstance( Interpolation.INTERP_BILINEAR); int value = 45; float angle = (float)(value * (Math.PI/180.0F)); // Create a ParameterBlock and specify the source and // parameters ParameterBlock pb = new ParameterBlock(); pb.addSource(image); // The source image//******************replace im with image**************** pb.add(0.0F); // The x origin pb.add(0.0F); // The y origin pb.add(angle); // The rotation angle pb.add(new InterpolationNearest()); // The interpolation // Create the scale operation RenderedOp im = JAI.create("Rotate", pb, null);//**************************************************create an instance im of type RenderedOp********//-----------------------------------add the other operator sample code here------------------------- /* Get the width and height of image2. */ int width = im.getWidth(); int height = im.getHeight(); /* Attach image2 to a scrolling panel to be displayed. */ ScrollingImagePanel panel = new ScrollingImagePanel(im, width, height); /* Create a frame to contain the panel. */ Frame window = new Frame("JAI Sample Program"); window.add(panel); window.pack(); window.show(); } }
用Graphic2D 旋转一下不就行了么import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException;import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel;public class T { public static void main(String[] args) throws IOException { final BufferedImage img = ImageIO.read(T.class.getResource("imgs/test.png")); JPanel p = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g);
楼主下载此jai之后,把jar放到lib下即可。以下是旋转一定角度的例程(转45度):
import java.awt.Frame;
import java.awt.image.renderable.ParameterBlock;
import java.io.IOException;
import javax.media.jai.*;
import com.sun.media.jai.codec.FileSeekableStream;
import javax.media.jai.widget.ScrollingImagePanel;
/**
* This program decodes an image file of any JAI supported
* formats, such as GIF, JPEG, TIFF, BMP, PNM, PNG, into a
* RenderedImage, scales the image by 2X with bilinear
* interpolation, and then displays the result of the scale
* operation.
*/
public class JAISampleProgramRotate{ /** The main method. */
public static void main(String[] args) {
/* Validate input. */
if (args.length != 1) {
System.out.println("Usage: java JAISampleProgram " +
"input_image_filename");
System.exit(-1);
} /*
* Create an input stream from the specified file name
* to be used with the file decoding operator.
*/
FileSeekableStream stream = null;
try {
stream = new FileSeekableStream(args[0]);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
} /* Create an operator to decode the image file. */
RenderedOp image = JAI.create("stream", stream);
/*
* Create a standard bilinear interpolation object to be
* used with the "scale" operator.
*/
Interpolation interp = Interpolation.getInstance(
Interpolation.INTERP_BILINEAR);
int value = 45;
float angle = (float)(value * (Math.PI/180.0F)); // Create a ParameterBlock and specify the source and
// parameters
ParameterBlock pb = new ParameterBlock();
pb.addSource(image); // The source image//******************replace im with image****************
pb.add(0.0F); // The x origin
pb.add(0.0F); // The y origin
pb.add(angle); // The rotation angle
pb.add(new InterpolationNearest()); // The interpolation
// Create the scale operation
RenderedOp im = JAI.create("Rotate", pb, null);//**************************************************create an instance im of type RenderedOp********//-----------------------------------add the other operator sample code here-------------------------
/* Get the width and height of image2. */
int width = im.getWidth();
int height = im.getHeight(); /* Attach image2 to a scrolling panel to be displayed. */
ScrollingImagePanel panel = new ScrollingImagePanel(im, width, height); /* Create a frame to contain the panel. */
Frame window = new Frame("JAI Sample Program");
window.add(panel);
window.pack();
window.show();
}
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;public class T
{
public static void main(String[] args) throws IOException
{
final BufferedImage img = ImageIO.read(T.class.getResource("imgs/test.png")); JPanel p = new JPanel() {
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.rotate(Math.toRadians(45), img.getWidth() / 2, img.getHeight() / 2);
g2d.drawImage(img, 0, 0, this);
}
};
JFrame f = new JFrame();
f.getContentPane().add(p, BorderLayout.CENTER);
f.setSize(800, 800);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}