最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java項目實現(xiàn)圖片等比縮放

 更新時間:2022年04月22日 14:51:53   作者:xiegongmiao  
這篇文章主要為大家詳細介紹了java項目實現(xiàn)圖片等比縮放,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java項目實現(xiàn)圖片等比縮放的具體代碼,供大家參考,具體內容如下

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;
?
?? ?/**
?? ? * 初始化參數(shù)
?? ? * @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, ?//核心池的大?。淳€程池中的線程數(shù)目大于這個參數(shù)時,提交的任務會被放進任務緩存隊列)
?? ??? ??? ? ? ? ? ? ? ?10, //線程池最大能容忍的線程數(shù)
?? ??? ??? ? ? ? ? ? ? ?200, //線程存活時間 ??
?? ??? ??? ? ? ? ? ? ? ?TimeUnit.MILLISECONDS, //參數(shù)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)方法的具體實現(xiàn)
?? ? * 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)方法的具體實現(xiàn)
?? ? * Executors.newSingleThreadExecutor()?
?? ? * 創(chuàng)建容量為1的緩沖池
?? ? */
?? ?public static ExecutorService newSingleThreadExecutor() {
?? ? ? ?return ?new ThreadPoolExecutor(1, 1,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0L, TimeUnit.MILLISECONDS,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new LinkedBlockingQueue<Runnable>());
?? ?}
?? ?
?? ?/*靜態(tài)方法的具體實現(xiàn)
?? ? * Executors.newFixedThreadPool(int)?
?? ? * 創(chuàng)建固定容量大小的緩沖池
?? ? */
?? ?public static ExecutorService newFixedThreadPool(int nThreads) {
?? ? ? ?return new ThreadPoolExecutor(nThreads, nThreads,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0L, TimeUnit.MILLISECONDS,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new LinkedBlockingQueue<Runnable>());
?? ?}
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • JAVA設計模式零基礎解析之單例模式的八種方式

    JAVA設計模式零基礎解析之單例模式的八種方式

    設計模式(Design pattern)是一套被反復使用、多數(shù)人知曉的、經過分類編目的、代碼設計經驗的總結。使用設計模式是為了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性
    2021-10-10
  • SpringBoot 2 統(tǒng)一異常處理過程解析

    SpringBoot 2 統(tǒng)一異常處理過程解析

    這篇文章主要介紹了SpringBoot 2 統(tǒng)一異常處理過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • SpringBoot JPA實現(xiàn)增刪改查、分頁、排序、事務操作等功能示例

    SpringBoot JPA實現(xiàn)增刪改查、分頁、排序、事務操作等功能示例

    本篇文章主要介紹了SpringBoot JPA實現(xiàn)增刪改查、分頁、排序、事務操作等功能示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Spring @CrossOrigin 注解原理實現(xiàn)

    Spring @CrossOrigin 注解原理實現(xiàn)

    這篇文章主要介紹了Spring @CrossOrigin 注解原理實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • SpringBoot中如何處理不同的類型的POST請求

    SpringBoot中如何處理不同的類型的POST請求

    在Web開發(fā)中,POST請求是非常常見的,用于向服務器提交數(shù)據,根據數(shù)據的編碼方式,POST請求可以分為form-data、x-www-form-urlencoded和raw三種類型,本文將介紹這三種請求方式的區(qū)別,并展示如何在Spring Boot中編寫代碼來處理它們,需要的朋友可以參考下
    2024-08-08
  • java集合類源碼分析之Set詳解

    java集合類源碼分析之Set詳解

    下面小編就為大家?guī)硪黄猨ava集合類源碼分析之Set詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • JavaEE線程安全定時器模式任務

    JavaEE線程安全定時器模式任務

    這篇文章主要介紹了JavaEE線程安全定時器模式任務,定時器模式像是一個鬧鐘定時,在一定時間之后被喚醒并執(zhí)行某個之前設定好的任務,感興趣的小伙伴可以參考一下
    2022-06-06
  • MyBatis源碼解析之Transaction事務模塊

    MyBatis源碼解析之Transaction事務模塊

    這篇文章主要介紹了MyBatis源碼解析之Transaction事務模塊,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • springMVC如何將controller中Model數(shù)據傳遞到jsp頁面

    springMVC如何將controller中Model數(shù)據傳遞到jsp頁面

    本篇文章主要介紹了springMVC如何將controller中Model數(shù)據傳遞到jsp頁面,具有一定的參考價值,有興趣的可以了解一下
    2017-07-07
  • springmvc下實現(xiàn)登錄驗證碼功能示例

    springmvc下實現(xiàn)登錄驗證碼功能示例

    本篇文章主要介紹了springmvc下實現(xiàn)登錄驗證碼功能示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02

最新評論

临颍县| 五华县| 五原县| 微山县| 垦利县| 灌南县| 泸水县| 延津县| 鹤庆县| 阿鲁科尔沁旗| 招远市| 昌吉市| 宿迁市| 本溪市| 六枝特区| 高雄县| 朝阳市| 泗洪县| 朝阳市| 吉林市| 临漳县| 牟定县| 石河子市| 凤城市| 开封市| 金平| 呼伦贝尔市| 溧水县| 万宁市| 宁化县| 郸城县| 宁河县| 清原| 土默特右旗| 迁安市| 修水县| 洪洞县| 洮南市| 托克托县| 定陶县| 龙陵县|