ImageUtils

[java]  view plain copy
  1. package com.easystructure.utils.system;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.FontMetrics;  
  6. import java.awt.Graphics;  
  7. import java.awt.Graphics2D;  
  8. import java.awt.Image;  
  9. import java.awt.image.BufferedImage;  
  10. import java.io.File;  
  11. import java.io.FileInputStream;  
  12. import java.io.FileNotFoundException;  
  13. import java.io.FileOutputStream;  
  14. import java.io.IOException;  
  15. import java.io.InputStream;  
  16.   
  17. import javax.imageio.ImageIO;  
  18.   
  19. import com.sun.image.codec.jpeg.JPEGCodec;  
  20. import com.sun.image.codec.jpeg.JPEGEncodeParam;  
  21. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  22.   
  23. @SuppressWarnings("all")  
  24. public class ImageUtils {  
  25.   
  26.  /** 
  27.   * 把图片印刷到图片上 
  28.   *  
  29.   * @param pressImg 
  30.   *            -- 水印文件 
  31.   * @param targetImg 
  32.   *            -- 目标文件 
  33.   * @param intAlign 
  34.   *            --位置类型 
  35.   */  
  36.  public final static void pressImage(String pressImg, String targetImg, int intAlign) {  
  37.     
  38.   try {  
  39.    // 目标文件  
  40.    File _file = new File(targetImg);  
  41.    Image src = ImageIO.read(_file);  
  42.    int wideth = src.getWidth(null);  
  43.    int height = src.getHeight(null);  
  44.    BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);  
  45.    Graphics2D g = image.createGraphics();  
  46.    g.drawImage(src, 00, wideth, height, null);  
  47.   
  48.    // 水印文件  
  49.    File _filebiao = new File(pressImg);  
  50.    Image src_biao = ImageIO.read(_filebiao);  
  51.    int wideth_biao = src_biao.getWidth(null);  
  52.    int height_biao = src_biao.getHeight(null);  
  53.    if (intAlign == 1) {  
  54.     g.drawImage(src_biao, 510, wideth_biao, height_biao, null);  
  55.    } else if (intAlign == 2) {  
  56.     g.drawImage(src_biao, (wideth - wideth_biao) / 2 - 510, wideth_biao, height_biao, null);  
  57.    } else if (intAlign == 3) {  
  58.     g.drawImage(src_biao, wideth - wideth_biao - 510, wideth_biao, height_biao, null);  
  59.    } else if (intAlign == 4) {  
  60.     g.drawImage(src_biao, 5, (height - height_biao) / 2 - 10, wideth_biao, height_biao, null);  
  61.    } else if (intAlign == 5) {  
  62.     g.drawImage(src_biao, (wideth - wideth_biao) / 2 - 5, (height - height_biao) / 2 - 10, wideth_biao, height_biao, null);  
  63.    } else if (intAlign == 6) {  
  64.     g.drawImage(src_biao, wideth - wideth_biao - 5, (height - height_biao) / 2 - 10, wideth_biao, height_biao, null);  
  65.    } else if (intAlign == 7) {  
  66.     g.drawImage(src_biao, 5, height - height_biao - 10, wideth_biao, height_biao, null);  
  67.    } else if (intAlign == 8) {  
  68.     g.drawImage(src_biao, (wideth - wideth_biao) / 2 - 5, height - height_biao - 10, wideth_biao, height_biao, null);  
  69.    } else if (intAlign == 9) {  
  70.     g.drawImage(src_biao, wideth - wideth_biao - 5, height - height_biao - 10, wideth_biao, height_biao, null);  
  71.    }  
  72.    // 水印文件结束  
  73.    g.dispose();  
  74.    FileOutputStream out = new FileOutputStream(targetImg);  
  75.    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  76.    encoder.encode(image);  
  77.    out.close();  
  78.   } catch (Exception e) {  
  79.    e.printStackTrace();  
  80.   }  
  81.  }  
  82.   
  83.  /** 
  84.   *  
  85.   * @param strImageText 
  86.   * @param fm 
  87.   * @return 
  88.   * @author googol Feb 9, 2006 
  89.   */  
  90.  public static int getStringWidth(String strImageText, FontMetrics fm) {  
  91.   int intReturn = 0;  
  92.   int intCount = strImageText.length();  
  93.   char chrImageText[] = strImageText.toCharArray();  
  94.   for (int i = 0; i < intCount; i++) {  
  95.    int charWidth = fm.charWidth(chrImageText[i]);  
  96.    intReturn += charWidth;  
  97.   }  
  98.   
  99.   return intReturn += 10;  
  100.  }  
  101.   
  102.  /** */  
  103.  /** 
  104.   * 打印文字水印图片 
  105.   *  
  106.   * @param pressText 
  107.   *            --文字 
  108.   * @param targetImg 
  109.   *            -- 目标图片 
  110.   * @param fontName 
  111.   *            -- 字体名 
  112.   * @param fontStyle 
  113.   *            -- 字体样式 
  114.   * @param color 
  115.   *            -- 字体颜色 
  116.   * @param fontSize 
  117.   *            -- 字体大小 
  118.   * @param intAlign 
  119.   *            --位置类型 
  120.   */  
  121.   
  122.  public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int intAlign) {  
  123.   try {  
  124.    File _file = new File(targetImg);  
  125.    Image src = ImageIO.read(_file);  
  126.    int wideth = src.getWidth(null);  
  127.    int height = src.getHeight(null);  
  128.    BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);  
  129.    Graphics2D g = image.createGraphics();  
  130.    g.drawImage(src, 00, wideth, height, null);  
  131.    g.setColor(color);  
  132.    g.setFont(new Font(fontName, fontStyle, fontSize));  
  133.    int intWidth = getStringWidth(pressText, g.getFontMetrics());  
  134.    if (wideth > intWidth) {  
  135.     if (intAlign == 1) {  
  136.      g.drawString(pressText, 520);  
  137.     } else if (intAlign == 2) {  
  138.      g.drawString(pressText, (wideth - intWidth) / 2 - 520);  
  139.     } else if (intAlign == 3) {  
  140.      g.drawString(pressText, wideth - intWidth - 520);  
  141.     } else if (intAlign == 4) {  
  142.      g.drawString(pressText, 5, height / 2 - 10);  
  143.     } else if (intAlign == 5) {  
  144.      g.drawString(pressText, (wideth - intWidth) / 2 - 5, height / 2 - 10);  
  145.     } else if (intAlign == 6) {  
  146.      g.drawString(pressText, wideth - intWidth - 5, height / 2 - 10);  
  147.     } else if (intAlign == 7) {  
  148.      g.drawString(pressText, 5, height - 10);  
  149.     } else if (intAlign == 8) {  
  150.      g.drawString(pressText, (wideth - intWidth) / 2 - 5, height - 10);  
  151.     } else if (intAlign == 9) {  
  152.      g.drawString(pressText, wideth - intWidth - 5, height - 10);  
  153.     }  
  154.    } else {  
  155.     g.drawString(pressText, 0, height - 10);  
  156.    }  
  157.    g.dispose();  
  158.    FileOutputStream out = new FileOutputStream(targetImg);  
  159.    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  160.    encoder.encode(image);  
  161.    out.close();  
  162.   } catch (Exception e) {  
  163.    System.out.println(e);  
  164.   }  
  165.  }  
  166.   
  167.  /** 
  168.   * 只限制宽度生成缩略图,按比例缩放,如果原图宽度比需要生成的图的宽度还小,则简单拷贝一张图 
  169.   *  
  170.   * @author Dennis 
  171.   *  
  172.   *         2009-11-17 下午04:41:55 
  173.   */  
  174.  public static void makeMiniature(String strPicturePath, String strOutPath, int intMinWidth) {  
  175.   File _file = new File(strPicturePath);  
  176.   Image src;  
  177.   try {  
  178.    src = ImageIO.read(_file);  
  179.    int width = src.getWidth(null);  
  180.    int height = src.getHeight(null);  
  181.    if (width <= intMinWidth) {  
  182.     // 直接copy  
  183.     makeMiniature(strPicturePath, strOutPath, width, height);  
  184.    } else {  
  185.     // 得到缩放比例  
  186.     float scale = (float) intMinWidth / width;  
  187.     makeMiniature(strPicturePath, strOutPath, intMinWidth, (int) (height * scale - 1));  
  188.    }  
  189.   
  190.   } catch (IOException e) {  
  191.    System.out.println(e);  
  192.   }  
  193.  }  
  194.   
  195.  /** 
  196.   * 产生一个缩略 
  197.   *  
  198.   * @param strPicturePath 
  199.   *            原图片位置 
  200.   * @param strOutPath 
  201.   *            生成图片的位置 2009-11-17 下午04:43:50 
  202.   */  
  203.  public static void makeMiniature(String strPicturePath, String strOutPath, int intMinWidth, int intMinHeight) {  
  204.   
  205.   try {  
  206.    String imageFile = strPicturePath;  
  207.    File file = new File(imageFile);  
  208.   
  209.    BufferedImage im = null;  
  210.    InputStream imageIn = new FileInputStream(file);  
  211.   
  212.    im = ImageIO.read(imageIn);  
  213.    int minh = intMinHeight, minw = intMinWidth;  
  214.   
  215.    BufferedImage imout = new BufferedImage(minw, minh, 1);  
  216.    Graphics g = imout.getGraphics();  
  217.    g.drawImage(im, 00, minw, minh, null);  
  218.   
  219.    imageIn.close();  
  220.   
  221.    File out = new File(strOutPath);  
  222.    if (strOutPath.endsWith(".JPG") || strOutPath.endsWith(".jpg")) {  
  223.     ImageIO.write(imout, "jpg", out);  
  224.   
  225.    } else if (strOutPath.endsWith(".GIF") || strOutPath.endsWith(".gif")) {  
  226.     ImageIO.write(imout, "gif", out);  
  227.    }  
  228.   } catch (Exception ex) {  
  229.    ex.printStackTrace();  
  230.   }  
  231.  }  
  232.   
  233.    
  234.  /** 
  235.   * 压缩图片文件<br> 
  236.   * 先保存原文件,再压缩、上传 
  237.   *  
  238.   * @param oldFile 
  239.   *            要进行压缩的文件全路径 
  240.   * @param width 
  241.   *            宽度 
  242.   * @param height 
  243.   *            高度 
  244.   * @param quality 
  245.   *            质量 
  246.   * @param smallIcon 
  247.   *            小图片的后缀 
  248.   * @return 返回压缩后的文件的全路径 
  249.   */  
  250.  public static String zipImageFile(String oldFile, int width, int height, float quality, String smallIcon) {  
  251.   if (oldFile == null)  
  252.    return null;  
  253.   String newImage = null;  
  254.   try {  
  255.    /** 对服务器上的临时文件进行处理 */  
  256.    Image srcFile = ImageIO.read(new File(oldFile));  
  257.    double rate1 = ((double) srcFile.getWidth(null)) / (double) width + 0.1;  
  258.    double rate2 = ((double) srcFile.getHeight(null)) / (double) width + 0.1;  
  259.    double rate = rate1 > rate2 ? rate1 : rate2;  
  260.    width = (int) (((double) srcFile.getWidth(null)) / rate);  
  261.    width = (int) (((double) srcFile.getHeight(null)) / rate);  
  262.   
  263.    /** 宽,高设定 */  
  264.    BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  265.    tag.getGraphics().drawImage(srcFile, 00, width, height, null);  
  266.    String filePrex = oldFile.substring(0, oldFile.indexOf('.'));  
  267.    /** 压缩后的文件名 */  
  268.    newImage = filePrex + smallIcon + oldFile.substring(filePrex.length());  
  269.   
  270.    /** 压缩之后临时存放位置 */  
  271.    FileOutputStream out = new FileOutputStream(newImage);  
  272.   
  273.    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  274.    JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);  
  275.    /** 压缩质量 */  
  276.    jep.setQuality(quality, true);  
  277.    encoder.encode(tag, jep);  
  278.    out.close();  
  279.   
  280.   } catch (FileNotFoundException e) {  
  281.    e.printStackTrace();  
  282.   } catch (IOException e) {  
  283.    e.printStackTrace();  
  284.   }  
  285.   return newImage;  
  286.  }  
  287.   
  288.  /** 
  289.   * 经过缩略图算法的压缩。存放生成图片的路径同源图片。 
  290.   *  
  291.   * @param oldFile 
  292.   * @param width 
  293.   * @param height 
  294.   * @param smallIcon 
  295.   */  
  296.  public static void zipImageFileSameOldPath(String oldFile, int intMinWidth, int intMinHeight, String smallIcon) {  
  297.     
  298.   String strPicturePath = oldFile;  
  299.   String strOutPath = CommonUtils.addStrToFileName(strPicturePath, smallIcon);  
  300.   zipImageFileDifferPath(strPicturePath, strOutPath, intMinWidth, intMinHeight);  
  301.  }  
  302.   
  303.  /** 
  304.   * 经过缩略图算法的压缩。存放生成图片的路径为指定路径。 
  305.   *  
  306.   * @param oldFile 
  307.   * @param width 
  308.   * @param height 
  309.   * @param smallIcon 
  310.   */  
  311.  public static void zipImageFileDifferPath(String oldFilePath, String newFilePath, int intMinWidth, int intMinHeight) {  
  312.     
  313.   BufferedImage im = null;  
  314.   BufferedImage imout = null;  
  315.   int minh = intMinHeight;  
  316.   int minw = intMinWidth;  
  317.   try {  
  318.    File file = new File(oldFilePath);  
  319.    InputStream imageIn = new FileInputStream(file);  
  320.    im = ImageIO.read(imageIn);  
  321.       imout = new ImageScale().imageZoomOut(im, minw, minh);  
  322.    imageIn.close();  
  323.   
  324.    File out = new File(newFilePath);  
  325.    if (newFilePath.endsWith(".JPG") || newFilePath.endsWith(".jpg")) {  
  326.     ImageIO.write(imout, "jpg", out);  
  327.    } else if (newFilePath.endsWith(".GIF") || newFilePath.endsWith(".gif")) {  
  328.     ImageIO.write(imout, "gif", out);  
  329.    }  
  330.   } catch (Exception ex) {  
  331.    ex.printStackTrace();  
  332.   }  
  333.  }  
  334.    
  335.    
  336.  public static void main(String[] args) {  
  337.   // pressImage("F:/bisonte.png", "F:/123.jpg", 9);  
  338.   pressText("Dennis""F:/123.jpg""宋体", Font.PLAIN, Color.RED, 156);  
  339.   // createMark("F:/123.jpg", "mmmmnn", Color.RED, 15);  
  340.  }  
  341.   
  342. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值