java關於圖片處理修改圖片大小


最近做了一個關於圖片瀏覽的內容。因為圖片都是一些證件的資料的掃描件所以比較大,對系統的影響也是非常之大的,有很大可能直接把系統干死。那么我是這么處理的,給大家分享一下。如果大家有好的方案的話一定要早點告訴我。

需求簡單介紹:

上傳文件是壓縮包,但是到查看資料的時候壓縮包可下載本地看,同時也可以在系統中直接在線瀏覽。

設計方案

  

  

1 營業部用戶上傳圖片文件壓縮包文件到綜合業務系統tomcat服務器,系統在tomcat服務器將壓縮文件解壓到系統臨時目錄。

2 系統分析解壓的圖片文件(文件名上有分類和序號),按照分類和順序將文件名存入到數據庫表中。存入數據庫的文件名帶有路徑。

3 將解壓的文件和壓縮包按照規定的路徑上傳到FTP服務器中。

4 FTP服務器同時是一台tomcat服務器,圖片的目錄在tomcat服務器的webapps目錄下。

5 圖片的路徑命名方式:

     webapps/imageserver/年(如2014)/月(如05)/日(如19)/16位唯一隨機碼/壓縮包和解壓文件

6 信審部查看圖片文件和下載壓縮包時,直接通過URL訪問。

7 文件服務器不另外設置權限訪問控制,所有訪問控制通過16位唯一隨機碼控制,只有知道了16位唯一隨機碼,才能訪問到圖片文件和壓縮包。

8 為了保證訪問速度,單個圖片文件最好控制在1M一下(我們可以推薦用戶使用較低分辨率的掃描文件)。

9 如果圖片訪問量較大,需要購買硬件來升級文件服務器,例如采用NAS存儲,升級帶寬等。

實現步驟:

  1:上傳壓縮包:解壓到本地臨時目錄,檢測圖片大小修改圖片大小,上傳到ftp。

  2:讀取對應的的數據進行瀏覽。

這些相信大家都會,我下面貼出一個修改圖片大小的工具類,方便我自己記憶,如果對大家有幫助也可以參考。

這個圖片處理機制效率實在太慢,6個圖片4-5秒,查了很多試了很多也沒找到合適方法,暫且就用這個吧。如果有好的方法還望大家多多指教。

package com.minxinloan.black.web.utils;

import java.awt.Graphics;
import java.awt.Image;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.IOException;
  
import javax.imageio.ImageIO;  

import org.apache.log4j.Logger;
//  
import com.sun.image.codec.jpeg.JPEGCodec;  
import com.sun.image.codec.jpeg.JPEGImageEncoder;  
//
//import com.sun.media.jai.codecimpl.JPEGCodec;
//import com.sun.media.jai.codecimpl.JPEGImageEncoder;
  



public class ImagesUtils {  
  
    private final static Logger log = Logger.getLogger(ImagesUtils.class);    
    //圖片允許的最大大小
    public static final int IMAGEMAXSIZE = 1024*1024;
    // 圖片寬和高的最大尺寸  
    public static final int IMAGEMAXBIG = 2000;  
    // 圖片寬和高的最小尺寸  
    public static final int IMAGEMINBIG = 10;  
    // 按原圖大小生成新圖  
    public static final int CREATENEWIMAGETYPE_0 = 0;  
    // 按指定的大小生成新圖  
    public static final int CREATENEWIMAGETYPE_1 = 1;  
    // 按原圖寬高比例生成新圖-按指定的寬度  
    public static final int CREATENEWIMAGETYPE_2 = 2;  
    // 按原圖寬高比例生成新圖-按指定的高度  
    public static final int CREATENEWIMAGETYPE_3 = 3;  
    // 按原圖寬高比例生成新圖-按指定的寬和高中較大的尺寸  
    public static final int CREATENEWIMAGETYPE_4 = 4;  
    // 按原圖寬高比例生成新圖-按指定的寬和高中較小的尺寸  
    public static final int CREATENEWIMAGETYPE_5 = 5;  
    // 按原圖寬高比例生成新圖-按原圖大小的90%進行修改 
    public static final int CREATENEWIMAGETYPE_6 = 6;
  
