更多参考
使用Java的Graphics2D类的rotate方法实现图片的翻转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
private BufferedImage rotateImage(BufferedImage img) throws IOException { int width = img.getWidth(); int height = img.getHeight(); BufferedImage newImage; double[][] newPositions = new double[4][]; newPositions[0] = this.calculatePosition(0, 0); newPositions[1] = this.calculatePosition(width, 0); newPositions[2] = this.calculatePosition(0, height); newPositions[3] = this.calculatePosition(width, height); double minX = Math.min(Math.min(newPositions[0][0], newPositions[1][0]), Math.min(newPositions[2][0], newPositions[3][0])); double maxX = Math.max(Math.max(newPositions[0][0], newPositions[1][0]), Math.max(newPositions[2][0], newPositions[3][0])); double minY = Math.min(Math.min(newPositions[0][1], newPositions[1][1]), Math.min(newPositions[2][1], newPositions[3][1])); double maxY = Math.max(Math.max(newPositions[0][1], newPositions[1][1]), Math.max(newPositions[2][1], newPositions[3][1])); int newWidth = (int) Math.round(maxX - minX); int newHeight = (int) Math.round(maxY - minY); //newImage = new BufferedImageBuilder(newWidth, newHeight).build(); newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g = newImage.createGraphics(); this.setRenderingHint(g); g.setPaint(this.bgcolor); g.fillRect(0, 0, newWidth, newHeight); /* * TODO consider RenderingHints to use. The following are hints which * have been chosen to give decent image quality. In the future, there * may be a need to have a way to change these settings. */ double w = newWidth / 2.0; double h = newHeight / 2.0; int centerX = (int) Math.round((newWidth - width) / 2.0); int centerY = (int) Math.round((newHeight - height) / 2.0); g.rotate(Math.toRadians(angle), w, h); g.drawImage(img, centerX, centerY, null); g.dispose(); return newImage; } |
调用
1 2 3 4 5 6 7 8 |
String str = ImageUtilsTest.class.getResource("/org.jpg").getPath(); File f = new File(str); ImageUtils.fromFile(f) .scale(1) .rotate(57) //旋转角度 .quality(1) .bgcolor(Color.BLUE) .toFile(new File("d:\\image\\test.jpg")); |
原图如下:
生成的图片如下
下载:前往下载
演示:Demo
你好,我旋转后 的图像是重复旋转的,请问是什么原因呢?
你好,图片生成的代码是怎么写,贴出来看下?