Java旋转ImageBuffer失败 [英] Java rotating an ImageBuffer fails
本文介绍了Java旋转ImageBuffer失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试旋转一个名为pic的BufferImage实例时,它会调整大小并歪斜并裁剪图像,有关使其正常工作的任何建议
I am trying to rotate an instance of a BufferImage named pic when I try this it resizes and skews and crops the image, any advice to get it to work properly
public void rotate(double rads){
AffineTransform tx = new AffineTransform();
tx.rotate(rads,pic.getWidth()/2,pic.getHeight()/2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
pic = op.filter(pic, null);
}
当我旋转90°时,它可以正常工作,所以我想知道是否问题是图像的形状吗?
When I have it rotate 90˚ it works fine so I'm wondering if the problem is that it is the shape of the image?
推荐答案
用于 AffineTransform
,则可以使用以下类似的图像对图像进行平方处理:
For use with AffineTransform
, you can square an image using something like this:
private BufferedImage getImage(String name) {
BufferedImage image;
try {
image = ImageIO.read(new File(name));
} catch (IOException ioe) {
return errorImage;
}
int w = image.getWidth();
int h = image.getHeight();
int max = Math.max(w, h);
max = (int) Math.sqrt(2 * max * max);
BufferedImage square = new BufferedImage(
max, max, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = square.createGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(image, (max - w) / 2, (max - h) / 2, null);
g2d.dispose();
return square;
}
这篇关于Java旋转ImageBuffer失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文