新聞中心
這篇文章主要介紹了怎么在Java項目中將gif圖片轉(zhuǎn)換為jpg格式,此處給大家介紹的非常詳細,對大家的學習或工作具有一定的參考價值,需要的朋友可以參考下:
成都一家集口碑和實力的網(wǎng)站建設服務商,擁有專業(yè)的企業(yè)建站團隊和靠譜的建站技術(shù),10多年企業(yè)及個人網(wǎng)站建設經(jīng)驗 ,為成都上1000+客戶提供網(wǎng)頁設計制作,網(wǎng)站開發(fā),企業(yè)網(wǎng)站制作建設等服務,包括成都營銷型網(wǎng)站建設,品牌網(wǎng)站設計,同時也為不同行業(yè)的客戶提供做網(wǎng)站、成都網(wǎng)站制作的服務,包括成都電商型網(wǎng)站制作建設,裝修行業(yè)網(wǎng)站制作建設,傳統(tǒng)機械行業(yè)網(wǎng)站建設,傳統(tǒng)農(nóng)業(yè)行業(yè)網(wǎng)站制作建設。在成都做網(wǎng)站,選網(wǎng)站制作建設服務商就選創(chuàng)新互聯(lián)公司。
if(fileName.toLowerCase().endsWith(".gif")){//由于頭像上傳支持JPG、JPEG、BMP、GIF、PNG格式圖片.而商湯人臉設備僅支持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("***請設置文件路徑***"); 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(); } } }
到此這篇關于怎么在Java項目中將gif圖片轉(zhuǎn)換為jpg格式的文章就介紹到這了,更多相關的內(nèi)容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持創(chuàng)新互聯(lián)!
文章標題:怎么在Java項目中將gif圖片轉(zhuǎn)換為jpg格式
標題網(wǎng)址:http://www.ef60e0e.cn/article/pdcghs.html