¶ÁÊéÈË

java ͼƬˮӡ(ͼƬˮӡ¸úÎÄ×Öˮӡ)

·¢²¼Ê±¼ä£º 2013-11-08 17:52:35 ×÷Õߣº rapoo

java ͼƬˮӡ(ͼƬˮӡºÍÎÄ×Öˮӡ)

package com.text.util;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.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import javax.imageio.ImageIO;import javax.swing.ImageIcon;/** * ͼƬˮӡ´¦Àí */public class ImageMarkUtil {/** * @param args */public static void main(String[] args) {String srcImgPath = "d:/2.jpg";String iconPath = "d:/3.png";String targerPath = "d:/4.jpg";String targerPath3 = "d:/5.jpg";String logoText = "[ͼƬˮӡ http://563525042.iteye.com]";// ¸øÍ¼Æ¬Ìí¼ÓͼƬˮӡImageMarkUtil.markImageByIcon(iconPath, srcImgPath, targerPath,0,0.5f, 50, 80);// ¸øÍ¼Æ¬Ìí¼ÓÎÄ×ÖˮӡImageMarkUtil.markByText(logoText, srcImgPath, targerPath3,0, 0.5f,60, 90,"0",20);}    /** * ¸øÍ¼Æ¬Ìí¼Óˮӡ¡¢¿ÉÉèÖÃˮӡͼƬÐýת½Ç¶È *  * @param iconPath *            ˮӡͼƬ·¾¶ * @param srcImgPath *            ԴͼƬ·¾¶ * @param targerPath *            Ä¿±êͼƬ·¾¶ * @param degree *            ˮӡͼƬÐýת½Ç¶È * @param transparency *            ˮӡ͸Ã÷¶È Èç0.5f * @param sWidth *            ˮӡͼƬÀëԭͼ×ó±ß¿ò¾àÀë°Ù·Ö±È * @param sHeight ˮӡͼƬÀëԭͼÉϱ߿ò¾àÀë°Ù·Ö±È */public static  void markImageByIcon(String iconPath, String srcImgPath,String targerPath, Integer degree, float transparency,Integer sWidth, Integer sHeight) {OutputStream os = null;try {Image srcImg = ImageIO.read(new File(srcImgPath));BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);// µÃµ½»­±Ê¶ÔÏóGraphics2D g = 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µÄ£¬ÕâÑù¿ÉÉèÖÃ͸Ã÷¶ÈImageIcon imgIcon = new ImageIcon(iconPath);// µÃµ½Image¶ÔÏó¡£Image img = imgIcon.getImage();float alpha = transparency; // ͸Ã÷¶Èg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));ImageIcon imgtarget = new ImageIcon(srcImgPath);Image theImg = imgtarget.getImage();int  width = theImg.getWidth(null);int height = theImg.getHeight(null);// ±íʾˮӡͼƬµÄλÖÃg.drawImage(img,(int)(width*((float)sWidth/(float)100)), (int)(height*(float)sHeight/(float)100), null);g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));g.dispose();os = new FileOutputStream(targerPath);// Éú³ÉͼƬImageIO.write(buffImg, "JPG", os);} catch (Exception e) {e.printStackTrace();} finally {try {if (null != os)os.close();} catch (Exception e) {e.printStackTrace();}}}/** * ¸øÍ¼Æ¬Ìí¼ÓÎÄ×Öˮӡ¡¢¿ÉÉèÖÃˮӡµÄÐýת½Ç¶È *  * @param logoText * @param srcImgPath * @param targerPath * @param degree * @param transparency *            ˮӡ͸Ã÷¶È Èç0.5f * @param sWidth *            ˮӡͼƬÀëԭͼ×ó±ß¿ò¾àÀë°Ù·Ö±È * @param sHeight *           ˮӡͼƬÀëԭͼÉϱ߿ò¾àÀë°Ù·Ö±È *@param sfontbold ×ÖÌåÊÇ·ñ¼Ó´Ö£¨0£º¼Ó´Ö£¬1£ºÕý³££© *@param sforntsize ×ÖÌå´óС         */public static void markByText(String logoText, String srcImgPath,String targerPath, Integer degree,float transparency,Integer sWidth, Integer sHeight,String sfontbold,Integer sforntsize) {// Ö÷ͼƬµÄ·¾¶InputStream is = null;OutputStream os = null;try {Image srcImg = ImageIO.read(new File(srcImgPath));BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);// µÃµ½»­±Ê¶ÔÏóGraphics2D g = 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);}// ÉèÖÃÑÕÉ«g.setColor(Color.blue);// ÉèÖà Font(×ÖÌ壬×ÖÌåÑùʽ£¬×ÖÌå´óС) Èçfont("ËÎÌå",Font.BOLD,20)if(sfontbold!=null&&sfontbold.equals("0")){g.setFont(new Font("ËÎÌå", Font.BOLD, sforntsize));}else {g.setFont(new Font("ËÎÌå", Font.ITALIC, sforntsize));}// ÉèÖÃ͸Ã÷¶È 1f ²»Í¸Ã÷£¬0.5f°ë͸Ã÷£¬0fÍêȫ͸Ã÷float alpha = transparency;g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));ImageIcon imgIcon = new ImageIcon(srcImgPath);Image theImg = imgIcon.getImage();int width = theImg.getWidth(null);int height = theImg.getHeight(null);// µÚÒ»²ÎÊý->ÉèÖõÄÄÚÈÝ£¬ºóÃæÁ½¸ö²ÎÊý->ÎÄ×ÖÔÚͼƬÉϵÄ×ø±êλÖÃ(x,y) .g.drawString(logoText, width*((float)sWidth/(float)100), height*(float)sHeight/(float)100);g.dispose();os = new FileOutputStream(targerPath);// Éú³ÉͼƬImageIO.write(buffImg, "JPG", os);} catch (Exception e) {e.printStackTrace();} finally {try {if (null != is)is.close();} catch (Exception e) {e.printStackTrace();}try {if (null != os)os.close();} catch (Exception e) {e.printStackTrace();}}}}

¶ÁÊéÈËÍø >±à³Ì

ÈȵãÍÆ¼ö