fanth 发表于 2013-1-27 06:09:14

java给图片加水印,文字水印

RSS上看到的,收藏了。

原文链接:http://blog.csdn.net/gooogledev/archive/2007/05/16/1611254.aspx


public final class ImageUtils ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public ImageUtils() ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public final static String getPressImgPath()...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return ApplicationContext.getRealPath("/template/data/util/shuiyin.gif");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /** *//**
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif  * 把图片印刷到图片上
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif  * @param pressImg -- 水印文件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif  * @param targetImg  -- 目标文件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif  * @param x
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif  * @param y
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif  */
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public final static void pressImage(String pressImg, String targetImg, int x, int y) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        try ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            File _file = new File(targetImg);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Image src = ImageIO.read(_file);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int wideth = src.getWidth(null);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int height = src.getHeight(null);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            BufferedImage image = new BufferedImage(wideth, height,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    BufferedImage.TYPE_INT_RGB);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Graphics g = image.createGraphics();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            g.drawImage(src, 0, 0, wideth, height, null);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            // 水印文件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            File _filebiao = new File(pressImg);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Image src_biao = ImageIO.read(_filebiao);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int wideth_biao = src_biao.getWidth(null);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int height_biao = src_biao.getHeight(null);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            g.drawImage(src_biao, wideth - wideth_biao - x, height - height_biao -y, wideth_biao,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    height_biao, null);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            // /
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            g.dispose();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            FileOutputStream out = new FileOutputStream(targetImg);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            encoder.encode(image);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            out.close();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        } catch (Exception e) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            e.printStackTrace();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    /** *//**
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * 打印文字水印图片
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @param pressText --文字
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @param targetImg -- 目标图片
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @param fontName -- 字体名
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @param fontStyle -- 字体样式
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @param color -- 字体颜色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @param fontSize -- 字体大小
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @param x -- 偏移量
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @param y
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif     */
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static void pressText(String pressText, String targetImg, String fontName,int fontStyle, int color, int fontSize, int x, int y) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        try ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            File _file = new File(targetImg);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Image src = ImageIO.read(_file);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int wideth = src.getWidth(null);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int height = src.getHeight(null);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            BufferedImage image = new BufferedImage(wideth, height,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    BufferedImage.TYPE_INT_RGB);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Graphics g = image.createGraphics();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            g.drawImage(src, 0, 0, wideth, height, null);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            // String s="www.qhd.com.cn";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            g.setColor(Color.RED);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            g.setFont(new Font(fontName, fontStyle, fontSize));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif         
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            g.drawString(pressText, wideth - fontSize - x, height - fontSize/2 - y);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            g.dispose();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            FileOutputStream out = new FileOutputStream(targetImg);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            encoder.encode(image);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            out.close();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        } catch (Exception e) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            System.out.println(e);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static void main(String[] args) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        pressImage("C:/shuiyin/shuiyin.gif", "c:/shuiyin/DSC02342.JPG", 20 ,20);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: java给图片加水印,文字水印