java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問(wèn)題總結(jié)及解決
java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問(wèn)題總結(jié)及解決
生成jpeg圖像這是個(gè)非常非常簡(jiǎn)單的東西了,網(wǎng)上很多介紹是直接用com.sun.image.codec.jpeg.JPEGImageEncoder來(lái)實(shí)現(xiàn),如下:
/** * 將原圖壓縮生成jpeg格式的數(shù)據(jù) * @param source * @return */ public static byte[] wirteJPEGBytes(BufferedImage source){ if(null==source) throw new NullPointerException(); ByteArrayOutputStream output = new ByteArrayOutputStream(); JPEGImageEncoder jencoder = JPEGCodec.createJPEGEncoder(output); JPEGEncodeParam param = jencoder.getDefaultJPEGEncodeParam(source); param.setQuality(0.75f, true); jencoder.setJPEGEncodeParam(param); try { jencoder.encode(source); } catch (ImageFormatException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } return output.toByteArray(); }
JPEGImageEncoder只是sun的jpeg編碼實(shí)現(xiàn),并不是標(biāo)準(zhǔn)的Java API,只在sun jvm中被支持,但在其他的jvm上,并不會(huì)被支持。
而且,雖然上面的代碼在Java 1.6,1.7上都能正常執(zhí)行,但在如果使用java 1.8,上面這個(gè)代碼會(huì)報(bào)錯(cuò):
訪問(wèn)限制:由于對(duì)必需的庫(kù) C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar 具有一定限制,因此無(wú)法訪問(wèn)類型JPEGImageEncoder
所以這個(gè)方法是有局限性的。
走捷徑是不行的,還是得規(guī)規(guī)矩矩按java的規(guī)范來(lái)做,ImageIO類中提供了ImageIO.writer方法可以生成指定的格式的圖像,才是正規(guī)的實(shí)現(xiàn)方式。
但是使用ImageIO.writer方法也是有講究的。
我原先是這樣寫的,就是簡(jiǎn)單的調(diào)用ImageIO.writer方法生成jpeg數(shù)據(jù):
/** * 將原圖壓縮生成jpeg格式的數(shù)據(jù) * @param source * @return * @see #wirteBytes(BufferedImage, String) */ public static byte[] wirteJPEGBytes(BufferedImage source){ return wirteBytes(source,"JPEG"); } /** * 將原圖壓縮生成jpeg格式的數(shù)據(jù) * @param source * @return * @see #wirteBytes(BufferedImage, String) */ public static byte[] wirteJPEGBytes(BufferedImage source){ return wirteBytes(source,"JPEG"); } /** * 將{@link BufferedImage}生成formatName指定格式的圖像數(shù)據(jù) * @param source * @param formatName 圖像格式名,圖像格式名錯(cuò)誤則拋出異常 * @return */ public static byte[] wirteBytes(BufferedImage source,String formatName){ Assert.notNull(source, "source"); Assert.notEmpty(formatName, "formatName"); ByteArrayOutputStream output = new ByteArrayOutputStream(); try { if(!ImageIO.write(source, formatName.toLowerCase(), output)) // 返回false則拋出異常 throw new IllegalArgumentException(String.format("not found writer for '%s'",formatName)); } catch (IOException e) { throw new RuntimeException(e); } return output.toByteArray(); }
處理了幾萬(wàn)張圖像文件都沒問(wèn)題,遇到一張png圖像,ImageIO.write居然返回false,拋出異常了。
究其原因,是ImageIO.wite方法在中調(diào)用的私有方法getWriter尋找合適的ImageWriter時(shí)不僅與formatName相關(guān),還是輸入的原圖有關(guān)(具體是怎么相關(guān)的,因?yàn)檫壿嬯P(guān)系太復(fù)雜沒有深究),造成getWriter方法找不到對(duì)應(yīng)的ImageWriter。
參考網(wǎng)上別人的寫法改成這樣就沒問(wèn)題了:
/** * 將{@link BufferedImage}生成formatName指定格式的圖像數(shù)據(jù) * @param source * @param formatName 圖像格式名,圖像格式名錯(cuò)誤則拋出異常 * @return */ public static byte[] wirteBytes(BufferedImage source,String formatName){ Assert.notNull(source, "source"); Assert.notEmpty(formatName, "formatName"); ByteArrayOutputStream output = new ByteArrayOutputStream(); BufferedImage newBufferedImage = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g = newBufferedImage.createGraphics(); try { g.drawImage(source, 0, 0,null); if(!ImageIO.write(newBufferedImage, formatName, output)) throw new IllegalArgumentException(String.format("not found writer for '%s'",formatName)); } catch (IOException e) { throw new RuntimeException(e); }finally{ g.dispose(); } return output.toByteArray(); }
基本的思路就是重創(chuàng)建一個(gè)大小相同的BufferedImage,然后用Graphics.drawImage方法將原圖寫入新的BufferedImage對(duì)象,通過(guò)這一道轉(zhuǎn)換,抹平了,不同類型圖像格式生成的BufferedImage對(duì)象之間的區(qū)別,再調(diào)用 ImageIO.write 對(duì)新的ImageIO.write對(duì)象進(jìn)行圖像處理就不會(huì)有問(wèn)題了。
改進(jìn)
在我的項(xiàng)目中圖像數(shù)據(jù)是從互聯(lián)網(wǎng)上搜索到的,遇到的圖像格式絕大部分都是jpeg,但也有少量的png,bmp等格式,對(duì)于占絕大多數(shù)的jpeg圖像來(lái)說(shuō),我最開始的方法都是有效的,而上面的這個(gè)方法多出一道工序就顯得有些多余,還浪費(fèi)資源,所以又改進(jìn)了上述的方法,基本的原理就是先嘗試直接ImageIO.write來(lái)生成jpeg,如果失敗,就用第二種方式。
/** * 將{@link BufferedImage}生成formatName指定格式的圖像數(shù)據(jù) * @param source * @param formatName 圖像格式名,圖像格式名錯(cuò)誤則拋出異常 * @return */ public static byte[] wirteBytes(BufferedImage source,String formatName){ Assert.notNull(source, "source"); Assert.notEmpty(formatName, "formatName"); ByteArrayOutputStream output = new ByteArrayOutputStream(); Graphics2D g = null; try { for(BufferedImage s=source;!ImageIO.write(s, formatName, output);){ if(null!=g) throw new IllegalArgumentException(String.format("not found writer for '%s'",formatName)); s = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_RGB); g = s.createGraphics(); g.drawImage(source, 0, 0,null); } } catch (IOException e) { throw new RuntimeException(e); } finally { if (null != g) g.dispose(); } return output.toByteArray(); }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
基于mybatis batch實(shí)現(xiàn)批量提交大量數(shù)據(jù)
這篇文章主要介紹了基于mybatis batch實(shí)現(xiàn)批量提交大量數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Tomcat 實(shí)現(xiàn)WebSocket詳細(xì)介紹
這篇文章主要介紹了Tomcat 如何實(shí)現(xiàn)WebSocket的相關(guān)資料,對(duì)WebSocket協(xié)議通信的過(guò)程進(jìn)行了詳細(xì)介紹,需要的朋友可以參考下2016-12-12Java 動(dòng)態(tài)編譯在項(xiàng)目中的實(shí)踐分享
在 Java 中,動(dòng)態(tài)編譯是指在運(yùn)行時(shí)動(dòng)態(tài)地編譯 Java 源代碼,生成字節(jié)碼,并加載到 JVM 中執(zhí)行,動(dòng)態(tài)編譯可以用于實(shí)現(xiàn)動(dòng)態(tài)代碼生成、動(dòng)態(tài)加載、插件化等功能,本文將給大家分享一下Java 動(dòng)態(tài)編譯在項(xiàng)目中的實(shí)踐,感興趣的同學(xué)跟著小編一起來(lái)看看吧2023-07-07MyBatis實(shí)現(xiàn)表連接查詢寫法(三種對(duì)應(yīng)關(guān)系)的方法總結(jié)
這篇文章主要介紹了MyBatis實(shí)現(xiàn)表連接查詢寫法(一對(duì)一關(guān)系、一對(duì)多關(guān)系、多對(duì)多關(guān)系)的方法,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-01-01SpringMVC MVC架構(gòu)原理及實(shí)現(xiàn)方法詳解
這篇文章主要介紹了SpringMVC MVC架構(gòu)原理及實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用
這篇文章主要介紹了詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用的相關(guān)資料,需要的朋友可以參考下2017-02-02