java 圖片處理

來源:互聯網
上載者:User

圖片處理工具類,

包括:讀取本地圖片、網狀圖片,圖片合成(圖片浮水印),圖片上寫文字(文字浮水印),16進位和Color對象的互轉

package com.chat;import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;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;/** * 圖片處理工具類 *  * @author Daven * */public class ImgUtil {public static void main(String[] args) {ImgUtil.composePic("D:/back.png", "http://t3.qlogo.cn/mbloghead/d387538f629cfb53c2b0/100", "D:/output.png", 53, 161);}/** * 合成圖片(類似圖片浮水印) * @param backImage 背景圖片對象 * @param headImage 帳戶圖片對象 * @param outPutPath 輸出路徑(D:/img/out.png) * @param leftX 距離靶心圖表片左側的位移量 * @param leftY 距離靶心圖表片左側的位移量 * @throws InterruptedException * @throws IOException */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;     }}

原始圖片:

片:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.