    /** 
     *  
     * @param _file 
     *            原圖片 
     * @param createType 
     *            處理類型 
     * @param newW 
     *            新寬度 
     * @param newH 
     *            新高度 
     * @return 
     * @throws Exception 
     */  
    public static String createNewImage(File _file, int createType, int newW,  
            int newH) throws Exception {  
        if (_file == null)  
            return null;  
        String fileName = _file.getPath();  
        if (fileName == null || "".equals(fileName)  
                || fileName.lastIndexOf(".") == -1)  
            return null;  
        /* 
         * else newFileName = "_" + newFileName; 
         */  
  
        String outFileName = fileName.substring(0, fileName.lastIndexOf(".")) 
                + fileName.substring(fileName.lastIndexOf("."), fileName  
                        .length());  
        String fileExtName = fileName.substring(  
                (fileName.lastIndexOf(".") + 1), fileName.length());  
        if (newW < IMAGEMINBIG)  
            newW = IMAGEMINBIG;  
        else if (newW > IMAGEMAXBIG)  
            newW = IMAGEMAXBIG;  
  
        if (newH < IMAGEMINBIG)  
            newH = IMAGEMINBIG;  
        else if (newH > IMAGEMAXBIG)  
            newH = IMAGEMAXBIG;  
  
        // 得到原圖信息  
        if (!_file.exists() || !_file.isAbsolute() || !_file.isFile()  
                || !checkImageFile(fileExtName))  
            return null;  
        Image src = ImageIO.read(_file);  
        int w = src.getWidth(null);  
        int h = src.getHeight(null);  
  
        // 確定目標圖片的大小  
        int nw = w;  
        int nh = h;  
        if (createType == CREATENEWIMAGETYPE_0)  
            ;  
        else if (createType == CREATENEWIMAGETYPE_1) {  
            nw = newW;  
            nh = newH;  
        } else if (createType == CREATENEWIMAGETYPE_2) {  
            nw = newW;  
            nh = (int) ((double) h / (double) w * nw);  
        } else if (createType == CREATENEWIMAGETYPE_3) {  
            nh = newH;  
            nw = (int) ((double) w / (double) h * nh);  
        } else if (createType == CREATENEWIMAGETYPE_4) {  
            if ((double) w / (double) h >= (double) newW / (double) newH) {  
                nh = newH;  
                nw = (int) ((double) w / (double) h * nh);  
            } else {  
                nw = newW;  
                nh = (int) ((double) h / (double) w * nw);  
            }  
        } else if (createType == CREATENEWIMAGETYPE_5) {  
            if ((double) w / (double) h <= (double) newW / (double) newH) {  
                nh = newH;  
                nw = (int) ((double) w / (double) h * nh);  
            } else {  
                nw = newW;  
                nh = (int) ((double) h / (double) w * nw);  
            }  
        } else if(createType == CREATENEWIMAGETYPE_6){
//            nw = (int)(w*0.5);  
//            nh = (int)(h*0.5); 
            double proportion = (double)1700/(double)w;
            nw = (int)((double)w*proportion);
            nh = (int)((double)h*proportion);
        } 
  
        // 構造目標圖片  
        BufferedImage tag = new BufferedImage(nw, nh,  
                BufferedImage.TYPE_INT_RGB);  
  
        // 得到目標圖片輸出流  
        FileOutputStream out = new FileOutputStream(outFileName);  
  
        // 根據需求畫出目標圖片 方式1  
        tag.getGraphics().drawImage(src, 0, 0, nw, nh, null);  
  
        // 將畫好的目標圖輸出到輸出流 方式1  
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
        encoder.encode(tag);  
        out.close();  
        return outFileName;  
    }  
  
    public static boolean checkImageFile(String extName) {  
  
        if ("jpg".equalsIgnoreCase(extName))  
            return true;  
        if ("gif".equalsIgnoreCase(extName))  
            return true;  
        if ("bmp".equalsIgnoreCase(extName))  
            return true;  
        if ("jpeg".equalsIgnoreCase(extName))  
            return true;  
        if ("png".equalsIgnoreCase(extName))  
            return true;  
        return false;  
    }  
    public static String checkImageFile2(String extName) {  
          
        if ("jpg".equalsIgnoreCase(extName))  
            return "jpg";  
        if ("gif".equalsIgnoreCase(extName))  
            return "gif";  
        if ("bmp".equalsIgnoreCase(extName))  
            return "bmp";  
        if ("jpeg".equalsIgnoreCase(extName))  
            return "jpeg";  
        if ("png".equalsIgnoreCase(extName))  
            return "jpeg";  
        return null;  
    }  
   
    
    //遞歸修改圖片大小
    public static void changeImgSize(String filePath,int createType)
    {
        try {
            File tempFile = new File(filePath);
            if(tempFile.length()>IMAGEMAXSIZE){
                System.out.println("sss");
                changeImgSize(createNewImage(tempFile, createType, 0, 0),createType);
            }
        } catch (Exception e) {
            log.error("the changeImgSize is failed . the message is "+e.getMessage());
        }
    }
    /**
     * 縮放圖像(按比例縮放)
     * @param srcImageFile 源圖像文件地址
     * @param result 縮放后的圖像地址
     * @param scale 縮放比例
     * @param flag 縮放選擇:true 放大; false 縮小;
     */
    public final static void scale(String srcImageFile,String type, String result) {
        try {
            
            File tempFile = new File(srcImageFile);
            if(tempFile.length()>IMAGEMAXSIZE){
                System.out.println("sss");
                BufferedImage src = ImageIO.read(tempFile); // 讀入文件
                int width = src.getWidth(); // 得到源圖寬
                int height = src.getHeight(); // 得到源圖長
                
                double sc = (double)1700/(double)width;
                
             
                width = (int)((double)width * sc);
                height = (int)((double)height * sc);
              
                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, type, new File(result));// 輸出到文件流
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    public static void changeImgSize(File file)
     {
         try {
                // 判斷文件是否是文件,如果是文件,獲取路徑,並計數
                if (file.isFile()) {
                    String fileExtName = file.getName().substring(  
                            (file.getName().lastIndexOf(".") + 1), file.getName().length());  
                    
                    String temp = ImagesUtils.checkImageFile2(fileExtName);
                    if(temp!=null)
                        //scale(file.getAbsolutePath(),temp,file.getAbsolutePath());
                        ImagesUtils.changeImgSize(file.getAbsolutePath(), ImagesUtils.CREATENEWIMAGETYPE_6);
                } else {
                    // 如果是文件夾,聲明一個數組放文件夾和他的子文件
                    File[] f = file.listFiles();
                    // 遍歷文件件下的文件,並獲取路徑
                    for (File file2 : f) {
                        changeImgSize(file2);
                    }
                }
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
     }
  
    public static void main(String[] args) {
        

        
        
//
//        long start=System.currentTimeMillis();
//        String filePath = "C:\\Users\\zhangmi\\Desktop\\資料";
//        changeImgSize(new File(filePath));
//
//        long end=System.currentTimeMillis();
//        //在最好的一行加上:
//        System.out.println("執行耗時 : "+(end-start)/1000f+" 秒 ");
        
    }
}  

 

做已銘記

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM