早上主要學(xué)習(xí)了框架中的工具類:
包括為了生成UUID所需要的將bytes[]轉(zhuǎn)成Int,以及java圖像的獲取,放大,縮小及圖像水印。
byte[]轉(zhuǎn)Int
//bytes 轉(zhuǎn)換為 int int 32位范圍[-2^32,2^32) byte 8位 最用范圍是[-2^8,2^8)
public static int toInt(byte[] bytes) {
int result = 0;
//遍歷四次
for (int i = 0; i < 4; i++) {
//<<左位移運算(在二進制狀態(tài)下向左移動8位) 2^8+x
//思想:1Int是4個byte,要將byte轉(zhuǎn)換為int,就算byte為0 也至少滿足10000000100000001000000010000000這樣的形式
//然后每一位byte上具體是多少只要在10000000上添加即可
//如果字節(jié)數(shù)組超過4個轉(zhuǎn)化為Int時由于數(shù)據(jù)過大,導(dǎo)致無法完全轉(zhuǎn)換為Int,所以最終顯示Int范圍內(nèi)的數(shù)據(jù)
result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
}
return result;
}
圖像處理
/** *//**
* 縮放圖像
* @param srcImageFile 源圖像文件地址
* @param result 縮放后的圖像地址
* @param scale 縮放比例
* @param flag 縮放選擇:true 放大; false 縮小;
*/
public static void scale(String srcImageFile, String result, int scale, boolean flag)
{
try
{
BufferedImage src = ImageIO.read(new File(srcImageFile)); // 讀入文件
int width = src.getWidth(); // 得到源圖寬
int height = src.getHeight(); // 得到源圖長
if (flag)
{
// 放大
width = width * scale;
height = height * scale;
}
else
{
// 縮小
width = width / scale;
height = height / scale;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 繪制縮小后的圖
g.dispose();
ImageIO.write(tag, "JPG", new File(result));// 輸出到文件流
}
catch (IOException e)
{
e.printStackTrace();
}
}
圖片水印文字
/**
* 添加文字水印
* @param targetImg 目標(biāo)圖片路徑,如:C://myPictrue//1.jpg
* @param pressText 水印文字, 如:中國證券網(wǎng)
* @param fontName 字體名稱, 如:宋體
* @param fontStyle 字體樣式,如:粗體和斜體(Font.BOLD|Font.ITALIC)
* @param fontSize 字體大小,單位為像素
* @param color 字體顏色
* @param x 水印文字距離目標(biāo)圖片左側(cè)的偏移量,如果x<0, 則在正中間
* @param y 水印文字距離目標(biāo)圖片上側(cè)的偏移量,如果y<0, 則在正中間
* @param alpha 透明度(0.0 -- 1.0, 0.0為完全透明,1.0為完全不透明)
*/
public static void pressText( File file, String pressText, String fontName, int fontStyle, int fontSize, Color color, int x, int y, float alpha) {
try {
Image image = ImageIO.read(file);
int width = image.getWidth(null);
int height = image.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.setColor(color);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int width_1 = fontSize * getLength(pressText);
int height_1 = fontSize;
int widthDiff = width - width_1;
int heightDiff = height - height_1;
if(x < 0){
x = widthDiff / 2;
}else if(x > widthDiff){
x = widthDiff;
}
if(y < 0){
y = heightDiff / 2;
}else if(y > heightDiff){
y = heightDiff;
}
g.drawString(pressText, x, y + height_1);
g.dispose();
ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取字符長度,一個漢字作為 1 個字符, 一個英文字母作為 0.5 個字符
* @param text
* @return 字符長度,如:text="中國",返回 2;text="test",返回 2;text="中國ABC",返回 4.
* @throws UnsupportedEncodingException
*/
public static int getLength(String text) throws UnsupportedEncodingException {
int textLength = text.length();
int length = textLength;
for (int i = 0; i < textLength; i++) {
if (String.valueOf(text.charAt(i)).getBytes("utf-8").length > 1) {
length++;
}
}
return (length % 2 == 0) ? length / 2 : length / 2 + 1;
}