溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java怎么實現(xiàn)按比例縮小圖片

發(fā)布時間:2022-04-22 13:44:03 來源:億速云 閱讀:305 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Java怎么實現(xiàn)按比例縮小圖片”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java怎么實現(xiàn)按比例縮小圖片”吧!

使用spring注解上傳文件@RequestParam(value="", required=true),required為true時不能傳入空值,為false時反之;UploadFile.getOriginalFilename()獲取上傳的文件的文件名;System.nanoTime()返回當(dāng)前時間的納秒,用做文件名;FileUtils.writeByteArrayToFile()上傳文件到本地目錄;使用BufferedImage將圖片加載到內(nèi)存中,然后對圖片進(jìn)行修改如大小變換、圖片變灰、設(shè)置透明等。 

效果圖:

Java怎么實現(xiàn)按比例縮小圖片

HTML:

<div class="form-group">
    <label class="col-lg-1 col-md-1 col-sm-2 col-xs-2 label-size"><span class="c-red">*</span>頭像:</label>
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 img-padding-zero">
        <div>
            <span id="Upload_ph" class="img-hide">
                <input type="file" title="" id="UploadFile" name="UploadFile" multiple="" class="img-input">
            </span>
            <span>
                <img alt="" src="${ctx}/UserManages/reveal_photo.do?file=tree.png">
                <a class="a-word" >上傳圖片</a>&nbsp;&nbsp;
                <a id="showApellation" class="a-word" ></a>                
            </span>
        </div>
        <div>
            <img src="${ctx}/UserManages/reveal_photo.do?file=noImg.png" class="img-padding" width="198" height="198" alt="" id="imgOperPhoto1" onload="AutoResizeImage(198, 198, this)" title="用于頭像顯示">
            <img src="${ctx}/UserManages/reveal_photo.do?file=noImg_tab.png" class="img-padding" width="28" height="40" alt="" id="imgOperPhoto3" title="用于列表顯示">
        </div>
    </div>
</div>

form表單提交到j(luò)ava:

@SuppressWarnings("unused")
@RequestMapping(value="/IntOpers")
private void IntOper(@RequestParam(value="UploadFile", required=true) MultipartFile UploadFile, PrintWriter pw, HttpServletRequest request) throws IOException {
    String StrReturn = "error", FileName = "", ext = "";
    long time = 0;
    if (!UploadFile.isEmpty()) {
        if (UploadFile != null) {
            String fullName = UploadFile.getOriginalFilename();
            ext = fullName.substring(fullName.lastIndexOf("."));//獲取擴(kuò)展名稱
            time = System.nanoTime();
            FileName = time + ext;//設(shè)置文件保存的名稱
            
            //原圖
            String upDir = "F:/java/upload-ssm";//文件上傳路徑
            FileUtils.writeByteArrayToFile(new File(upDir, FileName), UploadFile.getBytes());
            //縮小圖片
            Image srcFile = ImageIO.read(new File(upDir + "/" + FileName));
            BufferedImage tag = new BufferedImage(28, 40, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(srcFile, 0, 0, 28, 40, null);
 
            String FileName_tab = time + "_tab" + ext;//設(shè)置文件保存的名稱
            FileOutputStream out = new FileOutputStream(upDir + "/" + FileName_tab);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
            
            jep.setQuality(1f, true);
            encoder.encode(tag, jep);
            out.close();
            StrReturn = "success";
        }
    }
    pw.write(StrReturn);
}

感謝各位的閱讀,以上就是“Java怎么實現(xiàn)按比例縮小圖片”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Java怎么實現(xiàn)按比例縮小圖片這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI