溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用java代碼P圖

發(fā)布時間:2022-07-28 10:37:13 來源:億速云 閱讀:132 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了怎么用java代碼P圖的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇怎么用java代碼P圖文章都會有所收獲,下面我們一起來看看吧。

準備階段

原圖(來源于網(wǎng)絡(luò))

怎么用java代碼P圖

PS處理后的模板圖

怎么用java代碼P圖

待合成圖片

怎么用java代碼P圖

圖片合成

基本步驟

  • 讀取模板圖和待合成圖,調(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");
}

怎么用java代碼P圖

調(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圖

關(guān)于“怎么用java代碼P圖”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“怎么用java代碼P圖”知識都有一定的了解,大家如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI