空间站

北极心空

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks
jsp 上传图片并生成缩位图或者加水印

// 添加水印,filePath 源图片路径, watermark 水印图片路径
public   static   boolean  createMark(String filePath,String watermark)  {
ImageIcon imgIcon
= new  ImageIcon(filePath);
Image theImg 
= imgIcon.getImage();
ImageIcon waterIcon
= new  ImageIcon(watermark);
Image waterImg 
= waterIcon.getImage();
int  width = theImg.getWidth( null );
int  height =  theImg.getHeight( null );
BufferedImage bimage 
=   new  BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); 
Graphics2D g
= bimage.creatGraphics( );
g.setColor(Color.red);
g.setBackground(Color.white);
g.drawImage(theImg, 
0 0 null  );
g.drawImage(waterImg, 
100 100 null  );
g.drawString(
" 12233 " , 10 , 10 );  // 添加文字
g.dispose();
try {
FileOutputStream out
= new  FileOutputStream(filePath);
JPEGImageEncoder encoder 
= JPEGCodec.createJPEGEncoder(out); 
JPEGEncodeParam param 
=  encoder.getDefaultJPEGEncodeParam(bimage); 
param.setQuality(50f, 
true ); 
encoder.encode(bimage, param); 
out.close();
}
catch (Exception e) return   false ; }
return   true ;
}

JAVA给图片上添加水印文字

import  java.awt. * ;
import  java.awt.image. * ;
import  java.io. * ;
import  javax.swing. * ;
import  com.sun.image.codec.jpeg. * ;
import  java.text.AttributedString;
import  java.awt.font.TextAttribute;
import  java.text.AttributedCharacterIterator;


public   class  WaterMark  {
   
/**   */ /**
      * 给图片添加水印
     * 
@param  filePath 需要添加水印的图片的路径
     * 
@param  markContent 水印的文字
     * 
@param  markContentColor 水印文字的颜色
     * 
@param  qualNum 图片质量
     * 
@param  fontType 字体
     * 
@param  fontsize 字体大小
     * 
@return
     * 
@author  zhongweihai newwei2001@yahoo.com.cn
     
*/

    
public   boolean  createMark(String filePath,String markContent,Color markContentColor, float  qualNum,
                              String fontType,
int  fontSize)
    
{
        ImageIcon imgIcon
= new  ImageIcon(filePath);
        Image theImg 
= imgIcon.getImage();
        
int  width = theImg.getWidth( null );
        
int  height =  theImg.getHeight( null );
        BufferedImage bimage 
=   new  BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g
= bimage.createGraphics();
        g.setColor(markContentColor);
        g.setBackground(Color.white);
        g.drawImage(theImg, 
0 0 null  );
        AttributedString ats 
=   new  AttributedString(markContent);
        Font f 
=   new  Font(fontType,Font.BOLD, fontSize);

        ats.addAttribute(TextAttribute.FONT, f, 
0 ,markContent.length() );
        AttributedCharacterIterator iter 
=  ats.getIterator();

        g.drawString(iter,width
/ 5 ,height / 5 );  // 添加水印的文字和设置水印文字出现的内容
        g.dispose();

        
try {
        FileOutputStream out
= new  FileOutputStream(filePath);
        JPEGImageEncoder encoder 
= JPEGCodec.createJPEGEncoder(out);
        JPEGEncodeParam param 
=  encoder.getDefaultJPEGEncodeParam(bimage);
        param.setQuality(qualNum, 
true );
        encoder.encode(bimage, param);
        out.close();
        }
catch (Exception e)
        
return   false ; }
        
return   true ;
    }


    
public   static   void  main(String[] args)
    
{
     WaterMark wm 
=   new  WaterMark();
     wm.createMark(
" c:\year2-11.jpg " , " 此图片来自煞笔网 " ,Color.red,70f, " 黑体 " , 23 );
     }

}
生成缩小jpg图片程序

