日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java實(shí)現(xiàn)上傳圖片后的縮放處理

 真愛圖書 2011-11-04

package com.test.image;

import java.awt.Color; 

 import java.awt.Graphics2D; 

 import java.awt.Image; 

 import java.awt.image.BufferedImage; 

 import java.awt.image.ConvolveOp; 

 import java.awt.image.Kernel; 

 import java.io.File; 

 import java.io.FileOutputStream; 

import java.io.InputStream; 


  

 import org.apache.commons.fileupload.FileItem; 
import org.apache.log4j.Logger;

 

  

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

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

   
 public class ImageUtil { 

     /** 

      * Logger for this class 

      */

     private static final Logger logger = Logger.getLogger(ImageUtil.class); 

   

     public void imageScale(String srcFilePath, String targetFilePath, 

            int width, int height) { 

         this.imageScale(new File(srcFilePath), new File(targetFilePath), width, 

                 height); 

     } 

   

     public void imageScale(File srcFile, File targetFile, int width, int height) { 

         try { 

             Image image = javax.imageio.ImageIO.read(srcFile); 

   

             image = image.getScaledInstance(width, height, 

                     Image.SCALE_AREA_AVERAGING); 

            // Make a BufferedImage from the Image. 

             BufferedImage mBufferedImage = new BufferedImage(width, height, 

                     BufferedImage.TYPE_INT_RGB); 

             Graphics2D g2 = mBufferedImage.createGraphics();             

   

             g2.drawImage(image, 0, 0, width, height, Color.white, null); 

             g2.dispose(); 

   

             float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2, 

                     -0.125f, -0.125f, -0.125f, -0.125f }; 

             Kernel kernel = new Kernel(3, 3, kernelData2); 

             ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); 

             mBufferedImage = cOp.filter(mBufferedImage, null); 

   

             File targetDir = targetFile.getParentFile(); 

             if (!targetDir.exists()) 

                 targetDir.mkdirs(); 

   

             FileOutputStream out = new FileOutputStream(targetFile); 

             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 

             encoder.encode(mBufferedImage); 

            out.close(); 

         } catch (Exception e) { 

            logger.error( 

                     "imageScale(String, String, int, int) - 圖片壓縮出錯(cuò) - srcFilePath="

                             + srcFile.getPath() + ", targeFilePath="

                             + targetFile.getPath() + ", width=" + width 

                            + ", height=" + height, e); 

         } 
         

     } 

   

     public void imageScale(FileItem fileItem, String targetFilePath, int width, 

            int height) { 

         try { 

             InputStream input = fileItem.getInputStream(); 

             Image image = javax.imageio.ImageIO.read(input); 

   

             image = image.getScaledInstance(width, height, 

                     Image.SCALE_AREA_AVERAGING); 

             BufferedImage mBufferedImage = new BufferedImage(width, height, 

                     BufferedImage.TYPE_INT_RGB); 

             Graphics2D g2 = mBufferedImage.createGraphics(); 

   

             g2.drawImage(image, 0, 0, width, height, Color.white, null); 

             g2.dispose(); 

   

             float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2, 

                     -0.125f, -0.125f, -0.125f, -0.125f }; 

            Kernel kernel = new Kernel(3, 3, kernelData2); 

             ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); 

             mBufferedImage = cOp.filter(mBufferedImage, null); 

  

             File target = new File(targetFilePath); 

            File targetDir = target.getParentFile(); 

             if (!targetDir.exists()) 

                targetDir.mkdirs(); 

   

             FileOutputStream out = new FileOutputStream(targetFilePath); 

             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 

             encoder.encode(mBufferedImage); 

             out.close(); 

             input.close(); 

         } catch (Exception e) { 

             logger.error( 

                     "imageScale(String, String, int, int) - 圖片壓縮出錯(cuò) - fileItem="

                             + fileItem.getName() + ", targetFilePath="

                             + targetFilePath + ", width=" + width + ", height="

                             + height, e); 

        } 
         

     } 

       

     public static void main(String[] args) { 

         ImageUtil iu=new ImageUtil(); 

         iu.imageScale("圖片位置/t.jpg", "存放位置/**.jpg", width, height); 

     } 

 }

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約