在Java中旋转图像 [英] Rotating an Image in java
问题描述
我有一张潘卡的图像,当我尝试将它旋转45度并保存时,我得到一张裁剪后的图像。
旋转图片的代码是:
$ p $ BufferedImage dimg = new BufferedImage(w,h,img.getType() );
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,// Anti-alias!
RenderingHints.VALUE_ANTIALIAS_ON);
g.rotate(Math.toRadians(angle),w / 2,h / 2);
g.drawImage(img,null,0,0);
看看这个使用AffineTransform的例子: / p>
http ://www.billthelizard.com/2008/07/rotate-image-in-java.html
有一些代码可以加载图片,然后这是核心:
私人图片图片;
AffineTransform identity = new AffineTransform();
Graphics2D g2d =(Graphics2D)g;
AffineTransform trans = new AffineTransform();
trans.setTransform(identity);
trans.rotate(Math.toRadians(45));
g2d.drawImage(image,trans,this);
I have an image of a Pan Card and when I try to rotate it by 45 degrees and save it, I get a cropped image. Code to rotate an image is:
BufferedImage dimg = new BufferedImage(w, h, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
RenderingHints.VALUE_ANTIALIAS_ON);
g.rotate(Math.toRadians(angle), w / 2, h / 2);
g.drawImage(img, null, 0, 0);
Have a look at this example, using AffineTransform:
http://www.billthelizard.com/2008/07/rotate-image-in-java.html
there's some code to load the image, then this is the core:
private Image image;
AffineTransform identity = new AffineTransform();
Graphics2D g2d = (Graphics2D)g;
AffineTransform trans = new AffineTransform();
trans.setTransform(identity);
trans.rotate( Math.toRadians(45) );
g2d.drawImage(image, trans, this);
这篇关于在Java中旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!