How to rotate image at a specific point by a certain angle in java?

☆樱花仙子☆ 提交于 2019-12-25 03:08:52

问题


I want to rotate an image about a specific pixel position and not about the centre of the image. I want to rotate the image by certain degrees less than 90. I've made use of rotate function of the graphics class but i get the image blurred when rotated. How to remove this blur? Is there a specific function for this? This is the code i am using right now:

BufferedImage oldImage = ImageIO.read(new File("C:/omr/OMR_Rotation_1.jpg"));

BufferedImage newImage = new BufferedImage(oldImage.getWidth(), oldImage.getHeight(),oldImage.getType());

            Graphics2D graphics = (Graphics2D) newImage.getGraphics();

            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                    RenderingHints.VALUE_ANTIALIAS_DEFAULT);

            graphics.rotate(Math.toRadians(-2.3), newImage.getWidth() / 2, newImage.getHeight() / 2);

            graphics.translate((newImage.getWidth() - oldImage.getWidth()) / 2, (newImage.getHeight() - oldImage.getHeight()) / 2);

            //graphics.drawImage(oldImage, 0, 0, oldImage.getWidth(), oldImage.getHeight(), null);
            graphics.drawImage(oldImage, null, 0, 0);

            ImageIO.write(newImage, "JPG", new File("C:/omr/OMR_Rotation_2.jpg"));

            graphics.translate((newImage.getWidth() - oldImage.getWidth()) / 2, (newImage.getHeight() - oldImage.getHeight()) / 2);

来源:https://stackoverflow.com/questions/15583933/how-to-rotate-image-at-a-specific-point-by-a-certain-angle-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!