用java代碼幫朋友P圖
引言
事件起因:當(dāng)時(shí)有個(gè)朋友發(fā)了張圖片給我,讓我給他P圖,說(shuō)是讓我給他把自己的微信付款碼給P上去,我覺得也挺有意思的,就給他P了,這一P就一發(fā)不可收拾了,天天都有人來(lái)找我P這圖。
想了下,能有什么問題難倒咱們程序員呢?這不就想出來(lái)了用代碼實(shí)現(xiàn)圖片合成嘛,哈哈哈~
先去微信那里生成微信收款碼(收付款 -> 二維碼收款 -> 保存收款碼),然后上傳,接著選擇你的設(shè)備,安卓手機(jī)選安卓,蘋果手機(jī)選蘋果,最后點(diǎn)擊開始合成即可。
準(zhǔn)備階段
原圖(來(lái)源于網(wǎng)絡(luò))
PS處理后的模板圖
待合成圖片
圖片合成
基本步驟
- 讀取模板圖和待合成圖,調(diào)整待合成圖的尺寸、旋轉(zhuǎn)角度、亮度、對(duì)比度;
- 創(chuàng)建一個(gè)與模板圖尺寸一致的空白圖像;
- 創(chuàng)建
Graphics2D
對(duì)象,通過Graphics2D
對(duì)象的drawImage()
方法將待合成圖和模板圖繪制進(jìn)空白圖像的指定位置(需要注意圖層順序,先繪制待合成圖,后繪制模板圖); - 輸出空白圖像(合成后的圖像)到本地。
代碼
圖片尺寸調(diào)整、旋轉(zhuǎn)使用到了 Thumbnailator
, 需添加 Maven
依賴
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
/** * 橫坐標(biāo) */ private static final int x = 457; /** * 縱坐標(biāo) */ private static final int y = 295; /** * 旋轉(zhuǎn)角度 */ private static final double angle = 16; /** * 縮放比例 */ private static final double scale = 0.18; /** * 圖片合成 * * @param imagePath 待合成的圖片路徑 * @param outPath 合成后的圖片輸出路徑 * @throws IOException */ private static void synthesis(String imagePath, String outPath) throws IOException { // 模板圖 BufferedImage template = ImageIO.read(new File("D:\\local\\images\\template.png")); // 待合成圖 BufferedImage image = ImageIO.read(new File(imagePath)); // 調(diào)整待合成圖的尺寸和旋轉(zhuǎn)角度 image = Thumbnails.of(image).scale(scale).rotate(angle).asBufferedImage(); // 合成后的圖 BufferedImage result = new BufferedImage(template.getWidth(), template.getHeight(), template.getType()); Graphics2D graphics2D = result.createGraphics(); // 先畫待合成圖,后畫模板圖,這樣就能將待合成圖放置在模板圖的下層 graphics2D.drawImage(image, x, y, null); graphics2D.drawImage(template,0,0, null); graphics2D.dispose(); ImageIO.write(result, "png", new File(outPath)); }
運(yùn)行代碼
public static void main(String[] args) throws IOException { synthesis("D:\\local\\images\\weixin_payment_code.png", "D:\\local\\images\\result.png"); }
調(diào)整圖片亮度、對(duì)比度
/** * 調(diào)整亮度、對(duì)比度 * * @param image */ private static void adjustBrightnessAndContrast(BufferedImage image) { int width = image.getWidth(); int height = image.getHeight(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color color = new Color(image.getRGB(x, y)); int red = calculateColor(color.getRed()); int green = calculateColor(color.getGreen()); int blue = calculateColor(color.getBlue()); color = new Color(red, green, blue); image.setRGB(x, y, color.getRGB()); } } } /** * 亮度,取值范圍[-1,1] */ private static final double BRIGHTNESS = 0; /** * 對(duì)比度,取值范圍[-1,1] */ private static final double CONTRAST = -0.5; /** * 計(jì)算亮度、對(duì)比度顏色值 * * @param color 原顏色值 * @return 返回計(jì)算后的顏色值 */ private static int calculateColor(int color) { color = (int)((color - 127.5 * (1 - BRIGHTNESS)) * Math.tan((45 + 44 * CONTRAST) / 180 * Math.PI) + 127.5 * (1 + BRIGHTNESS)); return getColor(color); } /** * 獲取范圍內(nèi)的顏色值,[0,255] * * @param color * @return */ private static int getColor(int color) { return color > 255 ? 255 : color < 0 ? 0 : color; }
在處理待合成圖片的尺寸和旋轉(zhuǎn)角度后調(diào)用 adjustBrightnessAndContrast()
方法調(diào)整亮度和對(duì)比度
... // 調(diào)整待合成圖的尺寸和旋轉(zhuǎn)角度 image = Thumbnails.of(image).scale(scale).rotate(angle).asBufferedImage(); // 調(diào)整待合成圖片的亮度、對(duì)比度 adjustBrightnessAndContrast(image); ...
重新運(yùn)行代碼
以上就是用java代碼幫朋友P圖的詳細(xì)內(nèi)容,更多關(guān)于java代碼P圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Java中字符串緩沖區(qū)StringBuffer類的使用
StringBuffer與String類似,只不過StringBuffer在進(jìn)行字符串處理時(shí)不生成新的對(duì)象,下面我們就來(lái)詳解Java中字符串緩沖區(qū)StringBuffer類的使用:2016-06-06在java中由類名和方法名字符串實(shí)現(xiàn)其調(diào)用方式
這篇文章主要介紹了在java中由類名和方法名字符串實(shí)現(xiàn)其調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-09-09idea2020.3測(cè)試評(píng)價(jià)及感受
idea2020.3版本這次變化最大的也就是 UI了完全拋棄了之前一直使用的模板更改成了新的樣式,感興趣的朋友快來(lái)下載體驗(yàn)下吧2020-10-10Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序
最近在學(xué)習(xí)Java,然后上個(gè)月迷上了九宮格數(shù)獨(dú),玩了幾天,覺得實(shí)在有趣,就想著能不能用編程來(lái)解決,于是就自己寫了個(gè),還真解決了。下面這篇文章就給大家主要介紹了Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序,需要的朋友可以參考借鑒。2017-01-01