Java_ImageIO(图像处理)

via: http://blog.csdn.net/hu_shengyang/article/details/7433988

 

以下为对java ImageIO处理图形的一些常用的方法进行的封装。全部测试通过,main方法中的均为测试代码,如果想用这个图像处理类,可以参考mian方法的例子。

另外代码中有些地方需要改进,效率也需要进一步提高。

 

package com.adam.dev.pic.easyImage;  

import java.awt.AlphaComposite;  

import java.awt.Color;  

import java.awt.Font;  

import java.awt.Graphics;  

import java.awt.Graphics2D;  

import java.awt.Point;  

import java.awt.Rectangle;  

import java.awt.color.ColorSpace;  

import java.awt.image.BufferedImage;  

import java.awt.image.ColorConvertOp;  

import java.io.BufferedReader;  

import java.io.File;  

import java.io.FileInputStream;  

import java.io.FileOutputStream;  

import java.io.FileReader;  

import java.io.IOException;  

import java.io.InputStream;  

import java.io.OutputStream;  

import java.util.Iterator;  

import java.util.List;  

  

import javax.imageio.ImageIO;  

import javax.imageio.ImageReadParam;  

import javax.imageio.ImageReader;  

import javax.imageio.stream.ImageInputStream;  

  

import com.sun.image.codec.jpeg.JPEGCodec;  

import com.sun.image.codec.jpeg.JPEGImageEncoder;  

  

/** 

 * @author adam.胡升阳 

 * 创建日期 2012-2-29 

 */  

public class OperateImage{  

  

    public OperateImage() {  

        super();  

    }  

  

    /**  

     * 对图片裁剪,并把裁剪新图片保存  

     * @param srcPath 读取源图片路径 

     * @param toPath    写入图片路径 

     * @param x 剪切起始点x坐标 

     * @param y 剪切起始点y坐标 

     * @param width 剪切宽度 

     * @param height     剪切高度 

     * @param readImageFormat  读取图片格式 

     * @param writeImageFormat 写入图片格式 

     * @throws IOException 

     */  

    public void cropImage(String srcPath,String toPath,  

            int x,int y,int width,int height,  

            String readImageFormat,String writeImageFormat) throws IOException{     

        FileInputStream fis = null ;  

        ImageInputStream iis =null ;  

        try{     

            //读取图片文件  

            fis = new FileInputStream(srcPath);   

            Iterator it = ImageIO.getImageReadersByFormatName(readImageFormat);   

            ImageReader reader = (ImageReader) it.next();   

            //获取图片流   

            iis = ImageIO.createImageInputStream(fis);    

            reader.setInput(iis,true) ;  

            ImageReadParam param = reader.getDefaultReadParam();   

            //定义一个矩形  

            Rectangle rect = new Rectangle(x, y, width, height);   

            //提供一个 BufferedImage,将其用作解码像素数据的目标。   

            param.setSourceRegion(rect);  

            BufferedImage bi = reader.read(0,param);                  

            //保存新图片   

            ImageIO.write(bi, writeImageFormat, new File(toPath));       

        }finally{  

            if(fis!=null)  

                fis.close();         

            if(iis!=null)  

               iis.close();   

        }   

    }  

  

    /** 

     * 按倍率缩小图片 

     * @param srcImagePath 读取图片路径 

     * @param toImagePath 写入图片路径 

     * @param widthRatio    宽度缩小比例 

     * @param heightRatio    高度缩小比例 

     * @throws IOException 

     */  

