09-02
18

关于java对图片及进行抽取缩略图,添加水印及水印透明度的问题

在工作中遇到使用java来处图片的问题,介于java来处理图像图像还不是很完善,在这里将我解决抽取缩略图及添加水印等部分问题与大家分享并讨论。

有两个开源的包不得不先提一下jimi 和 ImageJ 这两个包对图像的处理有着丰富的方法
我使用的jimi大家可以在网上下载得到

下面我介绍一下我对所提问题的处理方法


import java.io.IOException;
import java.awt.Graphics;
import java.awt.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiException;
import com.sun.jimi.core.JimiWriter;
import com.sun.jimi.core.options.JPGOptions;

class ImageOperation {
public void toReduce(String source, String dest)

//将任何格式的图片转换成JPG图片

/*source原图片路径

*dest 转换后图片的路径


*/


{
      try {
       JPGOptions options = new JPGOptions();
       options.setQuality(100);
       ImageProducer image = Jimi.getImageProducer(source);// 图片格式在jimi的解码格式范围内
       JimiWriter writer = Jimi.createJimiWriter(dest);// 图片编码
       writer.setSource(image);
       writer.setOptions(options);// 对输出文件的属性进行相关的设置,每种格式的属性都不一样
       writer.putImage(dest);
      } catch (JimiException je) {
       System.err.println("Error: " + je);
      }
}
public void changDimension(String oldfile,String newfile,int wideths,int heights)throws JimiException, IOException

//实现对图片的抽取缩略并控制生成图的大小

/*

*oldfile原图片路径


*newfile缩略后的 图片路径


*wideths 缩略后图片的宽

*heights 缩略后图片的高

*/


{
      String temp = newfile;
      File inputfile = null;
      this.toReduce(oldfile, temp);
         inputfile = new File(temp); // 读入文件    
         Image src = ImageIO.read(inputfile); // 构造Image对象
         //double wideth = (double) src.getWidth(null); // 得到源图宽
         //double height = (double) src.getHeight(null); // 得到源图长
         int iWideth = wideths;
         int iHeight = heights;
         BufferedImage tag = null;  
         try{
             tag = new BufferedImage(iWideth, iHeight,BufferedImage.TYPE_INT_RGB);
          tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图
          JimiWriter writer = Jimi.createJimiWriter(newfile);
          writer.setSource(tag);
          writer.putImage(newfile);
         }
         catch(Exception e){
          e.printStackTrace();
         }
}
        public void Imagefont(String oldfile,String newfile,String text)

//给图片添加文字

*oldfile newfile就不用多说了


*text 就是给图片加的文字


{
         Image img;
           String filename = oldfile.substring(oldfile.lastIndexOf(".")+1);
         String temp = newfile;
    
         try{
          if(!filename.equals("jpg")){
                 this.toReduce(oldfile, newfile);
              img = ImageIO.read(new File(temp));
          }else{
              img = ImageIO.read(new File(oldfile));
          }
             int width = img.getWidth(null);  
             int height = img.getHeight(null);    
             BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    
             Graphics g = image.createGraphics();  
          
             g.drawImage(img, 0, 0, width, height, null);    
             g.setColor(Color.red);
             g.setFont(new Font("Courier", Font.HANGING_BASELINE, 40));
             g.drawString(text, width-360, height-72);    
             g.dispose();  
        
             FileOutputStream os = new FileOutputStream(temp);    
             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);    
             encoder.encode(image);
      
         }
         catch(Exception e){
          e.getMessage();
         }
        }
public void Imagese(String oldfile,String newfile,String syoldfile,int t,float to)

//给图片添加水印,并控制水印图片的位置及透明度

*oldfile newfile我也不说了

*syoldfile 要添加的水印 图片的路径


*t 水印图品添加的位置


*to 水印图片的透明度


{
         String filename = oldfile.substring(oldfile.lastIndexOf(".")+1);
         Image img;
         String temp = newfile;
         float alpha = to;
    
         try{    
          if(!filename.equals("jpg")){
                 toReduce(oldfile, newfile);
                 img = ImageIO.read(new File(temp));
             }else{
                 img = ImageIO.read(new File(oldfile));
             }
                int width = img.getWidth(null);  
                int height = img.getHeight(null);  
                int ws = width/3;
                int hs = height/3;
            
                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
                Graphics2D g = image.createGraphics();    
                g.drawImage(img, 0, 0, width, height, null);    
                Image logo = ImageIO.read(new File(syoldfile));    
                int lw = logo.getWidth(null);    
                int lh = logo.getHeight(null);  
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
          g.drawImage(logo,null,null);

                switch(t){
                    case 1:
                        g.drawImage(logo, 0, 0, lw, lh, null);
                        break;
                    case 2:
                        g.drawImage(logo, ws, 0, lw, lh, null);
                        break;
                    case 3:
                        g.drawImage(logo, 2*ws, 0, lw, lh, null);
                        break;
                    case 4:
                        g.drawImage(logo, 0, hs, lw, lh, null);
                        break;
                    case 5:
                        g.drawImage(logo, ws, hs, lw, lh, null);
                        break;
                    case 6:
                        g.drawImage(logo, 2*ws, hs, lw, lh, null);
                        break;
                    case 7:
                        g.drawImage(logo, 0, 2*hs, lw, lh, null);
                        break;
                    case 8:
                        g.drawImage(logo, ws, 2*hs, lw, lh, null);
                        break;
                    case 9:
                        g.drawImage(logo, 2*ws, 2*hs, lw, lh, null);
                        break;
                    default:
                     break;
                }    
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                g.dispose();  
                FileOutputStream oss = new FileOutputStream(temp);    
                JPEGImageEncoder encoders = JPEGCodec.createJPEGEncoder(oss);    
                encoders.encode(image);  
         }
         catch(Exception e){
          e.getMessage();
         }
      
        }
}


此文中有部分方法摘自互联网



文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 缩略图 水印 透明度
相关日志:
评论: 0 | 引用: 0 | 查看次数: 356
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