package com.api;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 缩略图、图片水印图、文字水印图 
 * @author xingzhe
 *
 */
@SuppressWarnings("restriction")
public class ImageHelper {

        /**
         * 生成缩略图
         * 保存:ImageIO.write(BufferedImage, imgType[jpg/png/...], File);
         * 
         * @param source 原图片
         * @param width 缩略图宽
         * @param height 缩略图高
         * @param b 是否等比缩放
         * */
        public static BufferedImage Thumb(BufferedImage source, int width, int height, boolean b) {
                // targetW,targetH分别表示目标长和宽
                int type = source.getType();
                BufferedImage target = null;
                double sx = (double) width / source.getWidth();
                double sy = (double) height / source.getHeight();

                if (b) {
                    if (sx > sy) {
                        sx = sy;
                        width = (int) (sx * source.getWidth());
                    } else {
                        sy = sx;
                        height = (int) (sy * source.getHeight());
                    }
                }
                
                if (type == BufferedImage.TYPE_CUSTOM) {
                    ColorModel cm = source.getColorModel();
                    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
                    boolean alphaPremultiplied = cm.isAlphaPremultiplied();
                    target = new BufferedImage(cm, raster, alphaPremultiplied, null);
                } else
                    target = new BufferedImage(width, height, type);
                Graphics2D g = target.createGraphics();
                g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
                g.dispose();
                return target;
        }

        /**
         * 图片水印
         * 
         * @param imgPath 待处理图片
         * @param markPath 水印图片
         * @param x 水印位于图片左上角的 x 坐标值
         * @param y 水印位于图片左上角的 y 坐标值
         * @param alpha 水印透明度 0.1f ~ 1.0f
         * */
		public static void waterMark(String imgPath, String markPath, int x, int y, float alpha) {
                try {
                    // 加载待处理图片文件
                    Image img = ImageIO.read(new File(imgPath));
                    BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
                    Graphics2D g = image.createGraphics();
                    g.drawImage(img, 0, 0, null);

                    // 加载水印图片文件
                    Image src_biao = ImageIO.read(new File(markPath));
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
                    g.drawImage(src_biao, x, y, null);
                    g.dispose();

                    // 保存处理后的文件
                    FileOutputStream out = new FileOutputStream("C:/Users/xingzhe/Desktop/321.jpg");
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                    encoder.encode(image);
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }

        /**
         * 文字水印
         * 
         * @param imgPath 待处理图片
         * @param text 水印文字
         * @param font 水印字体信息
         * @param color 水印字体颜色
         * @param x 水印位于图片左上角的 x 坐标值
         * @param y 水印位于图片左上角的 y 坐标值
         * @param alpha 水印透明度 0.1f ~ 1.0f
         */

		public static void textMark(String imgPath, String text, Font font, Color color, int x, int y, float alpha) {
                try {
                    Font Dfont = (font == null) ? new Font("宋体", 20, 13) : font;
                    Image img = ImageIO.read(new File(imgPath));

                    BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
                    Graphics2D g = image.createGraphics();

                    g.drawImage(img, 0, 0, null);
                    g.setColor(color);
                    g.setFont(Dfont);
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
                    g.drawString(text, x, y);
                    g.dispose();
                    
                    FileOutputStream out = new FileOutputStream("C:/Users/xingzhe/Desktop/421.jpg");
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                    encoder.encode(image);
                    out.close();
                } catch (Exception e) {
                    System.out.println(e);
                }
        }
        
        @SuppressWarnings("static-access")
		public static void main(String[] args) throws IOException {
        	new ImageHelper().waterMark("C:/Users/xingzhe/Desktop/123.jpg", "C:/Users/xingzhe/Desktop/213.jpg", 100, 300, 0.5f);
        	
        	Font titleFont = new Font("宋体", Font.BOLD, 30);
			new ImageHelper().textMark("C:/Users/xingzhe/Desktop/123.jpg", "春来桃花开",titleFont, Color.red, 100, 300, 1.0f);
		}
}
最近下载更多
蛇头凌志  LV8 2021年12月18日
zyyyyyyQAQ  LV1 2021年11月9日
13043860zj  LV16 2020年3月6日
pllpll  LV9 2019年11月23日
dagf113225  LV68 2019年1月9日
duduyan  LV11 2018年11月2日
soft5200  LV30 2018年2月27日
wgc_jy  LV21 2017年12月29日
909074489  LV34 2017年10月31日
1033755143  LV17 2017年3月24日
最近浏览更多
java小书童  LV17 1月29日
DongYingdie  LV2 2023年12月21日
xingxing1234  LV10 2023年3月22日
ls2008  LV15 2022年6月22日
crosa_Don  LV18 2022年4月1日
ming_123_9715  LV23 2022年1月19日
蛇头凌志  LV8 2021年12月18日
zyyyyyyQAQ  LV1 2021年11月9日
miaoshi  LV16 2021年8月31日
是一个鸽子啊  LV17 2021年8月10日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友