課程
/后端開發
/Java
/Java實現圖片等比例縮略圖
請問有課程的代碼嗎??
2018-06-07
源自:Java實現圖片等比例縮略圖 1-1
正在回答
public?class?NarrowImage?{ /** *?@param?im *????????????原始圖像 *?@param?resizeTimes *????????????倍數,比如0.5就是縮小一半,0.98等等double類型 *?@return?返回處理后的圖像 */ public?BufferedImage?zoomImage(String?src)?{ BufferedImage?result?=?null; try?{ File?srcfile?=?new?File(src); if?(!srcfile.exists())?{ System.out.println("文件不存在"); } BufferedImage?im?=?ImageIO.read(srcfile); /*?原始圖像的寬度和高度?*/ int?width?=?im.getWidth(); int?height?=?im.getHeight(); //壓縮計算 float?resizeTimes?=?0.3f;??/*這個參數是要轉化成的倍數,如果是1就是轉化成1倍*/ /*?調整后的圖片的寬度和高度?*/ int?toWidth?=?(int)?(width?*?resizeTimes); int?toHeight?=?(int)?(height?*?resizeTimes); /*?新生成結果圖片?*/ result?=?new?BufferedImage(toWidth,?toHeight, BufferedImage.TYPE_INT_RGB); result.getGraphics().drawImage( im.getScaledInstance(toWidth,?toHeight, java.awt.Image.SCALE_SMOOTH),?0,?0,?null); }?catch?(Exception?e)?{ System.out.println("創建縮略圖發生異常"?+?e.getMessage()); } return?result; } public?boolean?writeHighQuality(BufferedImage?im,?String?fileFullPath)?{ try?{ /*輸出到文件流*/ FileOutputStream?newimage?=?new?FileOutputStream(fileFullPath); JPEGImageEncoder?encoder?=?JPEGCodec.createJPEGEncoder(newimage); JPEGEncodeParam?jep?=?JPEGCodec.getDefaultJPEGEncodeParam(im); /*?壓縮質量?*/ jep.setQuality(0.9f,?true); encoder.encode(im,?jep); /*近JPEG編碼*/ newimage.close(); return?true; }?catch?(Exception?e)?{ return?false; } } public?static?void?main(String[]?args)?{ String?inputFoler?=?"E:\\新建文件夾\\7.jpg"?; /*這兒填寫你存放要縮小圖片的文件夾全地址*/ String?outputFolder?=?"E:\\新建文件夾\\07.jpg"; /*這兒填寫你轉化后的圖片存放的文件夾*/ NarrowImage?narrowImage?=?new?NarrowImage(); narrowImage.writeHighQuality(narrowImage.zoomImage(inputFoler),?outputFolder); } } 代碼給你,希望對你有用 望采納~
舉報
視頻教程以實例的方式,講解Java平臺實現圖片等比例縮略圖生成
1 回答課程有沒有源代碼?
1 回答跪求源碼 --源碼
2 回答課程源碼呢?
1 回答項目源碼在哪?
1 回答大家有運行通的嗎?我的為什么它不出圖片呢?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-07-16