import  java.awt.image.BufferedImage;
import  java.io.File;
import  javax.swing.Icon;
import  javax.swing.ImageIcon;
import  javax.imageio.ImageIO;
import  java.awt.image.AffineTransformOp;
import  java.awt.geom.AffineTransform;
import  java.awt.Image;

public   class  ZoomPicture {
  
public   static   void  main(String arg[]) {
    
    String filePath 
=   " g:/图片.jpg " ;   //  图片的位置
    
    
int  height = 50 ;
    
int  width = 150 ;
    Icon icon 
=   null ;
    
try {
       icon 
=  getFixedBoundIcon(filePath,height,width);
       }
catch (Exception e) {
        System.out.println(
" exception :  "   +  e);
        }

    System.out.println(
"  ###  "   +  icon);   // 生成新图片的位置;
  }

  
     
public   static  Icon getFixedBoundIcon(String filePath,  int  height,  int  width) 
        
throws  Exception {
        
double  Ratio = 0.0
       
// 缩放比例 
   File F  =   new  File(filePath); 
        
if  ( ! F.isFile())  throw   new  Exception
           (F
+ "  is not image file error in getFixedBoundIcon! " ); 
        Icon ret 
=   new  ImageIcon(filePath);
        BufferedImage Bi 
=  ImageIO.read(F); 
        
if  ((Bi.getHeight() > height)  ||  (Bi.getWidth() > width))
    
if  (Bi.getHeight() > Bi.getWidth())
     Ratio 
=  ( new  Integer(height)).doubleValue()  / Bi.getHeight(); 
    }

    
else   {
      Ratio 
=  ( new  Integer(width)).doubleValue() / Bi.getWidth(); 
    }

    
int  lastLength  =  filePath.lastIndexOf( " . " );
    String subFilePath 
=  filePath.substring( 0 ,lastLength);
    String fileType 
=  filePath.substring(lastLength);
    File zoomFile 
=   new  File(subFilePath + " _ " + height  + " _ " + width + fileType);
    Image Itemp 
=  Bi.getScaledInstance (width,height,Bi.SCALE_SMOOTH);
    AffineTransformOp op 
=   new  AffineTransformOp
     (AffineTransform.getScaleInstance(Ratio, Ratio), 
null );
    Itemp 
=  op.filter(Bi,  null ); 
    
try   {
      ImageIO.write((BufferedImage)Itemp, 
" jpg " , zoomFile); 
      ret 
=   new  ImageIcon(zoomFile.getPath()); 
    }
catch  (Exception ex)  {
     System.out.println(
" ######## here error :  "   +  ex);
    }
 
   }

    
return  ret;
 }

}



posted on 2006-11-24 15:34 芦苇 阅读(7406) 评论(5)  编辑  收藏 所属分类: JAVA

Feedback

# re: jsp 上传图片并生成缩位图或者加水印 ,给图片添加水印,生成缩小图片程序 等 2006-11-24 15:34 芦苇
Aspjpeg添加水印完整方法
Aspjpeg添加水印完整方法
用ASPJPEG组件制作图片的缩略图和加水印


ASPJPEG是Persits出品的共享软件,试用期为30天,您可以在这里下载:http://www.persits.com/aspjpeg.exe。最新版本号是1.3
ASPJPEG是一款功能相当强大的图象处理组件,用它可以轻松地做出图片的缩略图和为图片加上水印功能。下面简单介绍一下使用方法:
您先要执行下载得到的exe文件,安装该组件
1、为图片制作缩略图

<% \\\' 建立实例
Dim Jpeg,Path
Set Jpeg = Server.CreateObject("Persits.Jpeg")
\\\' 图片所在位置
Path = Server.MapPath("images") & "\\\\clock.jpg"


\\\' 打开
Jpeg.Open Path

\\\' 设置缩略图大小(这里比例设定为50%)
Jpeg.Width = Jpeg.OriginalWidth / 2
Jpeg.Height = Jpeg.OriginalHeight / 2

\\\' 保存缩略图到指定文件夹下
Jpeg.Save Server.MapPath("images") & "\\\\clock_small.jpg"

