java中壓縮圖片的代碼輔助類

來源:互聯網
上載者:User

 

package cn.com.images;import java.awt.Graphics;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.math.BigDecimal;import java.math.MathContext;import java.util.ArrayList;import javax.imageio.ImageIO;/*** * 對圖片進行操作 *  * @author chenzheng_java * @since 2011/7/29 *  */public class ImageHelper {private static ImageHelper imageHelper = null;public static ImageHelper getImageHelper() {if (imageHelper == null) {imageHelper = new ImageHelper();}return imageHelper;}/*** * 按指定的比例縮放圖片 *  * @param sourceImagePath *            源地址 * @param destinationPath *            改變大小後圖片的地址 * @param scale *            縮放比例,如1.2 */public static void scaleImage(String sourceImagePath,String destinationPath, double scale,String format) {File file = new File(sourceImagePath);BufferedImage bufferedImage;try {bufferedImage = ImageIO.read(file);int width = bufferedImage.getWidth();int height = bufferedImage.getHeight();width = parseDoubleToInt(width * scale);height = parseDoubleToInt(height * scale);Image image = bufferedImage.getScaledInstance(width, height,Image.SCALE_SMOOTH);BufferedImage outputImage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics graphics = outputImage.getGraphics();graphics.drawImage(image, 0, 0, null);graphics.dispose();ImageIO.write(outputImage, format, new File(destinationPath));} catch (IOException e) {System.out.println("scaleImage方法壓縮圖片時出錯了");e.printStackTrace();}}/*** * 將圖片縮放到指定的高度或者寬度 * @param sourceImagePath 圖片源地址 * @param destinationPath 壓縮完圖片的地址 * @param width 縮放後的寬度 * @param height 縮放後的高度 * @param auto 是否自動保持圖片的原高寬比例 * @param format 圖圖片格式 例如 jpg */public static void scaleImageWithParams(String sourceImagePath,String destinationPath, int width, int height, boolean auto,String format) {try {File file = new File(sourceImagePath);BufferedImage bufferedImage = null;bufferedImage = ImageIO.read(file);if (auto) {ArrayList<Integer> paramsArrayList = getAutoWidthAndHeight(bufferedImage,width,height);width = paramsArrayList.get(0);height = paramsArrayList.get(1);System.out.println("自動調整比例,width="+width+" height="+height);}Image image = bufferedImage.getScaledInstance(width, height,Image.SCALE_DEFAULT);BufferedImage outputImage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics graphics = outputImage.getGraphics();graphics.drawImage(image, 0, 0, null);graphics.dispose();ImageIO.write(outputImage, format, new File(destinationPath));} catch (Exception e) {System.out.println("scaleImageWithParams方法壓縮圖片時出錯了");e.printStackTrace();}}/** * 將double類型的資料轉換為int,四捨五入原則 *  * @param sourceDouble * @return */private static int parseDoubleToInt(double sourceDouble) {int result = 0;result = (int) sourceDouble;return result;}/*** *  * @param bufferedImage 要縮放的圖片對象 * @param width_scale 要縮放到的寬度 * @param height_scale 要縮放到的高度 * @return 一個集合,第一個元素為寬度,第二個元素為高度 */private static ArrayList<Integer> getAutoWidthAndHeight(BufferedImage bufferedImage,int width_scale,int height_scale){ArrayList<Integer> arrayList = new ArrayList<Integer>();int width = bufferedImage.getWidth();int height = bufferedImage.getHeight();double scale_w =getDot2Decimal( width_scale,width);System.out.println("getAutoWidthAndHeight width="+width + "scale_w="+scale_w);double scale_h = getDot2Decimal(height_scale,height);if (scale_w<scale_h) {arrayList.add(parseDoubleToInt(scale_w*width));arrayList.add(parseDoubleToInt(scale_w*height));}else {arrayList.add(parseDoubleToInt(scale_h*width));arrayList.add(parseDoubleToInt(scale_h*height));}return arrayList;}/*** * 返回兩個數a/b的小數點後三位的表示 * @param a * @param b * @return */public static double getDot2Decimal(int a,int b){BigDecimal bigDecimal_1 = new BigDecimal(a);BigDecimal bigDecimal_2 = new BigDecimal(b);BigDecimal bigDecimal_result = bigDecimal_1.divide(bigDecimal_2,new MathContext(4));Double double1 = new Double(bigDecimal_result.toString());System.out.println("相除後的double為:"+double1);return double1;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.