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>