本文目录一览:
如何使用java旋转图片
废话不多说直接上代码:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
public class ImageChange {
public static InputStream rotateImg(BufferedImage image, int degree, Color bgcolor) throws IOException {
int iw = image.getWidth();//原始图象的宽度
int ih = image.getHeight();//原始图象的高度
int w = 0;
int h = 0;
int x = 0;
int y = 0;
degree = degree % 360;
if (degree 0)
degree = 360 + degree;//将角度转换到0-360度之间
double ang = Math.toRadians(degree);//将角度转为弧度
/**
*确定旋转后的图象的高度和宽度
*/
if (degree == 180 || degree == 0 || degree == 360) {
w = iw;
h = ih;
} else if (degree == 90 || degree == 270) {
w = ih;
h = iw;
} else {
int d = iw + ih;
w = (int) (d * Math.abs(Math.cos(ang)));
h = (int) (d * Math.abs(Math.sin(ang)));
}
x = (w / 2) - (iw / 2);//确定原点坐标
y = (h / 2) - (ih / 2);
BufferedImage rotatedImage = new BufferedImage(w, h, image.getType());
Graphics2D gs = (Graphics2D)rotatedImage.getGraphics();
if(bgcolor==null){
rotatedImage = gs.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
}else{
gs.setColor(bgcolor);
gs.fillRect(0, 0, w, h);//以给定颜色绘制旋转后图片的背景
}
AffineTransform at = new AffineTransform();
at.rotate(ang, w / 2, h / 2);//旋转图象
at.translate(x, y);
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
op.filter(image, rotatedImage);
image = rotatedImage;
ByteArrayOutputStream byteOut= new ByteArrayOutputStream();
ImageOutputStream iamgeOut = ImageIO.createImageOutputStream(byteOut);
ImageIO.write(image, "png", iamgeOut);
InputStream inputStream = new ByteArrayInputStream(byteOut.toByteArray());
return inputStream;
}
}
java 怎么让一个图形绕一个点旋转360度
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
/**
* @author ZhengYesheng
*/
public class RotateImageCanvas extends Canvas implements Runnable
{
private static final long serialVersionUID = -1997487731464495923L;
BufferedImage img;
BufferedImage rotatedImg;
int degress = 0;
public RotateImageCanvas(BufferedImage img)
{
super();
this.img = img;
new Thread(this).start();
}
@Override
public void run()
{
while (true)
{
//A,与B的代码配合决定旋转的速度
degress += 1;
degress %= 360;
repaint();
try
{
if (degress == 0)
{
//绕一周后等待的时间在这里设置
Thread.sleep(3 * 1000);
}
else
{
//考虑到视觉平滑,这里不应大约40。
Thread.sleep(30);
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics graphics)
{
super.paint(graphics);
//获取旋转指定角度后的图片。为了避免累计误差,这里是用原始图像旋转的
rotatedImg = rotateImage(img, degress);
//绘制旋转后的图片
graphics.drawImage(rotatedImg, 0, 0, this);
}
/**
* 旋转图片为指定角度。
* 注意:1、这个方法实现了图像的基于中点的旋转,要想绕指定点,需要配合Matrix类
* 2、为避免图像被裁切,结果图片的尺寸也需要动态计算
* 3、现在旋转后有黑色背景,如果不需要这个效果,需要设置结果图片的Alpha模式
*
* @param bufferedimage
* 目标图像
* @param degree
* 旋转角度
* @return
*/
private BufferedImage rotateImage(BufferedImage bufferedimage, int degree)
{
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
}
java实现图片旋转90度的问题
类Graphics2D的方法rotate(double theta, double x,
double y)可以用来旋转图片
利用java实现图片翻转的代码
重载渲染控件的paintComponent(Graphics
g)方法.
设你当前图像实例为img,已初始化,需要旋转的角度为ang
public
void
paintComponent(Graphics
g){
super.paintCompoent(g);
Graphics2D
g2d
=
(Graphics2D)g;
g2d.rotate(-angle);
g2d.drawImage(img,0,0,this.getWidth(),this.getHeight(),null);
}
Graphics,Graphics2D
类中有对当前描绘环境进行仿射变换的方法,包括translate,scale,rotate,也可以直接设置仿射变换矩阵,利用这点就可以根据所需要的实现方式来进行描绘.