java上傳圖片放大(小圖等比放大,大圖等比裁剪)
java上傳圖片放大(小圖等比放大,大圖等比裁剪):
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;/**
- 上傳圖片-小圖片放大圖片不變型,但會模糊(取決于圖片本身的像素)
- @author sunlight
*/
public class AmplificationImage {/**
- 判斷圖片是否大于目標尺寸
- @param srcPath
- @param maxWidth
- @param maxHeight
- @return
*/
public static boolean isBigImage(String srcPath, int maxWidth, int maxHeight) {
BufferedImage bufferedImage = null;
try {
} catch (Exception e) {File of = new File(srcPath); if (of.canRead()) { bufferedImage = ImageIO.read(of); }
}return false;
if (bufferedImage != null) {
}int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); if (width > maxWidth && height > maxHeight) { return true; }
return false;
}
/**
- 圖片放大的方法(不會變色)
- @param inputUrl 圖片輸入路勁
- @param outputUrl 圖片輸出路勁
- @param maxWidth 目標寬
- @param maxHeight 目標高
- @param proportion 是否等比縮放
@return */
public static boolean zoomPicture(String inputUrl, String outputUrl,int maxWidth, int maxHeight, boolean proportion) {
try {
// 獲得源文件 File file = new File(inputUrl); if (!file.exists()) { return false; } Image img = ImageIO.read(file); // 判斷圖片格式是否正確 if (img.getWidth(null) == -1) { return false; } else { int newWidth; int newHeight; // 判斷是否是等比縮放 if (proportion == true) { // 為等比縮放計算輸出的圖片寬度及高度 double rate1 = ((double) img.getWidth(null)) / (double) maxWidth; double rate2 = ((double) img.getHeight(null)) / (double) maxHeight; // 根據縮放比率大的進行縮放控制 double rate = rate1 > rate2 ? rate2 : rate1; newWidth = (int) (((double) img.getWidth(null)) / rate); newHeight = (int) (((double) img.getHeight(null)) / rate); } else { newWidth = maxWidth; // 輸出的圖片寬度 newHeight = maxHeight; // 輸出的圖片高度 } BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage( img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null); FileOutputStream out = new FileOutputStream(outputUrl); // JPEGImageEncoder可適用于其他圖片類型的轉換 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); }
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
/**
- 對圖片進行放大(部分圖片會變紅)
- @param srcPath
- 原始圖片路徑(絕對路徑)
- @param newPath
- 放大后圖片路徑(絕對路徑)
- @param times
- 放大倍數
- @return 是否放大成功 */
public static boolean zoomInImage(String srcPath, String newPath,
int maxWidth, int maxHeight) { BufferedImage bufferedImage = null; try { File of = new File(srcPath); if (of.canRead()) { bufferedImage = ImageIO.read(of); } } catch (IOException e) { // TODO: 打印日志 return false; } if (bufferedImage != null) { bufferedImage = zoomInImage(bufferedImage, maxWidth, maxHeight); try { // TODO: 這個保存路徑需要配置下子好一點 ImageIO.write(bufferedImage, "JPG", new File(newPath)); // 保存修改后的圖像,全部保存為JPG格式 } catch (IOException e) { // TODO 打印錯誤信息 return false; } } return true;
}
/**
- 對圖片進行放大
- @param originalImage
- 原始圖片
- @param maxWidth
- 目標寬度
- @param maxHeight
- 目標高度
- @return
*/
private static BufferedImage zoomInImage(BufferedImage originalImage,
int times = 1; // 放大倍數int maxWidth, int maxHeight) {
int width = originalImage.getWidth();
int height = originalImage.getHeight();
double sw = (maxWidth 1.0) / (width 1.0);
double sh = (maxHeight 1.0) / (height 1.0);
if (width > maxWidth && height > maxHeight) {
} else if (width < maxWidth && height < maxHeight) {return originalImage;
} else if (width < maxWidth && height > maxHeight) {if (sw > sh) { times = (int) (sw + 0.8); } else { times = (int) (sh + 0.8); }
} else {times = (int) (sw + 0.8);
}times = (int) (sh + 0.8);
int lastW = times width;
int lastH = times height;
BufferedImage newImage = new BufferedImage(lastW, lastH, originalImage
Graphics g = newImage.getGraphics();.getType());
g.drawImage(originalImage, 0, 0, lastW, lastH, null);
g.dispose();
return newImage;
}
} </pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!