\\\' 注销实例
Set Jpeg = Nothing
%>

<IMG SRC="images/clock.jpg"><P>
<IMG SRC="images/clock_small.jpg">


2、为图片加入水印功能
<%
Dim Jpeg
\\\' 建立实例
Set Jpeg = Server.CreateObject("Persits.Jpeg")
\\\' 打开目标图片
Jpeg.Open Server.MapPath("images/dodge_viper.jpg")

\\\' 添加文字水印
Jpeg.Canvas.Font.Color = &HFF0000\\\' 红色
Jpeg.Canvas.Font.Family = "宋体"
Jpeg.Canvas.Font.Bold = True
Jpeg.Canvas.Print 10, 10, "Copyright (c) Cnmaya.org"

\\\' 保存文件
Jpeg.Save Server.MapPath("images/dodge_viper_framed.jpg")

\\\' 注销对象
Set Jpeg = Nothing
%>


上次做过图片水印,效果很差,没办法把图片弄成背景透明的,用背景透明gif会自动填充成白色。用去掉某种颜色的功能,图片颜色总是不纯,出来的效果斑斑点点。今天灵机一动,把这两个结合起来,用背景透明的gif,并且抽取水印底色,果然成功了!

ogvbox.Canvas.Pen.Color = &H000000 \\\'// 边框的颜色
ogvbox.Canvas.Pen.Width = 1 \\\'// 边框的粗细
ogvbox.Canvas.Brush.Solid = False \\\'// 图片边框内是否填充颜色
ogvbox.DrawImage ogvbox.Width-210, ogvbox.Height-74, Logobox ,0.3,&HFFFFFF \\\'// 加入图片的位置坐标(添加水印图片),我用图片大小减去水印大小,把水印加在右下角。参数顺序为:水平坐标,垂直坐标,水印图片地址,水银透明度,抽取颜色(&H表示16进制)
ogvbox.Canvas.Bar 0, 0, ogvbox.Width, ogvbox.Height \\\'// 图片边框线的位置坐标和大小
ogvbox.Save Server.MapPath(imagename) \\\'// 生成文件
\'//------Pollener.com AspJpeg组件的预览和水印生成------开始------
\'创建预览图片:call CreateView(原始文件的路径,预览文件名及路径)
Sub CreateView(imagename,tempFilename)
\'定义变量。
Dim PreviewImageFolderName
Dim ogvbox,objFont
Dim Logobox,LogoPath
LogoPath = Server.MapPath("images") & "\\shuiyin.gif" \'//加入图片所在路径及文件名(我的是论坛\\images\\shuiyin.gif)。

Select Case upload_ViewType
Case 0
\'---------------------CreatePreviewImage---------------
set ogvbox = Server.CreateObject("CreatePreviewImage.cGvbox")
ogvbox.SetSavePreviewImagePath=Server.MapPath(tempFilename) \'预览图存放路径。
ogvbox.SetPreviewImageSize =SetPreviewImageSize \'预览图宽度。
ogvbox.SetImageFile = trim(Server.MapPath(imagename)) \'imagename原始文件的物理路径。
\'创建预览图的文件。
If ogvbox.DoImageProcess=false Then
Response.write "生成预览图错误:"& ogvbox.GetErrString
End If
Case 1
\'---------------------AspJpegV1.2---------------
Set Logobox = Server.CreateObject("Persits.Jpeg")
\'//建议不要图片和文字水印同时使用,本代码为使用图片水印。
Logobox.Open LogoPath \'//读取添加的图片。

