亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

正在回答

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);
		
	}
}

代碼給你,希望對你有用
望采納~


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Java實現圖片等比例縮略圖
  • 參與學習       25330    人
  • 解答問題       17    個

視頻教程以實例的方式,講解Java平臺實現圖片等比例縮略圖生成

進入課程

詳細的代碼

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號