java图片处理文字水印图片水印缩放补白

java图片处理文字水印图片水印缩放补白

ID:8966451

大小:41.00 KB

页数:4页

时间:2018-04-13

上传者:U-5734
java图片处理文字水印图片水印缩放补白_第1页
java图片处理文字水印图片水印缩放补白_第2页
java图片处理文字水印图片水印缩放补白_第3页
java图片处理文字水印图片水印缩放补白_第4页
资源描述:

《java图片处理文字水印图片水印缩放补白》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库

importjava.awt.AlphaComposite;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics2D;importjava.awt.Image;importjava.awt.geom.AffineTransform;importjava.awt.image.importjava.awt.AlphaComposite;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics2D;importjava.awt.Image;importjava.awt.geom.AffineTransform;importjava.awt.image.AffineTransformOp;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjavax.imageio.ImageIO;/***@authorEricXu**/publicfinalclassImageUtils{/***图片水印*@parampressImg水印图片*@paramtargetImg目标图片*@paramx修正值默认在中间*@paramy修正值默认在中间*@paramalpha透明度*/publicfinalstaticvoidpressImage(StringpressImg,StringtargetImg,intx,inty,floatalpha){try{Fileimg=newFile(targetImg);Imagesrc=ImageIO.read(img);intwideth=src.getWidth(null);intheight=src.getHeight(null);BufferedImageimage=newBufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB);Graphics2Dg=image.createGraphics();g.drawImage(src,0,0,wideth,height,null); //水印文件Imagesrc_biao=ImageIO.read(newFile(pressImg));intwideth_biao=src_biao.getWidth(null);intheight_biao=src_biao.getHeight(null);g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));g.drawImage(src_biao,(wideth-wideth_biao)/2,(height-height_biao)/2,wideth_biao,height_biao,null);//水印文件结束g.dispose();ImageIO.write((BufferedImage)image,"jpg",img);}catch(Exceptione){e.printStackTrace();}}/***文字水印*@parampressText水印文字*@paramtargetImg目标图片*@paramfontName字体名称*@paramfontStyle字体样式*@paramcolor字体颜色*@paramfontSize字体大小*@paramx修正值*@paramy修正值*@paramalpha透明度*/publicstaticvoidpressText(StringpressText,StringtargetImg,StringfontName,intfontStyle,Colorcolor,intfontSize,intx,inty,floatalpha){try{Fileimg=newFile(targetImg);Imagesrc=ImageIO.read(img);intwidth=src.getWidth(null);intheight=src.getHeight(null);BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);Graphics2Dg=image.createGraphics();g.drawImage(src,0,0,width,height,null);g.setColor(color);g.setFont(newFont(fontName,fontStyle,fontSize)); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));g.drawString(pressText,(width-(getLength(pressText)*fontSize))/2+x,(height-fontSize)/2+y);g.dispose();ImageIO.write((BufferedImage)image,"jpg",img);}catch(Exceptione){e.printStackTrace();}}/***缩放*@paramfilePath图片路径*@paramheight高度*@paramwidth宽度*@parambb比例不对时是否需要补白*/publicstaticvoidresize(StringfilePath,intheight,intwidth,booleanbb){try{doubleratio=0.0;//缩放比例Filef=newFile(filePath);BufferedImagebi=ImageIO.read(f);Imageitemp=bi.getScaledInstance(width,height,bi.SCALE_SMOOTH);//计算比例if((bi.getHeight()>height)||(bi.getWidth()>width)){if(bi.getHeight()>bi.getWidth()){ratio=(newInteger(height)).doubleValue()/bi.getHeight();}else{ratio=(newInteger(width)).doubleValue()/bi.getWidth();}AffineTransformOpop=newAffineTransformOp(AffineTransform.getScaleInstance(ratio,ratio),null);itemp=op.filter(bi,null);}if(bb){BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics2Dg=image.createGraphics();g.setColor(Color.white);g.fillRect(0,0,width,height);if(width==itemp.getWidth(null))g.drawImage(itemp,0,(height-itemp.getHeight(null))/2,itemp.getWidth(null),itemp.getHeight(null),Color.white,null);elseg.drawImage(itemp,(width-itemp.getWidth(null))/2,0,itemp.getWidth(null),itemp.getHeight(null),Color.white,null);g.dispose();itemp=image;}ImageIO.write((BufferedImage)itemp,"jpg",f);}catch(IOExceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args)throwsIOException{pressImage("G:\imgtest\sy.jpg","G:\imgtest\test1.jpg",0,0,0.5f);pressText("我是文字水印","G:\imgtest\test1.jpg","黑体",36,Color.white,80,0,0,0.3f);resize("G:\imgtest\test1.jpg",500,500,true);}publicstaticintgetLength(Stringtext){intlength=0;for(inti=0;i1){length+=2;}else{length+=1;}}returnlength/2;}}本篇文章来源于:开发学院http://edu.codepub.com原文链接:http://edu.codepub.com/2009/1116/17708.php

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。
关闭