圖片處理工具類,
包括:讀取本地圖片、網絡圖片,圖片合成(圖片水印),圖片上寫文字(文字水印),16進制和Color對象的互轉
package com.chat;
java根據數據生成圖片、import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
Java圖片。import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
好聽的快手昵稱,import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
javaweb。/**
* 圖片處理工具類
*
* @author Daven
java方法,*
*/
public class ImgUtil {
public static void main(String[] args) {
python和java,ImgUtil.composePic("D:/back.png", "http://t3.qlogo.cn/mbloghead/d387538f629cfb53c2b0/100", "D:/output.png", 53, 161);
}
/**
* 合成圖片(類似圖片水印)
Java生成圖片。* @param backImage 背景圖片對象
* @param headImage 用戶頭像對象
* @param outPutPath 輸出路徑(D:/img/out.png)
* @param leftX 距離目標圖片左側的偏移量
java圖片處理框架?* @param leftY 距離目標圖片左側的偏移量
* @throws InterruptedException
* @throws IOException
*/
java讀取圖片,public static void composePic(Image backImage, Image headImage,
String outPutPath, int leftX, int leftY)
throws InterruptedException, IOException {
// 圖片的高/寬度
int bwidth = backImage.getWidth(null);
int bheight = backImage.getHeight(null);
int hwidth = headImage.getWidth(null);
int hheight = headImage.getHeight(null);
int alphaType = BufferedImage.TYPE_INT_RGB;
/*if (hasAlpha(backImage)) {
alphaType = BufferedImage.TYPE_INT_ARGB;
}*/
// 畫圖
BufferedImage backgroundImage = new BufferedImage(bwidth, bheight, alphaType);
Graphics2D graphics2D = backgroundImage.createGraphics();
graphics2D.drawImage(backImage, 0, 0, null);
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
graphics2D.drawImage(headImage, leftX, leftY, hwidth, hheight, null);
// 輸出
ImageIO.write(backgroundImage, "png", new File(outPutPath));
}
/**
* 合成圖片
* @param backPicPath 背景圖片本地路徑
* @param headPicUrl 用戶頭像網絡url
* @param outPutPath 輸出路徑(D:/img/out.png)
* @param leftX 距離目標圖片左側的偏移量
* @param leftY 距離目標圖片左側的偏移量
*/
public static void composePic(String backPicPath, String headPicUrl,
String outPutPath, int leftX, int leftY) {
try {
// 讀取背景圖片
Image backImage = ImgUtil.loadImageLocal(backPicPath);
// 加昵稱
int ln = ImgUtil.getLength("深水魚的馬甲") * 24;
backImage = ImgUtil.pressText("深水魚的馬甲", backImage, "微軟雅黑", Font.BOLD,
ImgUtil.String2Color("#FFFFFF"), 24, 175+(266-ln)/2, 188, 1);
// 得分
backImage = ImgUtil.pressText("100分", backImage, "華康海報體W12(P)", Font.LAYOUT_NO_LIMIT_CONTEXT,
ImgUtil.String2Color("#FD6E10"), 22, 280, 233, 1);
// 收益
backImage = ImgUtil.pressText("85%", backImage, "迷你簡凌波", Font.LAYOUT_NO_LIMIT_CONTEXT,
ImgUtil.String2Color("#E60000"), 24, 368, 264, 1);
// 稱號
int lc = ImgUtil.getLength("有股神潛力,萬眾膜拜!") * 32;
backImage = ImgUtil.pressText("有股神潛力,萬眾膜拜!", backImage, "華康海報體W12(P)", Font.LAYOUT_NO_LIMIT_CONTEXT,
ImgUtil.String2Color("#880000"), 32, (488-lc)/2, 310, 1);
// 讀取頭像圖片
Image headImage = ImgUtil.loadImageUrl(headPicUrl);
// 圖像
ImgUtil.composePic(backImage, headImage, outPutPath, leftX, leftY);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 圖片上加文字(文字水印)
* @param pressText 水印文字
* @param targetImg 目標圖片
* @param fontName 字體名稱
* @param fontStyle 字體樣式,如:粗體和斜體(Font.BOLD|Font.ITALIC)
* @param color 字體顏色
* @param fontSize 字體大小
* @param x 水印文字距離目標圖片左側的偏移量
* @param y 水印文字距離目標圖片上側的偏移量
* @param alpha 透明度(0.0 -- 1.0, 0.0為完全透明,1.0為完全不透明)
* @throws IOException
*/
public static BufferedImage pressText(String pressText, Image targetImg, String fontName, int fontStyle,
Color color, int fontSize, int x, int y, float alpha) throws IOException {
// 圖片寬高
int width = targetImg.getWidth(null);
int height = targetImg.getHeight(null);
//樣式
Font font = new Font(fontName, fontStyle, fontSize);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(targetImg, 0, 0, width, height, null);
g.setColor(color);// 顏色
g.setFont(font);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawString(pressText, x, y);
g.dispose();
return image;
}
/**
* 是否開啟alpha通道
*
* @param image
* @return
* @throws InterruptedException
*/
public static boolean hasAlpha(Image image)
throws InterruptedException {
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage) image;
return bimage.getColorModel().hasAlpha();
}
PixelGrabber pGrabber = new PixelGrabber(image, 0, 0, 1, 1, false);
pGrabber.grabPixels();
ColorModel colorModel = pGrabber.getColorModel();
return colorModel.hasAlpha();
}
/**
* 導入本地圖片到緩沖區
* @param imgPath 本地的圖片地址
* @return
* @throws IOException
*/
public static BufferedImage loadImageLocal(String imgPath)
throws IOException {
return ImageIO.read(new File(imgPath));
}
/**
* 導入網絡圖片到緩沖區
* @param imgUrl 網絡圖片url
* @return
* @throws IOException
*/
public static BufferedImage loadImageUrl(String imgUrl)
throws IOException {
URL url = new URL(imgUrl);
return ImageIO.read(url);
}
/**
* 16進制轉Color對象
* @param str
* @return
*/
public static Color String2Color(String str) {
int i = Integer.parseInt(str.substring(1), 16);
return new Color(i);
}
/**
* Color對象轉16進制
* @param color
* @return
*/
public static String Color2String(Color color) {
String R = Integer.toHexString(color.getRed());
R = R.length()<2?('0'+R):R;
String B = Integer.toHexString(color.getBlue());
B = B.length()<2?('0'+B):B;
String G = Integer.toHexString(color.getGreen());
G = G.length()<2?('0'+G):G;
return '#'+R+B+G;
}
/**
* 獲取字符長度,一個漢字作為 1 個字符, 一個英文字母作為 0.5 個字符
* @param text
* @return 字符長度,如:text="中國",返回 2
* text="test",返回 2
* text="中國ABC",返回 4
*/
public static int getLength(String text) {
int textLength = text.length();
int length = textLength;
for (int i = 0; i < textLength; i++) {
if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
length++;
}
}
return (length % 2 == 0) ? length / 2 : length / 2 + 1;
}
}
原始圖片:
效果圖片:
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态