免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

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

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); 

     } 

 }

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Java實(shí)現(xiàn)網(wǎng)頁截圖技術(shù),根據(jù)URL得到網(wǎng)頁快照(已測(cè)試)
C#放縮、截取、合并圖片并生成高質(zhì)量新圖的類
Java的圖片處理工具類
java圖片高質(zhì)量縮放類
JSP給圖片添加文字
用Java2D繪圖初探
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服