1. 程式人生 > >Jav檔案壓縮-InputStream轉化為base64-Base64解碼並生成圖片

Jav檔案壓縮-InputStream轉化為base64-Base64解碼並生成圖片

 Jav檔案壓縮-InputStream轉化為base64-Base64解碼並生成圖片

 

直接上原始碼,解釋見文章尾部

  1 package com.hs.common.util.imgecode;
  2 
  3 import com.hs.common.util.Logger;
  4 import net.coobird.thumbnailator.Thumbnails;
  5 import org.apache.commons.codec.binary.Base64;
  6 import sun.misc.BASE64Decoder;
  7 
  8
import javax.imageio.ImageIO; 9 import java.awt.image.BufferedImage; 10 import java.io.*; 11 12 13 public class ImageEncodeUtil { 14 private final static Logger logger = Logger.getLogger(ImageEncodeUtil.class); 15 16 //1-壓縮圖片 17 public static InputStream compressFile(InputStream input) throws
IOException { 18 //1-壓縮圖片 19 BufferedImage bufImg = ImageIO.read(input);// 把圖片讀入到記憶體中 20 bufImg = Thumbnails.of(bufImg).width(100).keepAspectRatio(true).outputQuality(0.2f).asBufferedImage();//壓縮:寬度100px,長度自適應;質量壓縮到0.1 21 ByteArrayOutputStream bos = new ByteArrayOutputStream();//
儲存圖片檔案byte陣列 22 ImageIO.write(bufImg, "jpg", bos); // 圖片寫入到 ImageOutputStream 23 input = new ByteArrayInputStream(bos.toByteArray()); 24 int available = input.available(); 25 //2-如果大小超過50KB,繼續壓縮 26 if(available > 50000){ 27 compressFile(input); 28 } 29 return input; 30 31 } 32 //2-InputStream轉化為base64 33 public static String getBase64FromInputStream(InputStream in) { 34 // 將圖片檔案轉化為位元組陣列字串,並對其進行Base64編碼處理 35 byte[] data = null; 36 // 讀取圖片位元組陣列 37 try { 38 ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); 39 byte[] buff = new byte[100]; 40 int rc = 0; 41 while ((rc = in.read(buff, 0, 100)) > 0) { 42 swapStream.write(buff, 0, rc); 43 } 44 data = swapStream.toByteArray(); 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } finally { 48 if (in != null) { 49 try { 50 in.close(); 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 } 55 } 56 String str = new String(Base64.encodeBase64(data)); 57 System.out.println( "str length: " + str.length() + " str: " + str); 58 return str; 59 } 60 61 //3-Base64解碼並生成圖片 62 public static boolean GenerateImage(String base64str,String savepath) { //對位元組陣列字串進行Base64解碼並生成圖片 63 if (base64str == null) //影象資料為空 64 return false; 65 BASE64Decoder decoder = new BASE64Decoder(); 66 try { 67 //Base64解碼 68 byte[] b = decoder.decodeBuffer(base64str); 69 // System.out.println("解碼完成"); 70 for (int i = 0; i < b.length; ++i) { 71 if (b[i] < 0) {//調整異常資料(這一步很重要) 72 b[i] += 256; 73 } 74 } 75 //生成jpeg圖片 76 OutputStream out = new FileOutputStream(savepath); 77 out.write(b); 78 out.flush(); 79 out.close(); 80 return true; 81 } catch (Exception e) { 82 return false; 83 } 84 } 85 86 public static void main(String[] args) { 87 try { 88 File file = new File("C:\\Users\\tyj\\Desktop\\01.jpg"); 89 FileInputStream fileInputStream = new FileInputStream(file); 90 InputStream inputStream = compressFile(fileInputStream); 91 String base64FromInputStream = getBase64FromInputStream(inputStream); 92 GenerateImage(base64FromInputStream,"C:\\\\Users\\\\tyj\\\\Desktop\\\\113001.jpg"); 93 InputStream is =new ByteArrayInputStream(base64FromInputStream.getBytes("UTF-8")); 94 System.out.println("compress success"); 95 } catch (IOException e) { 96 // TODO Auto-generated catch block 97 e.printStackTrace(); 98 } 99 } 100 101 }

 1.圖片壓縮使用谷歌thumbnailator,詳情可參考:https://www.cnblogs.com/ken-jl/p/8847567.html

        <!-- 谷歌圖片壓縮 -->
        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.8</version>
        </dependency>

2-InputStream轉化為base64

3-Base64解碼並生成圖片,注意其中的標紅部分,調整異常資料(這一步很重要)