用java代碼幫朋友P圖
引言
事件起因:當時有個朋友發(fā)了張圖片給我,讓我給他P圖,說是讓我給他把自己的微信付款碼給P上去,我覺得也挺有意思的,就給他P了,這一P就一發(fā)不可收拾了,天天都有人來找我P這圖。
想了下,能有什么問題難倒咱們程序員呢?這不就想出來了用代碼實現(xiàn)圖片合成嘛,哈哈哈~
先去微信那里生成微信收款碼(收付款 -> 二維碼收款 -> 保存收款碼),然后上傳,接著選擇你的設(shè)備,安卓手機選安卓,蘋果手機選蘋果,最后點擊開始合成即可。
準備階段
原圖(來源于網(wǎng)絡(luò))
PS處理后的模板圖
待合成圖片
圖片合成
基本步驟
- 讀取模板圖和待合成圖,調(diào)整待合成圖的尺寸、旋轉(zhuǎn)角度、亮度、對比度;
- 創(chuàng)建一個與模板圖尺寸一致的空白圖像;
- 創(chuàng)建
Graphics2D
對象,通過Graphics2D
對象的drawImage()
方法將待合成圖和模板圖繪制進空白圖像的指定位置(需要注意圖層順序,先繪制待合成圖,后繪制模板圖); - 輸出空白圖像(合成后的圖像)到本地。
代碼
圖片尺寸調(diào)整、旋轉(zhuǎn)使用到了 Thumbnailator
, 需添加 Maven
依賴
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
/** * 橫坐標 */ private static final int x = 457; /** * 縱坐標 */ 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)); }
運行代碼
public static void main(String[] args) throws IOException { synthesis("D:\\local\\images\\weixin_payment_code.png", "D:\\local\\images\\result.png"); }
調(diào)整圖片亮度、對比度
/** * 調(diào)整亮度、對比度 * * @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; /** * 對比度,取值范圍[-1,1] */ private static final double CONTRAST = -0.5; /** * 計算亮度、對比度顏色值 * * @param color 原顏色值 * @return 返回計算后的顏色值 */ 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)整亮度和對比度
... // 調(diào)整待合成圖的尺寸和旋轉(zhuǎn)角度 image = Thumbnails.of(image).scale(scale).rotate(angle).asBufferedImage(); // 調(diào)整待合成圖片的亮度、對比度 adjustBrightnessAndContrast(image); ...
重新運行代碼
以上就是用java代碼幫朋友P圖的詳細內(nèi)容,更多關(guān)于java代碼P圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Java中字符串緩沖區(qū)StringBuffer類的使用
StringBuffer與String類似,只不過StringBuffer在進行字符串處理時不生成新的對象,下面我們就來詳解Java中字符串緩沖區(qū)StringBuffer類的使用:2016-06-06在java中由類名和方法名字符串實現(xiàn)其調(diào)用方式
這篇文章主要介紹了在java中由類名和方法名字符串實現(xiàn)其調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09