public static boolean scale(String imagepath,String newpath){
成都創新互聯公司是一家專業提供三臺企業網站建設,專注與網站設計、成都網站設計、html5、小程序制作等業務。10年已為三臺眾多企業、政府機構等服務。創新互聯專業的建站公司優惠進行中。
// 返回一個 BufferedImage,作為使用從當前已注冊 ImageReader 中自動選擇的 ImageReader 解碼所提供 File 的結果
BufferedImage image=null;
try {
image = ImageIO.read(new File(imagepath));
} catch (IOException e) {
System.out.println("讀取圖片文件出錯!"+e.getMessage());
return false;
}
// Image Itemp = image.getScaledInstance(300, 300, image.SCALE_SMOOTH);
double Ratio = 0.0;
if ((image.getHeight() 300) ||(image.getWidth() 300)) {
if (image.getHeight() image.getWidth())
//圖片要縮放的比例
Ratio = 300.0 / image.getHeight();
else
Ratio = 300.0 / image.getWidth();
}
// 根據仿射轉換和插值類型構造一個 AffineTransformOp。
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
// 轉換源 BufferedImage 并將結果存儲在目標 BufferedImage 中。
image = op.filter(image,null);
//image.getScaledInstance(300,300,image.SCALE_SMOOTH);
FileOutputStream out=null;
try {
out = new FileOutputStream(newpath);
ImageIO.write((BufferedImage)image,"bmp",out);
out.close();
} catch (Exception e) {
System.out.println("寫圖片文件出錯!!"+e.getMessage());
return false;
}
return true;
}
package?com.hoo.util;
import?java.awt.Image;
import?java.awt.image.BufferedImage;
import?java.io.File;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.net.MalformedURLException;
import?java.net.URL;
import?javax.imageio.ImageIO;
import?com.sun.image.codec.jpeg.ImageFormatException;
import?com.sun.image.codec.jpeg.JPEGCodec;
import?com.sun.image.codec.jpeg.JPEGEncodeParam;
import?com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
*?bfunction:/b?縮放圖片工具類,創建縮略圖、伸縮圖片比例
*?@author?hoojo
*?@createDate?2012-2-3?上午10:08:47
*?@file?ScaleImageUtils.java
*?@package?com.hoo.util
*?@version?1.0
*/
public?abstract?class?ScaleImageUtils?{
private?static?final?float?DEFAULT_SCALE_QUALITY?=?1f;
private?static?final?String?DEFAULT_IMAGE_FORMAT?=?".jpg";?//?圖像文件的格式?
private?static?final?String?DEFAULT_FILE_PATH?=?"C:/temp-";
/**
*?bfunction:/b?設置圖片壓縮質量枚舉類;
*?Some?guidelines:?0.75?high?quality、0.5??medium?quality、0.25?low?quality
*?@author?hoojo
*?@createDate?2012-2-7?上午11:31:45
*?@file?ScaleImageUtils.java
*?@package?com.hoo.util
*?@project?JQueryMobile
*?@version?1.0
*/
public?enum?ImageQuality?{
max(1.0f),?high(0.75f),?medium(0.5f),?low(0.25f);
private?Float?quality;
public?Float?getQuality()?{
return?this.quality;
}
ImageQuality(Float?quality)?{
this.quality?=?quality;
}
}
private?static?Image?image;
/**
*?bfunction:/b?通過目標對象的大小和標準(指定)大小計算出圖片縮小的比例
*?@author?hoojo
*?@createDate?2012-2-6?下午04:41:48
*?@param?targetWidth?目標的寬度
*?@param?targetHeight?目標的高度
*?@param?standardWidth?標準(指定)寬度
*?@param?standardHeight?標準(指定)高度
*?@return?最小的合適比例
*/
public?static?double?getScaling(double?targetWidth,?double?targetHeight,?double?standardWidth,?double?standardHeight)?{
double?widthScaling?=?0d;
double?heightScaling?=?0d;
if?(targetWidth??standardWidth)?{
widthScaling?=?standardWidth?/?(targetWidth?*?1.00d);
}?else?{
widthScaling?=?1d;
}
if?(targetHeight??standardHeight)?{
heightScaling?=?standardHeight?/?(targetHeight?*?1.00d);
}?else?{
heightScaling?=?1d;
}
return?Math.min(widthScaling,?heightScaling);
}
也就是圖片壓縮,可以不修改任何大小的壓縮(速度快),也可等比例修改大小壓縮(較慢)
下面這是一段等比例縮小圖片的方法。
public String compressPic(String inputDir, String outputDir,
String inputFileName, String outputFileName, int width,
int height, boolean gp,String hzm) {
try {
if (!image.exists()) {
return "";
}
Image img = ImageIO.read(image);
// 判斷圖片格式是否正確
if (img.getWidth(null) == -1) {
return "no";
} else {
int newWidth; int newHeight;
// 判斷是否是等比縮放
if (gp == true) {
// 為等比縮放計算輸出的圖片寬度及高度
double rate1 = ((double) img.getWidth(null)) / (double) width ;
double rate2 = ((double) img.getHeight(null)) / (double) height ;
// 根據縮放比率大的進行縮放控制
double rate = rate1 rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = img.getWidth(null); // 輸出的圖片寬度
newHeight = img.getHeight(null); // 輸出的圖片高度
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的
* 優先級比速度高 生成的圖片質量比較好 但速度慢
*/
Image im = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
tag.getGraphics().drawImage(im, 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
//JPEGImageEncoder可適用于其他圖片類型的轉換
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return "ok";
}
//將圖按比例縮小。
public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分別表示目標長和寬
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
//這里想實現在targetW,targetH范圍內實現等比縮放。如果不需要等比縮放
//則將下面的if else語句注釋即可
if(sxsy)
{
sx = sy;
targetW = (int)(sx * source.getWidth());
}else{
sy = sx;
targetH = (int)(sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { //handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics();
//smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)
throws Exception {
BufferedImage srcImage;
// String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());
String imgType = "JPEG";
if (fromFileStr.toLowerCase().endsWith(".png")) {
imgType = "PNG";
}
// System.out.println(ex);
File saveFile=new File(saveToFileStr);
File fromFile=new File(fromFileStr);
srcImage = ImageIO.read(fromFile);
if(width 0 || hight 0)
{
srcImage = resize(srcImage, width, hight);
}
ImageIO.write(srcImage, imgType, saveFile);
}
public static void main (String argv[]) {
try{
//參數1(from),參數2(to),參數3(寬),參數4(高)
saveImageAsJpg("C:\\Documents and Settings\\xugang\\桌面\\tmr-06.jpg",
"C:\\Documents and Settings\\xugang\\桌面\\2.jpg",
120,120);
} catch(Exception e){
e.printStackTrace();
}
}
名稱欄目:java生成縮略圖代碼 java生成縮略圖代碼大全
分享網址:http://vcdvsql.cn/article36/doiehpg.html
成都網站建設公司_創新互聯,為您提供定制網站、網頁設計公司、響應式網站、虛擬主機、網站排名、面包屑導航
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