    public void reduceImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) throws IOException{  

        FileOutputStream out = null;  

        try{  

            //读入文件    

            File file = new File(srcImagePath);    

            // 构造Image对象    

            BufferedImage src = javax.imageio.ImageIO.read(file);    

            int width = src.getWidth();    

            int height = src.getHeight();    

            // 缩小边长   

            BufferedImage tag = new BufferedImage(width / widthRatio, height / heightRatio, BufferedImage.TYPE_INT_RGB);    

            // 绘制 缩小  后的图片   

            tag.getGraphics().drawImage(src, 0, 0, width / widthRatio, height / heightRatio, null);    

            out = new FileOutputStream(toImagePath);    

            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    

            encoder.encode(tag);    

        }catch(Exception e){  

            e.printStackTrace();  

        }finally{  

            if(out != null){  

                out.close();    

            }  

        }  

    }  

  

    /** 

     * 长高等比例缩小图片 

     * @param srcImagePath 读取图片路径 

     * @param toImagePath 写入图片路径 

     * @param ratio 缩小比例 

     * @throws IOException 

     */  

    public void reduceImageEqualProportion(String srcImagePath,String toImagePath,int ratio) throws IOException{  

        FileOutputStream out = null;  

        try{  

            //读入文件    

            File file = new File(srcImagePath);    

            // 构造Image对象    

            BufferedImage src = javax.imageio.ImageIO.read(file);    

            int width = src.getWidth();    

            int height = src.getHeight();    

            // 缩小边长   

            BufferedImage tag = new BufferedImage(width / ratio, height / ratio, BufferedImage.TYPE_INT_RGB);    

            // 绘制 缩小  后的图片   

            tag.getGraphics().drawImage(src, 0, 0, width / ratio, height / ratio, null);    

            out = new FileOutputStream(toImagePath);    

            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    

            encoder.encode(tag);    

        }catch(Exception e){  

            e.printStackTrace();  

        }finally{  

            if(out != null){  

                out.close();    

            }  

        }  

    }  

      

    /** 

     * 按倍率放大图片 

     * @param srcImagePath 读取图形路径 

     * @param toImagePath 写入入行路径 

     * @param widthRatio    宽度放大比例 

     * @param heightRatio 高度放大比例 

     * @throws IOException 

     */  

    public void enlargementImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) throws IOException{  

        FileOutputStream out = null;  

        try{  

            //读入文件    

            File file = new File(srcImagePath);    

            // 构造Image对象    

            BufferedImage src = javax.imageio.ImageIO.read(file);    

            int width = src.getWidth();    

            int height = src.getHeight();    

            // 放大边长  

            BufferedImage tag = new BufferedImage(width * widthRatio, height * heightRatio, BufferedImage.TYPE_INT_RGB);    

            //绘制放大后的图片  

            tag.getGraphics().drawImage(src, 0, 0, width * widthRatio, height * heightRatio, null);    

            out = new FileOutputStream(toImagePath);    

            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    

            encoder.encode(tag);    

        }catch(Exception e){  

            e.printStackTrace();  

        }finally{  

            if(out != null){  

                out.close();    

            }  

        }  

    }  

      

      

    /** 

     * 长高等比例放大图片 

     * @param srcImagePath 读取图形路径 

     * @param toImagePath 写入入行路径 

     * @param ratio 放大比例 

     * @throws IOException 

     */  

    public void enlargementImageEqualProportion(String srcImagePath,String toImagePath,int ratio) throws IOException{  

        FileOutputStream out = null;  

        try{  

            //读入文件    

            File file = new File(srcImagePath);    

            // 构造Image对象    

            BufferedImage src = javax.imageio.ImageIO.read(file);    

            int width = src.getWidth();    

            int height = src.getHeight();    

            // 放大边长  

            BufferedImage tag = new BufferedImage(width * ratio, height * ratio, BufferedImage.TYPE_INT_RGB);    

            //绘制放大后的图片  

            tag.getGraphics().drawImage(src, 0, 0, width * ratio, height * ratio, null);    

            out = new FileOutputStream(toImagePath);    

            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    

            encoder.encode(tag);    

        }catch(Exception e){  

            e.printStackTrace();  

        }finally{  

            if(out != null){  

                out.close();    

            }  

        }  

    }  

      

    /** 

     * 重置图形的边长大小 

     * @param srcImagePath  

     * @param toImagePath 

     * @param width 

     * @param height 

     * @throws IOException 

     */  

    public void resizeImage(String srcImagePath,String toImagePath,int width,int height) throws IOException{  

        FileOutputStream out = null;  

        try{  

            //读入文件    

            File file = new File(srcImagePath);    

            // 构造Image对象    

            BufferedImage src = javax.imageio.ImageIO.read(file);    

            // 放大边长  

            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    

            //绘制放大后的图片  

            tag.getGraphics().drawImage(src, 0, 0, width, height, null);    

            out = new FileOutputStream(toImagePath);    

            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    

            encoder.encode(tag);    

        }catch(Exception e){  

            e.printStackTrace();  

        }finally{  

            if(out != null){  

                out.close();    

            }  

        }  

    }  

      

    /** 

     * 横向拼接图片(两张) 

     * @param firstSrcImagePath 第一张图片的路径 

     * @param secondSrcImagePath    第二张图片的路径 

     * @param imageFormat   拼接生成图片的格式 

     * @param toPath    拼接生成图片的路径 

     */  

    public void joinImagesHorizontal(String firstSrcImagePath, String secondSrcImagePath,String imageFormat, String toPath){    

        try {    

            //读取第一张图片      

            File  fileOne  =  new  File(firstSrcImagePath);      

            BufferedImage  imageOne = ImageIO.read(fileOne);      

            int  width  =  imageOne.getWidth();//图片宽度      

            int  height  =  imageOne.getHeight();//图片高度      

            //从图片中读取RGB      

            int[]  imageArrayOne  =  new  int[width*height];      

            imageArrayOne  =  imageOne.getRGB(0,0,width,height,imageArrayOne,0,width);      

             

            //对第二张图片做相同的处理      

            File  fileTwo  =  new  File(secondSrcImagePath);      

            BufferedImage  imageTwo  =  ImageIO.read(fileTwo);   

            int width2 = imageTwo.getWidth();  

            int height2 = imageTwo.getHeight();  

            int[]   ImageArrayTwo  =  new  int[width2*height2];      

            ImageArrayTwo  =  imageTwo.getRGB(0,0,width,height,ImageArrayTwo,0,width);      

            //ImageArrayTwo  =  imageTwo.getRGB(0,0,width2,height2,ImageArrayTwo,0,width2);   

             

            //生成新图片  

            //int height3 = (height>height2 || height==height2)?height:height2;  

            BufferedImage  imageNew  =  new  BufferedImage(width*2,height,BufferedImage.TYPE_INT_RGB);      

            //BufferedImage  imageNew  =  new  BufferedImage(width+width2,height3,BufferedImage.TYPE_INT_RGB);      

            imageNew.setRGB(0,0,width,height,imageArrayOne,0,width);//设置左半部分的RGB    

            imageNew.setRGB(width,0,width,height,ImageArrayTwo,0,width);//设置右半部分的RGB   

            //imageNew.setRGB(width,0,width2,height2,ImageArrayTwo,0,width2);//设置右半部分的RGB      

             

            File  outFile  =  new  File(toPath);      

            ImageIO.write(imageNew,  imageFormat,  outFile);//写图片  

        } catch (Exception e) {    

            e.printStackTrace();    

        }    

    }  

      

    /** 

     * 横向拼接一组(多张)图像 

     * @param pics  将要拼接的图像 

     * @param type 图像写入格式 

     * @param dst_pic 图像写入路径 

     * @return 

     */  

    public  boolean joinImageListHorizontal(String[] pics, String type, String dst_pic) {     

        try {    

            int len = pics.length;    

            if (len < 1) {    

                System.out.println("pics len < 1");    

                return false;    

            }    

            File[] src = new File[len];    

            BufferedImage[] images = new BufferedImage[len];    

            int[][] imageArrays = new int[len][];    

            for (int i = 0; i < len; i++) {    

                src[i] = new File(pics[i]);    

                images[i] = ImageIO.read(src[i]);    

                int width = images[i].getWidth();    

                int height = images[i].getHeight();    

                imageArrays[i] = new int[width * height];// 从图片中读取RGB      

                imageArrays[i] = images[i].getRGB(0, 0, width, height,  imageArrays[i], 0, width);    

            }    

              

            int dst_width = 0;    

           

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值