欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 壓縮圖片(只縮小體積,不更改圖片尺寸)的示例

 更新時(shí)間:2020年10月21日 11:18:26   作者:Marydon  
這篇文章主要介紹了java 如何壓縮圖片體積,幫助大家更好的利用Java處理圖片,應(yīng)對(duì)特殊情況,感興趣的朋友可以了解下

1.情景展示

  在調(diào)用騰訊身份證OCR接口的時(shí)候,由于要求圖片大小只能限制在1MB以?xún)?nèi),這樣,就必須使用到圖片壓縮技術(shù)

2.代碼展示

/**
 * 圖片處理工具類(lèi)
 * @explain
 * @author Marydon
 * @creationTime 2019年9月3日上午10:14:17
 * @version 1.0
 * @since
 * @email marydon20170307@163.com
 */
public class ImgUtils {
    /**
     * 壓縮圖片(通過(guò)降低圖片質(zhì)量)
     * @explain 壓縮圖片,通過(guò)壓縮圖片質(zhì)量,保持原圖大小
     * @param quality
     *       圖片質(zhì)量(0-1)
     * @return byte[]
     *      壓縮后的圖片(jpg)
     * @throws
     */
    public static byte[] compressPicByQuality(byte[] imgByte, float quality) {
        byte[] imgBytes = null;
        try {
            ByteArrayInputStream byteInput = new ByteArrayInputStream(imgByte);
            BufferedImage image = ImageIO.read(byteInput);
 
            // 如果圖片空,返回空
            if (image == null) {
                return null;
            }
            // 得到指定Format圖片的writer(迭代器)
            Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
            // 得到writer
            ImageWriter writer = (ImageWriter) iter.next();
            // 得到指定writer的輸出參數(shù)設(shè)置(ImageWriteParam )
            ImageWriteParam iwp = writer.getDefaultWriteParam();
            // 設(shè)置可否壓縮
            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
             // 設(shè)置壓縮質(zhì)量參數(shù)
            iwp.setCompressionQuality(quality);
 
            iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
 
            ColorModel colorModel = ColorModel.getRGBdefault();
            // 指定壓縮時(shí)使用的色彩模式
            iwp.setDestinationType(
                    new javax.imageio.ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));
 
            // 開(kāi)始打包圖片,寫(xiě)入byte[]
            // 取得內(nèi)存輸出流
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            IIOImage iIamge = new IIOImage(image, null, null);
 
            // 此處因?yàn)镮mageWriter中用來(lái)接收write信息的output要求必須是ImageOutput
            // 通過(guò)ImageIo中的靜態(tài)方法,得到byteArrayOutputStream的ImageOutput
            writer.setOutput(ImageIO.createImageOutputStream(byteArrayOutputStream));
            writer.write(null, iIamge, iwp);
            imgBytes = byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            System.out.println("write errro");
            e.printStackTrace();
        }
        return imgBytes;
    }
}

3.其它代碼

  import org.apache.commons.codec.binary.Base64;

String imgBase64 = "";
try {
    // 圖片大小超過(guò)1MB
    if (file.getSize()/1024 > 1024) {
        byte[] imgBytes = ImgUtils.compressPicByQuality(file.getBytes(),0.1F);
        imgBase64 = Base64.encodeBase64String(imgBytes);
    } else {
        imgBase64 = Base64.encodeBase64String(file.getBytes());
    }
} catch (IOException e1) {
    e1.printStackTrace();
}

  說(shuō)明:

  生成的base64圖片沒(méi)有圖片格式頭,即:data:image/jpeg;base64,

4.增加前端代碼

/**
 * 拍照或選擇圖片
 */
