Java image scaling and cutting:

Source: Internet
Author: User

Java image scaling and cutting:

  1. PackageAction;
  2. ImportJava. AWT. graphics;
  3. ImportJava. AWT. image;
  4. ImportJava. AWT. toolkit;
  5. ImportJava. AWT. image. bufferedimage;
  6. ImportJava. AWT. image. cropimagefilter;
  7. ImportJava. AWT. image. filteredimagesource;
  8. ImportJava. AWT. image. imagefilter;
  9. ImportJava. Io. file;
  10. ImportJava. Io. fileoutputstream;
  11. ImportJava. Io. ioexception;
  12. ImportJavax. ImageIO. ImageIO;
  13. ImportCom.sun.image.codec.jpeg. Unzip codec;
  14. ImportCom.sun.image.codec.jpeg. Specify imageencoder;
  15. Public ClassImageprocess {
  16. /**
  17. * Scale the image
  18. *
  19. * @ Param srcimgfilename
  20. * @ Throws ioexception
  21. */
  22. Public VoidZoomimage (string srcimgfilename)ThrowsIoexception
    {
  23. // Read the file
  24. File _ file =NewFile (srcimgfilename );
  25. // Construct an image object
  26. Bufferedimage src = javax. ImageIO. ImageIO. Read (_ file );
  27. IntWidth = SRC. getwidth ();
  28. IntHeight = SRC. getheight ();
  29. // The length of the side is reduced to 1/2.
  30. Bufferedimage tag =NewBufferedimage (width/2, height/2,
    Bufferedimage. type_int_rgb );
  31. // Draw the reduced Graph
  32. Tag. getgraphics (). drawimage (SRC, 0, 0, width/2,
    Height/2,Null);
  33. Fileoutputstream out =NewFileoutputstream ("D: \ test1 \ targetIMG1-4.jpg ");
  34. Required imageencoder encoder = required codec. createjpegencoder (out );
  35. Encoder. encode (TAG );
  36. Out. Close ();
  37. // The side length is doubled.
  38. Tag =NewBufferedimage (width * 2, height * 2,
    Bufferedimage. type_int_rgb );
  39. Tag. getgraphics (). drawimage (SRC, 0, 0, width * 2,
    Height * 2,Null);
  40. Out =NewFileoutputstream ("D: \ test1 \ targetimgx2.jpg ");
  41. Encoder = export codec. createjpegencoder (out );
  42. Encoder. encode (TAG );
  43. Out. Close ();
  44. }
  45. /**
  46. * Divide an image into nine parts.
  47. *
  48. * @ Param srcimagefile
  49. * @ Throws ioexception
  50. */
  51. Public VoidCut (string srcimagefile)ThrowsIoexception
    {
  52. Image IMG;
  53. Imagefilter cropfilter;
  54. String dir =Null;
  55. // Read the source Image
  56. Bufferedimage src = ImageIO. Read (NewFile (srcimagefile ));
  57. IntDestwidth = SRC. getwidth ()/3;
  58. IntDestheight = SRC. getheight ()/3;
  59. // Loop
  60. For(IntI = 0;
    I <3; I ++ ){
  61. For(IntJ = 0;
    J <3; j ++ ){
  62. // The four parameters are the coordinates of the image start point and width and height.
  63. Cropfilter =NewCropimagefilter (J * destwidth, I * destheight, destwidth, destheight );
  64. IMG = toolkit. getdefatooltoolkit (). createimage (NewFilteredimagesource (SRC. getsource (), cropfilter ));
  65. Bufferedimage tag =NewBufferedimage (destwidth, destheight, bufferedimage. type_int_rgb );
  66. Graphics G = tag. getgraphics ();
  67. G. drawimage (IMG, 0, 0,Null);//
    Draw a thumbnail
  68. G. Dispose ();
  69. // Output as a file
  70. Dir = "D: \ test1 \ cut_image _" + I + "_" + J + ". jpg ";
  71. File F =NewFile (DIR );
  72. ImageIO. Write (TAG, "Jpeg", F );
  73. }
  74. }
  75. }
  76. Public Static VoidMain (string []
    ARGs)ThrowsIoexception {
  77. String imgfilename = "D: \ test \ test.png ";
  78. Imageprocess izoom =NewImageprocess ();
  79. Izoom. zoomimage (imgfilename );
  80. Izoom. Cut (imgfilename );
  81. }
  82. }

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.