Java rotate the image at any angle and generate a new rotated image (from: http://www.hackchina.com/r/21265/RotateImage.java__html)

Source: Internet
Author: User
Import Java. AWT. graphics2d; import Java. AWT. image; import Java. AWT. mediatracker; import Java. AWT. geom. affinetransform; import Java. AWT. image. affinetransformop; import Java. AWT. image. bufferedimage; import Java. io. file; import Java. io. ioexception; import Java. util. iterator; import javax. imageIO. iioimage; import javax. imageIO. imageIO; import javax. imageIO. imagetypespecifier; import javax. imageIO. imagewriteparam; import javax. imageIO. imagewriter; import javax. imageIO. stream. imageoutputstream; import javax. swing. jpanel; public class rotateimage extends jpanel {image; bufferedimage bufimage; // buffer image for display bufferedimage originalbufimage; // original buffer image graphics2d bufimageg; // public void LoadImage (string filename) {image = This. gettoolkit (). getimage (filename); // obtain the image mediatracker Mt = new mediatracker (this); // instantiate the media loader Mt. addimage (image, 0); // Add the image to the loader. Try {Mt. waitforall (); // wait for image loading} catch (exception ex) {ex. printstacktrace (); // output error message} originalbufimage = new bufferedimage (image. getwidth (this), image. getheight (this), bufferedimage. type_int_argb); // create the original buffer image bufimage = originalbufimage; bufimageg = bufimage. creategraphics (); // create a bufimage graphical environment bufimageg. drawimage (image, 0, 0, this); // transmit source image data to the buffer image} // filter the image public void ratoteimage (INT angle) {If (bufimage = NULL) return; // If the bufimage is null, bufferedimage filteredbufimage = new bufferedimage (image. getwidth (this), image. getheight (this), bufferedimage. type_int_rgb); // The filtered image affinetransform transform = new affinetransform (); // The transform object transform. rotate (Java. lang. math. toradians (angle), image. getwidth (this)/2, image. getheight (this)/2); // rotate the image affinetransformop imageop = new affinetransformop (transform, null); // create the image op object of the affinetransformop operation. filter (originalbufimage, filteredbufimage); // filter the image. The target image is in filteredbufimagebufimage = filteredbufimage; // direct the buffer image used for display to the filtered image drawimg (bufimage );} public void drawimg (bufferedimage image) {imagewriter writer = NULL; imagetypespecifier type = imagetypespecifier. createfromrenderedimage (image); iterator iter = ImageIO. getimagewriters (type, "jpg"); If (ITER. hasnext () {writer = (imagewriter) ITER. next () ;}if (writer = NULL) {return;} iioimage = new iioimage (image, null, null); imagewriteparam Param = writer. getdefaultwriteparam (); Param. setcompressionmode (imagewriteparam. mode_explicit); Param. setcompressionquality (float) (1)/10.0); imageoutputstream outputstream; try {outputstream = ImageIO. createimageoutputstream (new file ("test.jpg"); writer. setoutput (outputstream); writer. write (null, iioimage, Param);} catch (ioexception e) {e. printstacktrace () ;}}/*** @ Param ARGs */public static void main (string [] ARGs) {rotateimage rotateimg = new rotateimage (); rotateimg. loadImage ("bm.jpg"); // load the image rotateimg. ratoteimage (90 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.