java图片添加水印实例代码分享
本文为大家介绍了java图片添加水印实例代码,java实现水印还是非常方便的,水印可以是图片或者文字,具体内容如下
packagemichael.io.image; importjava.awt.AlphaComposite; importjava.awt.Graphics2D; importjava.awt.Image; importjava.awt.RenderingHints; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileOutputStream; importjava.io.InputStream; importjava.io.OutputStream; importjavax.imageio.ImageIO; importjavax.swing.ImageIcon; importcom.sun.image.codec.jpeg.JPEGCodec; importcom.sun.image.codec.jpeg.JPEGImageDecoder; importcom.sun.image.codec.jpeg.JPEGImageEncoder; /** *图片水印 *@bloghttp://sjsky.iteye.com *@authorMichael */ publicclassImageMarkLogoByIcon{ /** *@paramargs */ publicstaticvoidmain(String[]args){ StringsrcImgPath="d:/test/michael/myblog_01.png"; StringiconPath="d:/test/michael/blog_logo.png"; StringtargerPath="d:/test/michael/img_mark_icon.jpg"; StringtargerPath2="d:/test/michael/img_mark_icon_rotate.jpg"; //给图片添加水印 ImageMarkLogoByIcon.markImageByIcon(iconPath,srcImgPath,targerPath); //给图片添加水印,水印旋转-45 ImageMarkLogoByIcon.markImageByIcon(iconPath,srcImgPath,targerPath2, -45); } /** *给图片添加水印 *@paramiconPath水印图片路径 *@paramsrcImgPath源图片路径 *@paramtargerPath目标图片路径 */ publicstaticvoidmarkImageByIcon(StringiconPath,StringsrcImgPath, StringtargerPath){ markImageByIcon(iconPath,srcImgPath,targerPath,null); } /** *给图片添加水印、可设置水印图片旋转角度 *@paramiconPath水印图片路径 *@paramsrcImgPath源图片路径 *@paramtargerPath目标图片路径 *@paramdegree水印图片旋转角度 */ publicstaticvoidmarkImageByIcon(StringiconPath,StringsrcImgPath, StringtargerPath,Integerdegree){ OutputStreamos=null; try{ ImagesrcImg=ImageIO.read(newFile(srcImgPath)); BufferedImagebuffImg=newBufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),BufferedImage.TYPE_INT_RGB); //得到画笔对象 //Graphicsg=buffImg.getGraphics(); Graphics2Dg=buffImg.createGraphics(); //设置对线段的锯齿状边缘处理 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),srcImg .getHeight(null),Image.SCALE_SMOOTH),0,0,null); if(null!=degree){ //设置水印旋转 g.rotate(Math.toRadians(degree), (double)buffImg.getWidth()/2,(double)buffImg .getHeight()/2); } //水印图象的路径水印一般为gif或者png的,这样可设置透明度 ImageIconimgIcon=newImageIcon(iconPath); //得到Image对象。 Imageimg=imgIcon.getImage(); floatalpha=0.5f;//透明度 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); //表示水印图片的位置 g.drawImage(img,150,300,null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); g.dispose(); os=newFileOutputStream(targerPath); //生成图片 ImageIO.write(buffImg,"JPG",os); System.out.println("图片完成添加Icon印章。。。。。。"); }catch(Exceptione){ e.printStackTrace(); }finally{ try{ if(null!=os) os.close(); }catch(Exceptione){ e.printStackTrace(); } } } }
再为大家分享一例:
importjava.awt.Color; importjava.awt.Font; importjava.awt.Graphics; importjava.awt.Image; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.FileOutputStream; importjavax.imageio.ImageIO; importcom.sun.image.codec.jpeg.JPEGCodec; importcom.sun.image.codec.jpeg.JPEGImageEncoder; publicfinalclassImageUtils{ publicImageUtils(){ } /** *publicfinalstaticStringgetPressImgPath(){returnApplicationContext *.getRealPath("/template/data/util/shuiyin.gif");} */ /** *把图片印刷到图片上 * *@parampressImg-- *水印文件 *@paramtargetImg-- *目标文件 *@paramx *--x坐标 *@paramy *--y坐标 */ publicfinalstaticvoidpressImage(StringpressImg,StringtargetImg, intx,inty){ try{ //目标文件 File_file=newFile(targetImg); Imagesrc=ImageIO.read(_file); intwideth=src.getWidth(null); intheight=src.getHeight(null); BufferedImageimage=newBufferedImage(wideth,height, BufferedImage.TYPE_INT_RGB); Graphicsg=image.createGraphics(); g.drawImage(src,0,0,wideth,height,null); //水印文件 File_filebiao=newFile(pressImg); Imagesrc_biao=ImageIO.read(_filebiao); intwideth_biao=src_biao.getWidth(null); intheight_biao=src_biao.getHeight(null); g.drawImage(src_biao,(wideth-wideth_biao)/2, (height-height_biao)/2,wideth_biao,height_biao,null); //水印文件结束 g.dispose(); FileOutputStreamout=newFileOutputStream(targetImg); JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); }catch(Exceptione){ e.printStackTrace(); } } /** *打印文字水印图片 * *@parampressText *--文字 *@paramtargetImg-- *目标图片 *@paramfontName-- *字体名 *@paramfontStyle-- *字体样式 *@paramcolor-- *字体颜色 *@paramfontSize-- *字体大小 *@paramx-- *偏移量 *@paramy */ publicstaticvoidpressText(StringpressText,StringtargetImg, StringfontName,intfontStyle,intcolor,intfontSize,intx, inty){ try{ File_file=newFile(targetImg); Imagesrc=ImageIO.read(_file); intwideth=src.getWidth(null); intheight=src.getHeight(null); BufferedImageimage=newBufferedImage(wideth,height, BufferedImage.TYPE_INT_RGB); Graphicsg=image.createGraphics(); g.drawImage(src,0,0,wideth,height,null); g.setColor(Color.RED); g.setFont(newFont(fontName,fontStyle,fontSize)); g.drawString(pressText,wideth-fontSize-x,height-fontSize /2-y); g.dispose(); FileOutputStreamout=newFileOutputStream(targetImg); JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); }catch(Exceptione){ System.out.println(e); } } publicstaticvoidmain(String[]args){ pressImage("F:/logo.png","F:/123.jpg",0,0); } }
希望本文所述对大家用java语言实现图片添加水印有所帮助。