package
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import javax
/**
* @author Eric Xu
*
*/
public final class ImageUtils {
/**
* 圖片水印
* @param pressImg 水印圖片
* @param targetImg 目標圖片
* @param x 修正值 默認在中間
* @param y 修正值 默認在中間
* @param alpha 透明度
*/
public final static void pressImage(String pressImg
try {
File img = new File(targetImg);
Image src = ImageIO
int wideth = src
int height = src
BufferedImage image = new BufferedImage(wideth
Graphics
g
//水印文件
Image src_biao = ImageIO
int wideth_biao = src_biao
int height_biao = src_biao
g
g
//水印文件結束
g
ImageIO
} catch (Exception e) {
e
}
}
/**
* 文字水印
* @param pressText 水印文字
* @param targetImg 目標圖片
* @param fontName 字體名稱
* @param fontStyle 字體樣式
* @param color 字體顏色
* @param fontSize 字體大小
* @param x 修正值
* @param y 修正值
* @param alpha 透明度
*/
public static void pressText(String pressText
try {
File img = new File(targetImg);
Image src = ImageIO
int width = src
int height = src
BufferedImage image = new BufferedImage(width
Graphics
g
g
g
g
g
g
ImageIO
} catch (Exception e) {
e
}
}
/**
* 縮放
* @param filePath 圖片路徑
* @param height 高度
* @param width 寬度
* @param bb 比例不對時是否需要補白
*/
public static void resize(String filePath
try {
double ratio =
File f = new File(filePath);
BufferedImage bi = ImageIO
Image itemp = bi
//計算比例
if ((bi
if (bi
ratio = (new Integer(height))
} else {
ratio = (new Integer(width))
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
itemp = op
}
if (bb) {
BufferedImage image = new BufferedImage(width
Graphics
g
g
if (width == itemp
g
else
g
g
itemp = image;
}
ImageIO
} catch (IOException e) {
e
}
}
public static void main(String[] args) throws IOException {
pressImage(
pressText(
resize(
}
public static int getLength(String text) {
int length =
for (int i =
if (new String(text
length +=
} else {
length +=
}
}
return length /
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26874.html