java圖片處理類(圖片水印,圖片縮放)(2)_JSP教程
推薦:jsp switch語句的用法如果希望選擇執(zhí)行若干代碼塊中的一個,你可以使用switch語句: 語法: switch(n) { case 1: 執(zhí)行代碼塊 1 break case 2: 執(zhí)行代碼塊 2 break default: 如果n即不是1也不是2,則執(zhí)行此代碼 } 工作原理:switch后面的(n)可以是表達式,也可以(并通常)是變量。然后表達
/**
* 縮放圖像(按高度和寬度縮放)
* @param srcImageFile 源圖像文件地址
* @param result 縮放后的圖像地址
* @param height 縮放后的高度
* @param width 縮放后的寬度
* @param bb 比例不對時是否需要補白:true為補白; false為不補白;
*/
public final static void scale2(String srcImageFile, String result, int height, int width, boolean bb) {
try {
double ratio = 0.0; // 縮放比例
File f = new File(srcImageFile);
BufferedImage bi = ImageIO.read(f);
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
// 計算比例
if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
if (bi.getHeight() > bi.getWidth()) {
ratio = (new Integer(height)).doubleValue()
/ bi.getHeight();
} else {
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
if (bb) {//補白
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == itemp.getWidth(null))
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
else
g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "JPEG", new File(result));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 圖像切割(按指定起點坐標和寬高切割)
* @param srcImageFile 源圖像地址
* @param result 切片后的圖像地址
* @param x 目標切片起點坐標X
* @param y 目標切片起點坐標Y
* @param width 目標切片寬度
* @param height 目標切片高度
*/
public final static void cut(String srcImageFile, String result,
int x, int y, int width, int height) {
try {
// 讀取源圖像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getHeight(); // 源圖寬度
int srcHeight = bi.getWidth(); // 源圖高度
if (srcWidth > 0 && srcHeight > 0) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,
Image.SCALE_DEFAULT);
// 四個參數(shù)分別為圖像起點坐標和寬高
// 即: CropImageFilter(int x,int y,int width,int height)
ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
Image img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(),
cropFilter));
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, width, height, null); // 繪制切割后的圖
g.dispose();
// 輸出為文件
ImageIO.write(tag, "JPEG", new File(result));
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 圖像切割(指定切片的行數(shù)和列數(shù))
* @param srcImageFile 源圖像地址
* @param descDir 切片目標文件夾
* @param rows 目標切片行數(shù)。默認2,必須是范圍 [1, 20] 之內(nèi)
* @param cols 目標切片列數(shù)。默認2,必須是范圍 [1, 20] 之內(nèi)
*/
public final static void cut2(String srcImageFile, String descDir,
int rows, int cols) {
try {
if(rows<=0||rows>20) rows = 2; // 切片行數(shù)
if(cols<=0||cols>20) cols = 2; // 切片列數(shù)
// 讀取源圖像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getHeight(); // 源圖寬度
int srcHeight = bi.getWidth(); // 源圖高度
if (srcWidth > 0 && srcHeight > 0) {
Image img;
ImageFilter cropFilter;
Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
int destWidth = srcWidth; // 每張切片的寬度
int destHeight = srcHeight; // 每張切片的高度
// 計算切片的寬度和高度
if (srcWidth % cols == 0) {
destWidth = srcWidth / cols;
} else {
destWidth = (int) Math.floor(srcWidth / cols) + 1;
}
if (srcHeight % rows == 0) {
destHeight = srcHeight / rows;
} else {
destHeight = (int) Math.floor(srcWidth / rows) + 1;
}
// 循環(huán)建立切片
// 改進的想法:是否可用多線程加快切割速度
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 四個參數(shù)分別為圖像起點坐標和寬高
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(j * destWidth, i * destHeight,
destWidth, destHeight);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(),
cropFilter));
BufferedImage tag = new BufferedImage(destWidth,
destHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 繪制縮小后的圖
g.dispose();
// 輸出為文件
ImageIO.write(tag, "JPEG", new File(descDir
+ "_r" + i + "_c" + j + ".jpg"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
分享:jsp if else語句使用方法if else在大部份編程語言中都是這樣使用的,我們今天來簡單的介紹一下關(guān)于jsp教程 中的if else 與多重條件判斷。 HTML HEAD TITLEUsing the if Statement/TITLE /HEAD BODY H1Using the if Statement/H1 % int value = 10; if(value 0) out.println(Absolute value of
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復習整理
- JSP腳本元素和注釋復習總結(jié)示例
- JSP FusionCharts Free顯示圖表 具體實現(xiàn)
- 網(wǎng)頁模板:關(guān)于jsp頁面使用jstl的異常分析
- JSP頁面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項目中連接Access數(shù)據(jù)庫的配置方法
- JDBC連接Access數(shù)據(jù)庫的幾種方式介紹
- 網(wǎng)站圖片路徑的問題:絕對路徑/虛擬路徑
- (jsp/html)網(wǎng)頁上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對路徑下的圖片解決方法
- 相關(guān)鏈接:
- 教程說明:
JSP教程-java圖片處理類(圖片水印,圖片縮放)(2)
。