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

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

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

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

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 ?圖片名稱(chēng)
?? ? * @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寫(xiě)一個(gè)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(); // 啟動(dòng)線(xiàn)程
?? ??? ??? ?*/
?? ??? ??? ?
?? ??? ??? ?/*
?? ??? ??? ? * 方法二:使用ThreadPoolExecutor創(chuàng)建線(xiàn)程池,并不提倡我們直接使用ThreadPoolExecutor
?? ??? ??? ? *?
?? ??? ??? ? */
?? ??? ??? ?/* ThreadPoolExecutor executor = new ThreadPoolExecutor(
?? ??? ??? ? ? ? ? ? ? ?5, ?//核心池的大小(即線(xiàn)程池中的線(xiàn)程數(shù)目大于這個(gè)參數(shù)時(shí),提交的任務(wù)會(huì)被放進(jìn)任務(wù)緩存隊(duì)列)
?? ??? ??? ? ? ? ? ? ? ?10, //線(xiàn)程池最大能容忍的線(xiàn)程數(shù)
?? ??? ??? ? ? ? ? ? ? ?200, //線(xiàn)程存活時(shí)間 ??
?? ??? ??? ? ? ? ? ? ? ?TimeUnit.MILLISECONDS, //參數(shù)keepAliveTime的時(shí)間單位
?? ??? ??? ? ? ? ? ? ? ?new ArrayBlockingQueue<Runnable>(5) //任務(wù)緩存隊(duì)列,用來(lái)存放等待執(zhí)行的任務(wù)
?? ??? ??? ? );
?? ??? ??? ?executor.execute(r);*/
?? ??? ??? ?/*
?? ??? ??? ? * 方法三:并不提倡我們直接使用ThreadPoolExecutor,而是使用Executors類(lèi)中提供的幾個(gè)靜態(tài)方法來(lái)創(chuàng)建線(xiàn)程池
?? ??? ??? ? * ?以下是三個(gè)靜態(tài)方法
?? ??? ??? ? * ?Executors.newCachedThreadPool(); ? ? ? ?//創(chuàng)建一個(gè)緩沖池,緩沖池容量大小為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)方法的具體實(shí)現(xiàn)
?? ? * Executors.newCachedThreadPool()
?? ? * 創(chuàng)建一個(gè)緩沖池,緩沖池容量大小為Integer.MAX_VALUE
?? ? */
?? ?public static ExecutorService newCachedThreadPool() {
?? ? ? ?return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?60L, TimeUnit.SECONDS,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new SynchronousQueue<Runnable>());
?? ?}
?? ?
?? ?/*靜態(tài)方法的具體實(shí)現(xiàn)
?? ? * Executors.newSingleThreadExecutor()?
?? ? * 創(chuàng)建容量為1的緩沖池
?? ? */
?? ?public static ExecutorService newSingleThreadExecutor() {
?? ? ? ?return ?new ThreadPoolExecutor(1, 1,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0L, TimeUnit.MILLISECONDS,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new LinkedBlockingQueue<Runnable>());
?? ?}
?? ?
?? ?/*靜態(tài)方法的具體實(shí)現(xiàn)
?? ? * Executors.newFixedThreadPool(int)?
?? ? * 創(chuàng)建固定容量大小的緩沖池
?? ? */
?? ?public static ExecutorService newFixedThreadPool(int nThreads) {
?? ? ? ?return new ThreadPoolExecutor(nThreads, nThreads,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0L, TimeUnit.MILLISECONDS,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new LinkedBlockingQueue<Runnable>());
?? ?}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JAVA設(shè)計(jì)模式零基礎(chǔ)解析之單例模式的八種方式

    JAVA設(shè)計(jì)模式零基礎(chǔ)解析之單例模式的八種方式

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

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

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

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

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

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

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

    SpringBoot中如何處理不同的類(lèi)型的POST請(qǐng)求

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

    java集合類(lèi)源碼分析之Set詳解

    下面小編就為大家?guī)?lái)一篇java集合類(lèi)源碼分析之Set詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • JavaEE線(xiàn)程安全定時(shí)器模式任務(wù)

    JavaEE線(xiàn)程安全定時(shí)器模式任務(wù)

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

    MyBatis源碼解析之Transaction事務(wù)模塊

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

    springMVC如何將controller中Model數(shù)據(jù)傳遞到j(luò)sp頁(yè)面

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

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

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

最新評(píng)論

临湘市| 威海市| 双柏县| 彰武县| 五华县| 大宁县| 清镇市| 大庆市| 若尔盖县| 万年县| 磐安县| 什邡市| 樟树市| 鹤庆县| 图木舒克市| 仁布县| 喀喇沁旗| 仙桃市| 郑州市| 商洛市| 雷州市| 上蔡县| 武强县| 焉耆| 郧西县| 北海市| 乡宁县| 偃师市| 平凉市| 河西区| 竹溪县| 防城港市| 南安市| 威宁| 手机| 河南省| 三穗县| 长顺县| 视频| 宁远县| 淳化县|