- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- Java基礎之簡(jiǎn)單的圖片處理
先使用一個(gè)模板圖片,在圖片上添加圖片或者文字都可以。
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.18</version> <optional>true</optional> </dependency>
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.awt.*; /** * 坐標數據 * @author tyg * @date 2021-04-23 14:33 */ @Data @NoArgsConstructor @AllArgsConstructor public class PositionPO { /** 顯示的數據 */ private Object data; /** X軸坐標 */ private float x; /** Y軸坐標 */ private float y; /** 寬度 */ private float w; /** 高度 */ private float h; /** 字體 */ private Font font; public PositionPO(Object data, float x, float y, float w, float h) { this.data = data; this.x = x; this.y = y; this.w = w; this.h = h; } public PositionPO(Object data, float x, float y) { this.data = data; this.x = x; this.y = y; } public PositionPO(Object data, float x, float y, Font font) { this.data = data; this.x = x; this.y = y; this.font = font; } public PositionPO(float x, float y, float w, float h) { this.x = x; this.y = y; this.w = w; this.h = h; } }
import com.yt.distributor.po.pdf.PositionPO; import lombok.Data; import java.util.List; /** * 邀請海報 * @author tyg * @date 2021-04-24 14:52 */ @Data public class ImageHandlePO { /** 文字 */ private List<PositionPO> textList; /** 圖片 */ private List<PositionPO> imageList; public ImageHandlePO(List<PositionPO> textList, List<PositionPO> imageList) { this.textList = textList; this.imageList = imageList; } }
package com.yt.distributor.constant; import org.springframework.core.io.ClassPathResource; import java.awt.*; import java.io.File; import java.io.IOException; /** * 圖片常量 * @author tyg * @date 2021-04-24 16:59 */ public class ImageConstant { /** 透明度 */ public static final float PELLUCIDITY = 1.0F; /** 字體 */ public static final Font FONT = new Font("微軟雅黑", Font.BOLD, 18); /** 邀請海報模板圖片源文件 */ public static File POSTER_SOURCE_FILE; /** 圖片默認格式 */ public static final String FORMAT = "png"; static{ try { ClassPathResource resource = new ClassPathResource("conf/poster.jpg"); POSTER_SOURCE_FILE = resource.getFile(); } catch (IOException e) { e.printStackTrace(); } } }
import com.yt.distributor.constant.ImageConstant; import com.yt.distributor.po.img.ImageHandlePO; import com.yt.distributor.po.pdf.PositionPO; import lombok.extern.log4j.Log4j2; import net.dreamlu.mica.core.utils.$; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.URL; import java.util.ArrayList; import java.util.List; /** * 圖像合成處理 * 注:圖像處理的原點(diǎn)坐標在:左上角,距離為像素 * @author tyg * @date 2021-04-24 17:45 */ @Log4j2 public class PictureSynthesis { /** 原模板圖片文件 */ public static final Object FLAG = true; /** 原模板圖片文件 */ public static File sourceFile; public static void main(String[] args) throws IOException { // 生成二維碼 BufferedImage image = QrCodeGenerator.generateQrCode("http://www.baiud.com/index.html?id=13", 192, 192); // 圖片 List<PositionPO> imageList = new ArrayList<>(); imageList.add(new PositionPO(ImageIO.read(new URL("https://thirdwx.qlogo.cn/mmopen/vi_32/AtTHbmrMict69vB7ocDMbstibgvwxpK51bOoNkQiaemrImnicUK2L9OoF1JibHiceLwY53ibiaicJQibuEwLNFicJiaYcQHRiaw/132")), 120F, 1688F, 192F, 192F)); imageList.add(new PositionPO(image, 785F, 1632F, 192F , 192F)); // 文字 Font font = new Font("微軟雅黑", Font.PLAIN, 30); List<PositionPO> textList = new ArrayList<>(); textList.add(new PositionPO("顏魔子辰", 120F, 1660F, font)); textList.add(new PositionPO("顏魔子辰邀請您", 336F, 1758F, font)); textList.add(new PositionPO("加入某某小店。", 336F, 1796F, font)); textList.add(new PositionPO("長(cháng)按可識別二維碼", 760F, 1880F, font)); String sourcePath = "C:\\Users\\Administrator\\Desktop\\poster.jpg"; String savePath = "C:\\Users\\Administrator\\Desktop\\poster-handle.jpg"; // 輸出水印圖片 handleImage(new ImageHandlePO(textList, imageList), new File(sourcePath), savePath); } /** * 圖片處理(返回輸入流) * @param po 處理的數據 * @author tyg * @date 2021-04-14 15:45 * @return InputStream */ public static InputStream handleImage(ImageHandlePO po, File sourceFile) throws IOException { synchronized (FLAG) { PictureSynthesis.sourceFile = sourceFile; //圖片處理,導出數據 BufferedImage image = watermark(po); return getInputStream(image); } } /** * 圖片處理(輸出到文件中) * @param po 處理的數據 * @param saveFilePath 保存的路徑 * @author tyg * @date 2017年9月6日下午12:53:11 */ public static void handleImage(ImageHandlePO po, File sourceFile, String saveFilePath) throws IOException { synchronized (FLAG) { PictureSynthesis.sourceFile = sourceFile; // 構建疊加層 BufferedImage buffImg = watermark(po); // 輸出水印圖片 generateWaterFile(buffImg, saveFilePath); } } /** * 構建疊加層 * 圖像處理的原點(diǎn)坐標在:左上角 * @param po 處理的數據 * @throws IOException io異常 * @return BufferedImage 生成水印并返回java.awt.image.BufferedImage */ private static BufferedImage watermark(ImageHandlePO po) throws IOException { // 獲取底圖 BufferedImage buffImg = ImageIO.read(sourceFile); // 創(chuàng )建Graphics2D對象,用在底圖對象上繪圖 Graphics2D g2d = buffImg.createGraphics(); // 處理文字 if ($.isNotEmpty(po.getTextList())){ for (PositionPO pp : po.getTextList()){ g2d.setColor(Color.black); g2d.setFont( pp.getFont() == null ? ImageConstant.FONT : pp.getFont()); g2d.drawString(pp.getData().toString(), pp.getX(), pp.getY()); } } // 處理圖片 if ($.isNotEmpty(po.getImageList())){ for (PositionPO pp : po.getImageList()){ BufferedImage image = (BufferedImage) pp.getData(); // 獲取層圖的寬度 int width = pp.getW() <= 0 ? image.getWidth() : (int) pp.getW(); // 獲取層圖的高度 int height = pp.getH() <= 0 ? image.getHeight() : (int) pp.getH(); // 在圖形和圖像中實(shí)現混合和透明效果 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ImageConstant.PELLUCIDITY)); // 繪制 g2d.drawImage(image, (int)pp.getX(), (int)pp.getY(), width, height, null); } } // 釋放圖形上下文使用的系統資源 g2d.dispose(); return buffImg; } /** * 輸出水印圖片 * @param buffImg 圖像加水印之后的BufferedImage對象 * @param savePath 圖像加水印之后的保存路徑 * @author tyg * @date 2021-04-24 16:19 */ private static void generateWaterFile(BufferedImage buffImg, String savePath) { int temp = savePath.lastIndexOf(".") + 1; try { ImageIO.write(buffImg, savePath.substring(temp), new File(savePath)); } catch (IOException e1) { e1.printStackTrace(); } } /** * 獲取系統所支持的字體 * @author tyg * @date 2021-04-24 16:19 */ private static void getFonts(){ String[] fontNames=GraphicsEnvironment.getLocalGraphicsEnvironment(). getAvailableFontFamilyNames(); for(String fontName:fontNames){ System.out.println(fontName); } } /** * 獲取圖片輸入流 * @param image 圖片 * @author tyg * @date 2021-04-14 17:14 * @return java.io.InputStream */ public static InputStream getInputStream(BufferedImage image){ ByteArrayOutputStream os = new ByteArrayOutputStream(); try { ImageIO.write(image, ImageConstant.FORMAT, os); return new ByteArrayInputStream(os.toByteArray()); } catch (IOException e) { log.error("提示:",e); } return null; } }
以上的數據都是按圖片的1080*1920像素來(lái)設定的,下面紅框部分是動(dòng)態(tài)生成的。
到此這篇關(guān)于Java基礎之簡(jiǎn)單的圖片處理的文章就介紹到這了,更多相關(guān)Java圖片處理內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系站長(cháng)郵箱:ts@56dr.com進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2