Java scale picture and output, support JPG and PNG input and same type output

Source: Internet
Author: User
Tags return tag

Java scale picture and output, support JPG and PNG input and same type output

Import Java.io.File;
Import java.io.IOException;
Import Java.awt.image.BufferedImage;
Import Java.awt.Image;
Import java.awt.image.AffineTransformOp;
Import Javax.imageio.ImageIO;
Import cn.sharcom.config.Constant;
Import Java.awt.geom.AffineTransform;
 /** * * @param strdestfile * source file * @param strsourcefile * Target file * @param width * Output size Width * @param height * Output Size Height * @return/public static Integer ImageZoom1 (Str

    ing strDestFile, String strsourcefile, int width, int height) {Integer result = null;
    Open source File FileInput = new file (strsourcefile);
      if (fileinput.isfile () = = False) {String msg = String.Format ("Imagezoom No found file%s", strsourcefile);
      result = Constant.result_none;
    return result;
    }//suffix default jpg String strsuffix = "jpg";
    1+ eliminates "."
    int point = Strsourcefile.lastindexof (".") + 1; Get extension if (Point > 0 && Point < Strsourcefile.length ()) {int len = Strsourcefile.len based on incoming filename)
      Gth ();
      String Strfiletype = strsourcefile.substring (point, Len);
  if (strfiletype.length () > 0) {strfiletype = Strfiletype.tolowercase ();      Get a new extension strsuffix = Strfiletype;
    }//Open file Fileoutput = null;
    Read the picture bufferedimage bufferedimage = null;
      try {fileoutput = new File (strdestfile);
    BufferedImage = Imageio.read (fileInput);
      catch (IOException e) {e.printstacktrace (); String msg = String.Format ("Imagezoom failed to read the file.")
      %s ", strsourcefile);
      result = Constant.result_error;
    return result;
    Image image = Bufferedimage.getscaledinstance (width, height, bufferedimage.scale_smooth);
    Gets the original dimension int intheightold = Bufferedimage.getheight ();
    int intwidthold = Bufferedimage.getwidth ();
    Calculates the new dimension scale double Intwidthratio = (double) width/double intwidthold;
    Double intheightratio = (double) height/(double) intheightold; Set proportional Affinetransformop affinetransformop = new Affinetransformop (affinetransform.getscaleinstance (IntWidthRatio, I
    Ntheightratio), NULL); Image = AffInetransformop.filter (bufferedimage, NULL);
      try {///output the file as a specified type Imageio.write ((bufferedimage) image, Strsuffix, fileoutput);
    Set return tag result = constant.result_success; catch (Exception e) {String msg = String.Format ("Imagezoom write error%s,%s", E.getmessage (), Strdestfi
      Le);
      result = Constant.result_error;
    return result;
  return result;
  public static void Main (string[] args) {ImageZoom1 ("c:/b.jpg", "c:/1239501964fe4306.jpg", 3000, 4000); }

Q Group discussion: 236201801


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.