1. 程式人生 > >給圖片加圖片邊框 圖片邊框要求為PNG格式

給圖片加圖片邊框 圖片邊框要求為PNG格式

package com.product.utils;

import javax.imageio.ImageIO;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 * 圖片邊框類
 * 
 * @author 劍來V
 *
 */
public final class ImageBorderUtil {

	/**
	 * 
	 * @param url
	 *            需要加邊框的圖片URL地址
	 * @param pressImg
	 *            邊框圖片的存放路徑
	 * @param targetImg
	 *            圖片的路徑
	 * @param handleImg
	 *            新處理的圖片路徑
	 */

	public static void pressImage(String url, String pressImg, String targetImg, String handleImg) {
		try {
			int maxLength = 0;// 求出繪製的正方形長度
			int direction = 0;// 預設 0正方形 1橫向長方形 2縱向長方形
			// 目標檔案
			File _file = toFile(url, new File(targetImg));
			Image src = ImageIO.read(_file);
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			// 邊框檔案
			File _filebiao = new File(pressImg);
			Image src_biao = ImageIO.read(_filebiao);

			if (width == height) {
				maxLength = width;
			} else if (width - height > 0) {
				maxLength = width;
				direction = 1;
			} else {
				maxLength = height;
				direction = 2;
			}
			// 調整座標
			int x = 0, y = 0;
			switch (direction) {
			case 1:// 橫向長方形
				y = (maxLength - height) / 2;
				break;
			case 2:// 縱向長方形
				x = (maxLength - width) / 2;
				break;
			}
			BufferedImage image = new BufferedImage(maxLength, maxLength, BufferedImage.TYPE_INT_RGB);
			Graphics2D g = image.createGraphics();
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
			g.setBackground(Color.WHITE);// 設定白色背景填充
			g.clearRect(0, 0, maxLength, maxLength);// 重置畫布進行填充
			g.drawImage(src, 0 + x, 0 + y, width, height, null);// 底層畫布
			g.drawImage(src_biao, 0, 0, maxLength, maxLength, null);// 邊框PNG圖片覆蓋
			g.dispose();
			// 直接修改原始檔
			// FileOutputStream out = new FileOutputStream(targetImg);
			// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			// encoder.encode(image);
			// out.flush();
			// out.close();
			// 生成新的檔案
			File sf = new File(handleImg);
			ImageIO.write(image, "jpg", sf); // 儲存圖片
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 檔案儲存
	 *
	 * @param link
	 *            圖片連結
	 * @param file
	 *            存入的檔案地址
	 * @return 下載的檔案
	 */
	public static File toFile(String link, File file) {
		try {
			URL url = new URL(link);
			URLConnection uri = url.openConnection();
			// 獲取資料流
			InputStream ins = uri.getInputStream();
			OutputStream os = new FileOutputStream(file);
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.close();
			ins.close();
			return file;
		} catch (Exception e) {
			return null;
		}
	}

	public static void main(String[] args) {
		pressImage("http://imgsrc.baidu.com/imgad/pic/item/7af40ad162d9f2d3dd3f80d6a2ec8a136327ccd9.jpg", "D:\\imgin\\99-FS.png", "D:\\imgout\\test111111111.jpg", "D:/imgout/" + "test2018" + "." + "jpg");
		System.out.println("結束");
	}

}