java項目實現圖片等比縮放
更新時間:2022年04月22日 14:51:53 作者:xiegongmiao
這篇文章主要為大家詳細介紹了java項目實現圖片等比縮放,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java項目實現圖片等比縮放的具體代碼,供大家參考,具體內容如下
package common; ? import java.awt.Image; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.imageio.ImageIO; ? public class ImageCompressionTask implements Runnable{ ? ? ?? ?? ?private InputStream is; ?? ?private String fileName; ?? ?private int width; ?? ?private int height; ? ?? ?/** ?? ? * 初始化參數 ?? ? * @param is 圖片輸入流 ?? ? * @param file ?圖片 ?? ? * @param fileName ?圖片名稱 ?? ? * @param width ? 高 ?? ? * @param height ?寬 ?? ? */ ?? ?public ImageCompressionTask(InputStream is,String fileName,int width,int height) { ?? ??? ?this.is=is; ?? ??? ?this.fileName=fileName; ?? ??? ?this.width=width; ?? ??? ?this.height=height;?? ??? ? ?? ?} ? ?? ?public void run() { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?try{ ?? ??? ??? ?this.compressPic(); ?? ??? ?}catch(Exception e){ ?? ??? ??? ?System.out.println("文件壓縮失敗"+e); ?? ??? ?} ?? ??? ? ?? ?} ?? ? ?? ?private String compressPic() throws Exception{ ?? ??? ?String path = "E:\\xie\\";//新圖片存放路徑 ?? ??? ?String urlPath = ?path + fileName; ?? ??? ?BufferedImage buffImage; ?? ??? ?FileOutputStream output=null; ?? ??? ?BufferedImage compressPic=null; ?? ??? ?try { ?? ??? ??? ? ?? ??? ??? ?String imagetype = ""; ?? ??? ??? ?if(fileName.lastIndexOf(".") != -1){ ?? ??? ??? ??? ?imagetype = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ??? ?imagetype = imagetype.toLowerCase(); //文件后綴名 ?? ??? ??? ?output=new FileOutputStream(urlPath); ?? ??? ??? ?buffImage=ImageIO.read(is); ?? ??? ??? ?//圖片縮放 ?? ??? ??? ?compressPic=compressPicMin(buffImage,width,height); ?? ??? ??? ?//輸出圖片 ?? ??? ??? ?ImageIO.write(compressPic, imagetype, output); ?? ??? ?} finally { ?? ??? ??? ?if(output!=null){ ?? ??? ??? ? ? try{ ?? ??? ??? ? ? ? ?output.close(); ?? ??? ??? ? ? }catch(IOException e){ ?? ??? ??? ??? ? ? e.getStackTrace(); ?? ??? ??? ? ? } ?? ??? ??? ?} ?? ??? ??? ?if(is!=null){ ?? ??? ??? ? ? is.close(); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?return fileName; ?? ??? ? ?? ?} ? ? ? /** ? ? ?* 圖片等比縮放 ? ? ?*@param image 圖片輸入緩存流 ? ? ?*@param outputWidth 圖片壓縮到的寬 ? ? ?*@param outputHeight 圖片壓縮到的高 ? ? ?*@return BufferedImage ? ? ?* */ ?? ?private BufferedImage compressPicMin(BufferedImage image, ?? ?int outputWidth, int outputHeight) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?if(image==null){ ?? ??? ??? ?return null; ?? ??? ?} ?? ??? ? ?? ??? ?//如果圖片本身的寬和高均小于要壓縮到的寬和高,則不壓縮直接返回 ?? ??? ?if(outputWidth>image.getWidth(null)&&outputHeight>image.getHeight(null)){ ?? ??? ??? ?return image; ?? ??? ?} ?? ??? ? ?? ??? ?int newWidth; ?? ??? ?int newHeight; ?? ? ? ?//寬和高等比縮放的率 ?? ??? ?double rate1=(double)image.getWidth(null)/(double)outputWidth; ?? ??? ?double rate2=(double)image.getHeight(null)/(double)outputHeight; ?? ??? ?//控制縮放大小 ?? ??? ?double rate=rate1<rate2 ? rate1:rate2; ?? ??? ?newWidth=(int) (image.getWidth(null)/rate); ?? ??? ?newHeight=(int) (image.getHeight(null)/rate); ?? ??? ? ?? ??? ?BufferedImage newImage=new BufferedImage(newWidth, newHeight,BufferedImage.TYPE_INT_RGB); ?? ??? ?newImage.createGraphics().drawImage(image.getScaledInstance(newWidth, outputHeight, Image.SCALE_SMOOTH), 0, 0, null); ? ?? ??? ?return newImage; ?? ?} ?? ? ?? ?public int getWidth() { ?? ??? ?return width; ?? ?} ? ? ?? ?public void setWidth(int width) { ?? ??? ?this.width = width; ?? ?} ? ? ?? ?public int getHeight() { ?? ??? ?return height; ?? ?} ? ? ?? ?public void setHeight(int height) { ?? ??? ?this.height = height; ?? ?} ? ? }
創(chuàng)建ImageTest寫一個main()
package test1;? ? import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; ? import common.ImageCompressionTask; ? ? public class ImageTest { ? ?? ?public static void main(String[] args){ ?? ??? ?String imgName = System.currentTimeMillis() + "_" + ((int) (Math.random() * 900) + 100) + "." + "jpg"; ?? ??? ?File f=new File("E:\\xie\\xxx.jpg"); ?? ??? ?try { ?? ??? ??? ?InputStream input = new FileInputStream(f); ?? ??? ??? ?ImageCompressionTask r=new ImageCompressionTask(input, imgName, 520, 320); ?? ??? ??? ?/* ?? ??? ??? ? * 方法一: ?? ??? ??? ? *? ?? ??? ??? ? Thread thread1 = new Thread(r); ?? ??? ??? ? thread1.start(); // 啟動線程 ?? ??? ??? ?*/ ?? ??? ??? ? ?? ??? ??? ?/* ?? ??? ??? ? * 方法二:使用ThreadPoolExecutor創(chuàng)建線程池,并不提倡我們直接使用ThreadPoolExecutor ?? ??? ??? ? *? ?? ??? ??? ? */ ?? ??? ??? ?/* ThreadPoolExecutor executor = new ThreadPoolExecutor( ?? ??? ??? ? ? ? ? ? ? ?5, ?//核心池的大?。淳€程池中的線程數目大于這個參數時,提交的任務會被放進任務緩存隊列) ?? ??? ??? ? ? ? ? ? ? ?10, //線程池最大能容忍的線程數 ?? ??? ??? ? ? ? ? ? ? ?200, //線程存活時間 ?? ?? ??? ??? ? ? ? ? ? ? ?TimeUnit.MILLISECONDS, //參數keepAliveTime的時間單位 ?? ??? ??? ? ? ? ? ? ? ?new ArrayBlockingQueue<Runnable>(5) //任務緩存隊列,用來存放等待執(zhí)行的任務 ?? ??? ??? ? ); ?? ??? ??? ?executor.execute(r);*/ ?? ??? ??? ?/* ?? ??? ??? ? * 方法三:并不提倡我們直接使用ThreadPoolExecutor,而是使用Executors類中提供的幾個靜態(tài)方法來創(chuàng)建線程池 ?? ??? ??? ? * ?以下是三個靜態(tài)方法 ?? ??? ??? ? * ?Executors.newCachedThreadPool(); ? ? ? ?//創(chuàng)建一個緩沖池,緩沖池容量大小為Integer.MAX_VALUE ? ? ? ? ? ? ? ? ? ? ? ? ?* ?Executors.newSingleThreadExecutor(); ? //創(chuàng)建容量為1的緩沖池 ? ? ? ? ? ? ? ? ? ? ? ? ?* ?Executors.newFixedThreadPool(int); ? ?//創(chuàng)建固定容量大小的緩沖池 ?? ??? ??? ? */ ?? ??? ??? ? newCachedThreadPool().execute(r); ?? ??? ??? ? //newSingleThreadExecutor().execute(r); ?? ??? ??? ? //newFixedThreadPool(10).execute(r); ?? ??? ??? ?System.out.println("圖片上傳成功"); ?? ??? ?} catch (Exception e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ?} ?? ? ?? ?/*靜態(tài)方法的具體實現 ?? ? * Executors.newCachedThreadPool() ?? ? * 創(chuàng)建一個緩沖池,緩沖池容量大小為Integer.MAX_VALUE ?? ? */ ?? ?public static ExecutorService newCachedThreadPool() { ?? ? ? ?return new ThreadPoolExecutor(0, Integer.MAX_VALUE, ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?60L, TimeUnit.SECONDS, ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new SynchronousQueue<Runnable>()); ?? ?} ?? ? ?? ?/*靜態(tài)方法的具體實現 ?? ? * Executors.newSingleThreadExecutor()? ?? ? * 創(chuàng)建容量為1的緩沖池 ?? ? */ ?? ?public static ExecutorService newSingleThreadExecutor() { ?? ? ? ?return ?new ThreadPoolExecutor(1, 1, ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0L, TimeUnit.MILLISECONDS, ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new LinkedBlockingQueue<Runnable>()); ?? ?} ?? ? ?? ?/*靜態(tài)方法的具體實現 ?? ? * Executors.newFixedThreadPool(int)? ?? ? * 創(chuàng)建固定容量大小的緩沖池 ?? ? */ ?? ?public static ExecutorService newFixedThreadPool(int nThreads) { ?? ? ? ?return new ThreadPoolExecutor(nThreads, nThreads, ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0L, TimeUnit.MILLISECONDS, ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new LinkedBlockingQueue<Runnable>()); ?? ?} }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java中區(qū)別.toString() ,(String),valueOf()方法
這篇文章主要介紹了Java中區(qū)別.toString() ,(String),valueOf()方法,需要的朋友可以參考下2017-01-01關于@PostConstruct、afterPropertiesSet和init-method的執(zhí)行順序
這篇文章主要介紹了關于@PostConstruct、afterPropertiesSet和init-method的執(zhí)行順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09springboot啟動不加載bootstrap.yml文件的問題
這篇文章主要介紹了springboot啟動不加載bootstrap.yml文件的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12