Java gif圖片轉(zhuǎn)換為jpg格式
下面通過代碼給大家介紹Java gif圖片轉(zhuǎn)換為jpg格式,具體代碼如下所示:
if(fileName.toLowerCase().endsWith(".gif")){//由于頭像上傳支持JPG、JPEG、BMP、GIF、PNG格式圖片.而商湯人臉設(shè)備僅支持JPG、JPEG、BMP、PNG,故如圖片為GIF格式需要轉(zhuǎn)換 fileParams.put("avatarFile", api.GifToJpg(avatar_file)); }else{ fileParams.put("avatarFile", api.getBytes(avatar_file)); }
/** * 將文件轉(zhuǎn)換為byte二進制流 * @param f * @return */ public static byte[] getBytes(File f) { try { InputStream in = new FileInputStream(f); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] b = new byte[1024]; int n; while ((n = in.read(b)) != -1) out.write(b, 0, n); in.close(); out.close(); return out.toByteArray(); } catch (IOException e) { logger.error("***請設(shè)置文件路徑***"); e.printStackTrace(); } return null; } /** * 將gif格式圖片轉(zhuǎn)換為jpg格式文件,并直接返回byte二進制流 * @param file * @return */ public static byte[] GifToJpg(File file){ BufferedImage bufferedImage; ByteArrayOutputStream out = new ByteArrayOutputStream(1024); try { // read image file bufferedImage = ImageIO.read(file); // create a blank, RGB, same width and height, and a white BufferedImage newBufferedImage = new BufferedImage( bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB); // TYPE_INT_RGB:創(chuàng)建一個RBG圖像,24位深度,成功將32位圖轉(zhuǎn)化成24位 newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0,Color.WHITE, null); // write to jpeg file ImageIO.write(newBufferedImage, "jpg",out);//轉(zhuǎn)換輸出到二進制數(shù)組流 //ImageIO.write(newBufferedImage, "jpg",new File("c:\\java.jpg"));//轉(zhuǎn)換輸出到文件 return out.toByteArray();//二進制流 } catch (IOException e) { logger.error("***GifToJpg方法報錯***"); e.printStackTrace(); } return null; }
知識點擴展:
用java將png圖片轉(zhuǎn)換成jpg格式的圖片
import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ConvertImageFile { public static void main(String[] args) { BufferedImage bufferedImage; try { //read image file bufferedImage = ImageIO.read(new File("c:\\java.png")); // create a blank, RGB, same width and height, and a white background BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB); //TYPE_INT_RGB:創(chuàng)建一個RBG圖像,24位深度,成功將32位圖轉(zhuǎn)化成24位 newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null); // write to jpeg file ImageIO.write(newBufferedImage, "jpg", new File("c:\\java.jpg")); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } }
總結(jié)
以上所述是小編給大家介紹的Java gif圖片轉(zhuǎn)換為jpg格式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
springBoot前后端分離項目中shiro的302跳轉(zhuǎn)問題
這篇文章主要介紹了springBoot前后端分離項目中shiro的302跳轉(zhuǎn)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12spring boot加載freemarker模板路徑的方法
這篇文章主要介紹了spring boot加載freemarker模板路徑的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Java實現(xiàn)經(jīng)典游戲黃金礦工的示例代碼
《黃金礦工》游戲是一個經(jīng)典的抓金子小游戲,它可以鍛煉人的反應(yīng)能力。本文將用Java實現(xiàn)這一經(jīng)典的游戲,感興趣的小伙伴可以了解一下2022-02-02SpringBoot Application注解原理及代碼詳解
這篇文章主要介紹了SpringBoot Application注解原理及代碼詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06Springboot GET和POST請求參數(shù)獲取方式小結(jié)
Spring Boot GET和POST請求參數(shù)獲取是開發(fā)人員經(jīng)常需要解決的問題,本文主要介紹了Springboot GET和POST請求參數(shù)獲取方式小結(jié),具有一定的參考價值,感興趣的可以了解一下2023-09-09一文帶你掌握Java8中Lambda表達式 函數(shù)式接口及方法構(gòu)造器數(shù)組的引用
Java 8 (又稱為 jdk 1.8) 是 Java 語言開發(fā)的一個主要版本。 Oracle 公司于 2014 年 3 月 18 日發(fā)布 Java 8 ,它支持函數(shù)式編程,新的 JavaScript 引擎,新的日期 API,新的Stream API 等2021-10-10