Generate reduced JPG image programs

xiaoxiao2021-03-06  107

import java.awt.image.BufferedImage; import java.io.File; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.imageio.ImageIO; import java.awt.image.AffineTransformOp; import java.awt Import java.awt.Image;

Public class zoompicture {public static void main (string filepath = "g: / picture .jpg"; // Picture location int Height = 50; int width = 150; icon icon = null; try {icon = getFixedBoundicon (FilePath, Height, Width);} catch (Exception E) {system.out.println ("Exception:" E);} system.out.println ("###" icon); // Generate The location of the new picture;} public static icon getfixedboundicon (String Filepath, Int Height, int width "throws exception {double ratio = 0.0; // Zoom scaling file f = new file (filepath); if (! F.isfile ()) Throw new Exception (f "is not image file error in getfixedboundicon!"); icon ret = new imageicon (filepath); bufferedimage bi = imageio.read (f); if ((bi.getHeight ()> height) || Bi.GetWidth ()> width) {if (bi.getHeight ()> bi.getwidth ()) {ratio = (new integer ()). DoubleValue () /bi.getHeight ();} else {ratio = NEW INTEGER (Width). DoubleValue () / bi.getwidth (); } Int lastLength = filePath.lastIndexOf ( "."); String subFilePath = filePath.substring (0, lastLength); String fileType = filePath.substring (lastLength); File zoomFile = new File (subFilePath "_" height "_ " width fileType); Image Itemp = Bi.getScaledInstance (width, height, Bi.SCALE_SMOOTH); AffineTransformOp op = new AffineTransformOp (AffineTransform.getScaleInstance (Ratio, Ratio), null); Itemp = op.filter (Bi, null Try {imageio.write ((BufferedImage) Itemp, "JPG", Zoomfile; Ret = new imageicon (zoomfile.getpath ());

转载请注明原文地址:https://www.9cbs.com/read-103551.html

New Post(0)