Rotate a buffered image in Java -
i trying rotate buffered image in java. here code using:
public static bufferedimage rotate(bufferedimage bimg, double angle){ int w = bimg.getwidth(); int h = bimg.getheight(); graphics2d graphic = bimg.creategraphics(); graphic.rotate(math.toradians(angle), w/2, h/2); graphic.drawimage(bimg, null, 0, 0); graphic.dispose(); return bimg; }
i have looked numerous stack overflow questions , answers on topic , not been able figure out why image chopped way when try rotate it. here example showing loaded image: loaded image
after click rotate button calls above function buffered image , 90.0 angle: chopped image
can me understand happening , how fix it?
thanks!
as always, internet rescue. so, code hobbled other resources/post/blogs return new image sized contain rotated image
public bufferedimage rotateimagebydegrees(bufferedimage img, double angle) { double rads = math.toradians(angle); double sin = math.abs(math.sin(rads)), cos = math.abs(math.cos(rads)); int w = img.getwidth(); int h = img.getheight(); int newwidth = (int) math.floor(w * cos + h * sin); int newheight = (int) math.floor(h * cos + w * sin); bufferedimage rotated = new bufferedimage(newwidth, newheight, bufferedimage.type_int_argb); graphics2d g2d = rotated.creategraphics(); affinetransform @ = new affinetransform(); at.translate((newwidth - w) / 2, (newheight - h) / 2); int x = w / 2; int y = h / 2; at.rotate(rads, x, y); g2d.settransform(at); g2d.drawimage(img, 0, 0, this); g2d.setcolor(color.red); g2d.drawrect(0, 0, newwidth - 1, newheight - 1); g2d.dispose(); return rotated; }
updated
so, using png:
and code...
package javaapplication1.pkg040; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.geom.affinetransform; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private bufferedimage master; private bufferedimage rotated; public testpane() { try { master = imageio.read(new file("/volumes/disk02/dropbox/megatokyo/miho_small.png")); rotated = rotateimagebydegrees(master, 0.0); } catch (ioexception ex) { ex.printstacktrace(); } timer timer = new timer(40, new actionlistener() { private double angle = 0; private double delta = 1.0; @override public void actionperformed(actionevent e) { angle += delta; rotated = rotateimagebydegrees(master, angle); repaint(); } }); timer.start(); } @override public dimension getpreferredsize() { return master == null ? new dimension(200, 200) : new dimension(master.getwidth(), master.getheight()); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); if (rotated != null) { graphics2d g2d = (graphics2d) g.create(); int x = (getwidth() - rotated.getwidth()) / 2; int y = (getheight() - rotated.getheight()) / 2; g2d.drawimage(rotated, x, y, this); g2d.dispose(); } } public bufferedimage rotateimagebydegrees(bufferedimage img, double angle) { double rads = math.toradians(angle); double sin = math.abs(math.sin(rads)), cos = math.abs(math.cos(rads)); int w = img.getwidth(); int h = img.getheight(); int newwidth = (int) math.floor(w * cos + h * sin); int newheight = (int) math.floor(h * cos + w * sin); bufferedimage rotated = new bufferedimage(newwidth, newheight, bufferedimage.type_int_argb); graphics2d g2d = rotated.creategraphics(); affinetransform @ = new affinetransform(); at.translate((newwidth - w) / 2, (newheight - h) / 2); int x = w / 2; int y = h / 2; at.rotate(rads, x, y); g2d.settransform(at); g2d.drawimage(img, 0, 0, this); g2d.dispose(); return rotated; } } }
i can generate like...
Comments
Post a Comment