\'//重新设置图片的大小。
Logobox.Width = 186 \'//用做水印的图片的宽度值(像素)。
Logobox.Height = 52 \'//用做水印的图片的高度值(像素)。
\'//添加水印。
Set ogvbox = Server.CreateObject("Persits.Jpeg")
\'//读取要处理的原文件。
ogvbox.Open Trim(Server.MapPath(imagename))
If ogvbox.OriginalWidth<Cint(ImageWidth) or ogvbox.Originalheight<Cint(ImageHeight) Then
F_Viewname=""
Set ogvbox = Nothing
Exit Sub
Else
IF ImageMode<>"" and FileExt<>"gif" Then \'//如果将这行改为IF ImageMode<>"" Then则可给上传的GIF图片也加上水印,但是那些动画的GIF在加了水印以后就只剩第一桢了,根据你的需求酌情处理吧。

\'//关于修改字体及文字颜色的。
\'//ogvbox.Canvas.Font.Color = &H0000FF \'//水印文字的颜色,&H后面输入色彩值。
\'//ogvbox.Canvas.Font.Size = 18 \'//水印文字的大小。
\'//ogvbox.Canvas.Font.Family = "Arial" \'//水印文字的字体名称。
\'//ogvbox.Canvas.Font.ShadowColor = &H000000 \'//水印文字的阴影色彩。
\'//ogvbox.Canvas.Font.ShadowXoffset = 1 \'//水印文字阴影向右偏移的像素值,输入负值则向左偏移。
\'//ogvbox.Canvas.Font.ShadowYoffset = 1 \'//水印文字阴影向下偏移的像素值,输入负值则向右偏移。
\'//ogvbox.Canvas.Font.Quality = 3 \'//水印文字的清晰度,从0~4,变换不是很大,建议用2或3。
\'//ogvbox.Canvas.Font.Bold = True \'//水印文字是否为粗体,True=粗体 False=正常。

\'ogvbox.Canvas.Print 10, 10, ImageMode \'//水印文字的起始坐标(像素)。
ogvbox.Canvas.Pen.Color = &H000000 \'//增加水印后图片的边框色彩。
ogvbox.Canvas.Pen.Width = 1 \'//增加水印后图片的边框宽度。
ogvbox.Canvas.Brush.Solid = False \'//边框内是否填充颜色,你可以试试看值为True时的效果^o^
ogvbox.DrawImage ogvbox.width-186, ogvbox.height-52, Logobox, 0.5 \'//水印图片的起始坐标,我这里ogvbox.width-186, ogvbox.height-52,表示图片在右下角,因为我的图片宽是186,高是52,所以这样写,你可以根据自己的图片进行调整。0.5是透明度,我这里是半透明,1表示不透明,你也可以试试看0.7或者0.8的效果。
ogvbox.Canvas.Bar 0, 0, ogvbox.Width, ogvbox.Height \'//水印可用的范围。我这里表示左上角至右下角,即整张图片的任意为止都可加水印。
ogvbox.Save Server.MapPath(imagename) \'//根据以上参数生成增加水印后的图片文件。
End If
ogvbox.Width = ImageWidth
ogvbox.height = ImageHeight
\'ogvbox.height = ogvbox.Originalheight*ImageWidth\\ogvbox.OriginalWidth
ogvbox.Sharpen 1, 120
ogvbox.Save Server.MapPath(tempFilename) \'//生成增加水印后的图片的预览图片。
End If
Set Logobox=Nothing
\'//------Pollener.com AspJpeg组件的预览和水印生成------结束------

  回复  更多评论
  

# re: jsp 上传图片并生成缩位图或者加水印 ,给图片添加水印,生成缩小图片程序 等 2009-02-24 14:11 12
谢谢!  回复  更多评论
  

# re: jsp 上传图片并生成缩位图或者加水印 ,给图片添加水印,生成缩小图片程序 等 2009-05-21 22:21 wer
谢谢

-----------------------------------
http://www.wjxj.com  回复  更多评论
  

# re: jsp 上传图片并生成缩位图或者加水印 ,给图片添加水印,生成缩小图片程序 等 2011-03-08 23:51 大赛
谢谢  回复  更多评论
  

# re: jsp 上传图片并生成缩位图或者加水印 ,给图片添加水印,生成缩小图片程序 等[未登录] 2011-12-21 09:38 zsd
阿发  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: