ImageUtil圖片工具: 壓縮/格式轉換等

import net.coobird.thumbnailator.Thumbnails; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sun.misc.BASE64Encoder; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /** * Created by niewj on 2018/9/10. */ public class ImgUtil {// @TODO private static Logger logger = LoggerFactory.getLogger(ImgUtil.class); public static final String JPG = "jpg"; public static final String GIF = "gif"; public static final String PNG = "png"; public static final String BMP = "bmp"; public static final int K = 1024; public static final String TYPE_UNKNOWN = "unknown"; /** * png格式圖片轉爲jpg格式 * * @param pngFile * @return */ public static File convert2Jpg(File pngFile) { // #0. 判空 if (pngFile == null || !pngFile.exists() || !pngFile.isFile() || !pngFile.canRead()) { return null; } File jpgFile = null; BufferedImage image; try { //#1. read image file image = ImageIO.read(pngFile); String parentPath = pngFile.getParent(); logger.info(ImgUtil.getPicType(new FileInputStream(pngFile))); // #2. create a blank, RGB, same width and height, and a white background BufferedImage newBufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); newBufferedImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null); // #3. create image filename long currentMillis = System.currentTimeMillis(); jpgFile = new File(parentPath, currentMillis + ".jpg"); // write to jpeg file ImageIO.write(newBufferedImage, "jpg", jpgFile); logger.info("Done"); } catch (IOException e) { e.printStackTrace(); } return jpgFile; } /** * 旋轉圖片 * * @param sourceFile 原圖片 * @param degrees 旋轉度數 * @throws IOException */ public static void rotate(File sourceFile, double degrees) throws IOException { Thumbnails.of(sourceFile) .rotate(degrees)//旋轉度數 .scale(1)//縮放比例 .toFile(sourceFile); } /** * 根據文件流判斷圖片類型 * * @param fis * @return jpg/png/gif/bmp */ public static String getPicType(FileInputStream fis) { //讀取文件的前幾個字節來判斷圖片格式 byte[] b = new byte[4]; try { fis.read(b, 0, b.length); String type = bytesToHexString(b).toUpperCase(); if (type.contains("FFD8FF")) { return JPG; } else if (type.contains("89504E47")) { return PNG; } else if (type.contains("47494638")) { return GIF; } else if (type.contains("424D")) { return BMP; } else { return TYPE_UNKNOWN; } } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } /** * byte數組轉換成16進制字符串 * * @param src * @return */ private static String bytesToHexString(byte[] src) { StringBuilder sBuilder = new StringBuilder(); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { sBuilder.append(0); } sBuilder.append(hv); } return sBuilder.toString(); } /** * @param srcFile 待壓縮的源文件 * @param desFileSize 限制的文件大小,單位Kb * @param scale 壓縮比例(0, 1.0] */ public static void commpressPicForScale(File srcFile, long desFileSize, double scale) { try { // #0. 獲取文件類型,根據內容而不是後綴名 String type = getPicType(new FileInputStream(srcFile)); // #1. 判空 if (srcFile == null || !srcFile.exists() || !srcFile.isFile()) { return; } // #2.輸出大小 long len = srcFile.length(); logger.info("源圖片:" + srcFile.getAbsolutePath() + ",大小:" + len / K + "kb"); // #3. 遞歸方法壓縮 compressImage(type, srcFile, desFileSize, scale); } catch (IOException e) { e.printStackTrace(); } } /** * 根據照片類型、精確度,上限大小,壓縮指定照片 * * @param type 照片類型:jpg/png (1. type是根據文件的內容定的,而不是名字後綴值; 2. 除了jpg,其他都按照png處理) * @param srcFile 源文件和目標文件:用的同一個 * @param desFileSize 目標值限制大小,單位Kb * @param scale 壓縮比:(0,1.0] */ private static void compressImage(String type, File srcFile, long desFileSize, double scale) { if (srcFile == null || !srcFile.exists() || !srcFile.isFile()) { logger.error("_n_待壓縮源文件不合要求:srcFile={}", srcFile); return; } try { long fileSize = srcFile.length(); // 1、判斷大小,如果小於500kb,不壓縮;如果大於等於500kb,壓縮:遞歸結束條件 if (fileSize <= desFileSize * K) { return; } // 2. jpg格式有不失真的壓縮方式;png的沒有; if (JPG.equalsIgnoreCase(type)) { logger.info("jpg_file_compress:{}", srcFile.getAbsoluteFile()); Thumbnails.of(srcFile).scale(1.0f).outputQuality(scale).toFile(srcFile); } else { // 其他圖片類型,一律按照PNG格式縮放 logger.info("png_file_compress:{}", srcFile.getAbsoluteFile()); Thumbnails.of(srcFile).scale(scale).toFile(srcFile); } // 3. 記錄下壓縮後的大小 logger.info("compressing...{}kb", srcFile.length() / K); // 4. 遞歸調用,直到大小符合要求 compressImage(type, srcFile, desFileSize, scale); } catch (IOException e) { e.printStackTrace(); } } /** * 獲取base64字符串 * * @param file * @return */ public static String getImageBase64(File file) { FileInputStream inputFile = null; byte[] buffer = new byte[0]; try { // #1. Base64輸入流 inputFile = new FileInputStream(file); buffer = new byte[(int) file.length()]; inputFile.read(buffer); inputFile.close(); } catch (IOException e) { e.printStackTrace(); } finally { // 用完後刪除文件 deleteUploadedFile(file); } return new BASE64Encoder().encode(buffer); } /** * 返回之前,把生成的文件刪除掉 * * @param file */ public static void deleteUploadedFile(File file) { // 上傳完文件之後,照片或視頻文件刪除掉 if (file != null && file.exists()) { logger.info("file deleting: ", file.getAbsolutePath()); file.delete(); } } // private static String getImageBase64(String imageId) { // File orgFile = FtpHelper.getFile(imageId); // File file = renameFile(orgFile); // ImageUtil.commpressPicForScale(file, 500, 0.9d); // 壓縮照片 // Assert.assertion(file != null, ResponseCodeEnum.INTERNAL_HANDLE_ERROR, "file is not exit"); // FileInputStream inputFile = null; // byte[] buffer = new byte[0]; // try { // inputFile = new FileInputStream(file); // buffer = new byte[(int) file.length()]; // inputFile.read(buffer); // inputFile.close(); // } catch (Exception e) { // LOGGER.error("Exception->\n {}", e); // throw new ThirdPartException(ResponseCodeEnum.INTERNAL_HANDLE_ERROR, StringMsg.fmtMsg("*")); // } finally { // 用完後刪除文件 // FtpHelper.deleteUploadedFile(file); // FtpHelper.deleteUploadedFile(orgFile); // } // return new BASE64Encoder().encode(buffer); // } public static void main(String[] args) throws FileNotFoundException { // File f2 = new File("E:\\chrome_download\\1.png"); // String type2 = ImageUtil.getPicType(new FileInputStream(f2)); // System.out.println("type2=" + type2); //// ImageUtil.compressImage(type2, f2, 500, 0.9d); // File pngFile = new File("C:\\Users\\weijun.nie\\Desktop\\thinkstats\\test2.png"); // File jpgFile = convert2Jpg(pngFile); // System.out.println(jpgFile.getAbsolutePath()); System.out.printf("%02X", 125); } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章