this.uploadPicture = function () {
    // js 獲取文件對(duì)象
    var fileObj = document.getElementById("file").files[0];
    if (null == fileObj) {
        alert("圖像上傳失敗,請(qǐng)重試!");
    }
    // TODO 顯示遮罩
     
    // 創(chuàng)建form表單
    var formFile = new FormData();
    //加入文件對(duì)象
    formFile.append("file", fileObj);
    // 創(chuàng)建XMLHttpRequest 對(duì)象
    var xhr = new XMLHttpRequest();
    xhr.open("post", baseUrl + "/weixin/facein/upImg3.do", true);
    xhr.onload = function () {
        var resData = JSON.parse(this.responseText)
        // 將返回?cái)?shù)據(jù)轉(zhuǎn)換成JSON對(duì)象
        var json = eval('(' + resData.data2 + ')');
        // 身份證識(shí)別成功
        if (json.ret == 0) {
            // TODO 將所需數(shù)據(jù)填充到頁(yè)面
             
        } else {
            alert("識(shí)別失敗,請(qǐng)重試!" + json.msg);
        }
        document.getElementById("file").value = "";
        // TODO 隱藏遮罩
    };
    // 調(diào)用騰訊接口
    xhr.send(formFile);
};

以上就是java 壓縮圖片(只縮小體積,不更改圖片尺寸)的示例的詳細(xì)內(nèi)容,更多關(guān)于java 壓縮圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 使用springmvc的controller層獲取到請(qǐng)求的數(shù)據(jù)方式

    使用springmvc的controller層獲取到請(qǐng)求的數(shù)據(jù)方式

    這篇文章主要介紹了使用springmvc的controller層獲取到請(qǐng)求的數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringCloud Gateway路由組件詳解

    SpringCloud Gateway路由組件詳解

    SpringCloud Gateway 是 Spring Cloud 的一個(gè)全新項(xiàng)目,它旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的統(tǒng)一的 API 路由管理方式。這篇文章主要介紹了SpringCloud Gateway網(wǎng)關(guān)作用,需要的朋友可以參考下
    2023-02-02
  • Java中判斷對(duì)象是否為空的方法的詳解

    Java中判斷對(duì)象是否為空的方法的詳解

    這篇文章主要介紹了Java中判斷對(duì)象是否為空的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn)

    SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn)

    Spring項(xiàng)目中經(jīng)常需要配置日期時(shí)間格式格式,本文主要介紹了SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • Docker?快速部署Springboot項(xiàng)目超詳細(xì)最新版

    Docker?快速部署Springboot項(xiàng)目超詳細(xì)最新版

    這篇文章主要介紹了Docker?快速部署Springboot項(xiàng)目超詳細(xì)最新版的相關(guān)資料,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • java實(shí)現(xiàn)簡(jiǎn)單租車(chē)系統(tǒng)

    java實(shí)現(xiàn)簡(jiǎn)單租車(chē)系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單租車(chē)系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • 如何使用Spring AOP的通知類(lèi)型及創(chuàng)建通知

    如何使用Spring AOP的通知類(lèi)型及創(chuàng)建通知

    這篇文章主要給大家介紹了關(guān)于如何使用Spring AOP的通知類(lèi)型及創(chuàng)建通知的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring AOP具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • java中@ConfigurationProperties失效的問(wèn)題解決

    java中@ConfigurationProperties失效的問(wèn)題解決

    在Java開(kāi)發(fā)中,使用@ConfigurationProperties注解讀取配置文件時(shí),如果配置類(lèi)中的屬性設(shè)置為static,將無(wú)法正確讀取配置值,本文就來(lái)介紹一下具體解決方法,感興趣的可以了解一下
    2024-09-09
  • SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟

    SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟

    這篇文章主要介紹了SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Prometheus pushgateway的使用詳解

    Prometheus pushgateway的使用詳解

    為了防止 pushgateway 重啟或意外掛掉,導(dǎo)致數(shù)據(jù)丟失,我們可以通過(guò) -persistence.file 和 -persistence.interval 參數(shù)將數(shù)據(jù)持久化下來(lái),接下來(lái)通過(guò)本文給大家介紹下Prometheus pushgateway的使用,感興趣的朋友一起看看吧
    2021-11-11

最新評(píng)論