日期:2014-05-17 浏览次数:20850 次
package com.bean.tuil; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; import javax.imageio.ImageIO; /** * 对图片的操作 * * @author yangyz * */ public class ImageControl { // 添加字体的属性值 private Font font = new Font("", Font.PLAIN, 12); private Graphics2D g = null; private int fontsize = 0; private int x = 0; private int y = 0; private String destDir = "F:\\"; public void setDestDir(String path) { this.destDir = path; // 目录不存在 File file = new File(path); if (!file.exists()) { file.mkdir(); } } /** * 导入本地图片到缓冲中 * * @param imgPath * @return */ public BufferedImage loadImageLocal(String imgPath) { try { return ImageIO.read(new File(imgPath)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } /** * 读取网络中的图片到缓冲 * * @param imgPath * @return */ public BufferedImage loadImageUrl(String imgPath) { URL url; try { url = new URL(imgPath); return ImageIO.read(url); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } /** * 生成新图片到本地 * * @param newImage * 新名字 * @param img * 缓冲 * @param type * 类别 */ public void writeImageLocal(BufferedImage img, String type) { if (img != null) { File outputFile = new File(this.destDir + new Date().getTime() + "." + type); try { ImageIO.write(img, type, outputFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 设置文字字体,大小 * * @param fontStyle * @param fontSize */ public void setFont(String fontStyle, int fontSize) { this.fontsize = fontSize; this.font = new Font(fontStyle, Font.PLAIN, fontSize); } /** * 单行文本 * * @param img * @param content * @param x * @param y * @return */ public BufferedImage modifyImage(BufferedImage img, Object content) { try { int w = img.getWidth(); int h = img.getHeight(); g = img.createGraphics(); g.setColor(Color.RED); if (