Java圖片處理類

jopen 10年前發布 | 44K 次閱讀 Java 圖形/圖像處理

package com.util;

import java.io.;
import java.net.URL;
import java.awt.
;
import java.awt.image.*;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import javax.imageio.ImageIO;

import com.config.Config;

public class ImgUtil {

/** 
 * 生成圖片函數 
 * @param imgUrl 網站地址 
 * @param fileURL 保存到地址 
 * @param fileName 文件名 
 */  
public static String makeImg(String imgUrl, String fileURL,String fileName) {  
    try {  
        // 創建流  
        BufferedInputStream in = new BufferedInputStream(new URL(imgUrl).openStream());  

        // 存放地址  
        String filePath=fileURL + "http://"+fileName;  

        File img = new File(filePath);  
        // 生成圖片  
        BufferedOutputStream out = new BufferedOutputStream(  
                new FileOutputStream(img));  
        byte[] buf = new byte[2048];  
        int length = in.read(buf);  
        while (length != -1) {  
            out.write(buf, 0, length);  
            length = in.read(buf);  
        }  
        in.close();  
        out.close();  
        return filePath;  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
    return "";  
}  

 /** *//**  
 * 縮放圖像  
 * @param srcImageFile 源圖像文件地址  
 * @param result       縮放后的圖像地址  
 * @param scale        縮放比例  
 * @param flag         縮放選擇:true 放大; false 縮小;  
 */    
public static String scale(String srcImageFile, String result, int scale, boolean flag)    
{    
    try    
    {    
        BufferedImage src = ImageIO.read(new File(srcImageFile)); // 讀入文件    
        int width = src.getWidth(); // 得到源圖寬    
        int height = src.getHeight(); // 得到源圖長    
        if (flag)    
        {    
            // 放大    
            width = width * scale;    
            height = height * scale;    
        }    
        else    
        {    
            // 縮小    
            width = width / scale;    
            height = height / scale;    
        }    
        Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);    
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    
        Graphics g = tag.getGraphics();    
        g.drawImage(image, 0, 0, null); // 繪制縮小后的圖    
        g.dispose();    
        ImageIO.write(tag, "JPEG", new File(result));// 輸出到文件流    
    }    
    catch (IOException e)    
    {    
        e.printStackTrace();    
    }    
    return srcImageFile;  
}    
/** *//**  
 * 圖像切割  
 * @param srcImageFile 源圖像地址  
 * @param descDir      切片目標文件夾  
 * @param destWidth    目標切片寬度  
 * @param destHeight   目標切片高度  
 */    
public static void cut(String srcImageFile, String descDir, int destWidth, int destHeight)    
{    
    try    
    {    
        Image img;    
        ImageFilter cropFilter;    
        // 讀取源圖像    
        BufferedImage bi = ImageIO.read(new File(srcImageFile));    
        int srcWidth = bi.getHeight(); // 源圖寬度    
        int srcHeight = bi.getWidth(); // 源圖高度    
        if (srcWidth > destWidth && srcHeight > destHeight)    
        {    
            Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);    
            destWidth = 200; // 切片寬度    
            destHeight = 150; // 切片高度    
            int cols = 0; // 切片橫向數量    
            int rows = 0; // 切片縱向數量    
            // 計算切片的橫向和縱向數量    
            if (srcWidth % destWidth == 0)    
            {    
                cols = srcWidth / destWidth;    
            }    
            else    
            {    
                cols = (int) Math.floor(srcWidth / destWidth) + 1;    
            }    
            if (srcHeight % destHeight == 0)    
            {    
                rows = srcHeight / destHeight;    
            }    
            else    
            {    
                rows = (int) Math.floor(srcHeight / destHeight) + 1;    
            }    
            // 循環建立切片    
            // 改進的想法:是否可用多線程加快切割速度    
            for (int i = 0; i < rows; i++)    
            {    
                for (int j = 0; j < cols; j++)    
                {    
                    // 四個參數分別為圖像起點坐標和寬高    
                    // 即: CropImageFilter(int x,int y,int width,int height)    
                    cropFilter = new CropImageFilter(j * 200, i * 150, destWidth, destHeight);    
                    img = Toolkit.getDefaultToolkit().createImage(    
                                    new FilteredImageSource(image.getSource(), cropFilter));    
                    BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);    
                    Graphics g = tag.getGraphics();    
                    g.drawImage(img, 0, 0, null); // 繪制縮小后的圖    
                    g.dispose();    
                    // 輸出為文件    
                    ImageIO.write(tag, "JPEG", new File(descDir + "pre_map_" + i + "_" + j + ".jpg"));    
                }    
            }    
        }    
    }    
    catch (Exception e)    
    {    
        e.printStackTrace();    
    }    
}    
/** *//**  
 * 圖像類型轉換 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)  
 */    
public static void convert(String source, String result)    
{    
    try    
    {    
        File f = new File(source);    
        f.canRead();    
        f.canWrite();    
        BufferedImage src = ImageIO.read(f);    
        ImageIO.write(src, "JPG", new File(result));    
    }    
    catch (Exception e)    
    {    
        // TODO Auto-generated catch block    
        e.printStackTrace();    
    }    
}    
/** *//**  
 * 彩色轉為黑白  
 * @param source  
 * @param result  
 */    
public static void gray(String source, String result)    
{    
    try    
    {    
        BufferedImage src = ImageIO.read(new File(source));    
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);    
        ColorConvertOp op = new ColorConvertOp(cs, null);    
        src = op.filter(src, null);    
        ImageIO.write(src, "JPEG", new File(result));    
    }    
    catch (IOException e)    
    {    
        e.printStackTrace();    
    }    
}    

public static void main(String[] args)  
{  
    ImgUtil.makeImg("http://t3.qlogo.cn/mbloghead/79c1deae900c9607ceec/180", Config.SOCAIL_IMG_PATH,"3.jpg");  
    ImgUtil.scale("E://myeclipse//SSHWeb//WebContent//index//image//social//3.jpg","E://myeclipse//SSHWeb//WebContent//index//image//social//3.jpg",5,false);  
}  

} </pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!