java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決
java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決
生成jpeg圖像這是個非常非常簡單的東西了,網(wǎng)上很多介紹是直接用com.sun.image.codec.jpeg.JPEGImageEncoder來實現(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編碼實現(xiàn),并不是標準的Java API,只在sun jvm中被支持,但在其他的jvm上,并不會被支持。
而且,雖然上面的代碼在Java 1.6,1.7上都能正常執(zhí)行,但在如果使用java 1.8,上面這個代碼會報錯:
訪問限制:由于對必需的庫 C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar 具有一定限制,因此無法訪問類型JPEGImageEncoder
所以這個方法是有局限性的。
走捷徑是不行的,還是得規(guī)規(guī)矩矩按java的規(guī)范來做,ImageIO類中提供了ImageIO.writer方法可以生成指定的格式的圖像,才是正規(guī)的實現(xiàn)方式。
但是使用ImageIO.writer方法也是有講究的。
我原先是這樣寫的,就是簡單的調(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 圖像格式名,圖像格式名錯誤則拋出異常 * @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(); }
處理了幾萬張圖像文件都沒問題,遇到一張png圖像,ImageIO.write居然返回false,拋出異常了。
究其原因,是ImageIO.wite方法在中調(diào)用的私有方法getWriter尋找合適的ImageWriter時不僅與formatName相關(guān),還是輸入的原圖有關(guān)(具體是怎么相關(guān)的,因為邏輯關(guān)系太復雜沒有深究),造成getWriter方法找不到對應(yīng)的ImageWriter。
參考網(wǎng)上別人的寫法改成這樣就沒問題了:
/** * 將{@link BufferedImage}生成formatName指定格式的圖像數(shù)據(jù) * @param source * @param formatName 圖像格式名,圖像格式名錯誤則拋出異常 * @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)建一個大小相同的BufferedImage,然后用Graphics.drawImage方法將原圖寫入新的BufferedImage對象,通過這一道轉(zhuǎn)換,抹平了,不同類型圖像格式生成的BufferedImage對象之間的區(qū)別,再調(diào)用 ImageIO.write 對新的ImageIO.write對象進行圖像處理就不會有問題了。
改進
在我的項目中圖像數(shù)據(jù)是從互聯(lián)網(wǎng)上搜索到的,遇到的圖像格式絕大部分都是jpeg,但也有少量的png,bmp等格式,對于占絕大多數(shù)的jpeg圖像來說,我最開始的方法都是有效的,而上面的這個方法多出一道工序就顯得有些多余,還浪費資源,所以又改進了上述的方法,基本的原理就是先嘗試直接ImageIO.write來生成jpeg,如果失敗,就用第二種方式。
/** * 將{@link BufferedImage}生成formatName指定格式的圖像數(shù)據(jù) * @param source * @param formatName 圖像格式名,圖像格式名錯誤則拋出異常 * @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(); }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
基于mybatis batch實現(xiàn)批量提交大量數(shù)據(jù)
這篇文章主要介紹了基于mybatis batch實現(xiàn)批量提交大量數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05MyBatis實現(xiàn)表連接查詢寫法(三種對應(yīng)關(guān)系)的方法總結(jié)
這篇文章主要介紹了MyBatis實現(xiàn)表連接查詢寫法(一對一關(guān)系、一對多關(guān)系、多對多關(guān)系)的方法,文中的示例代碼講解詳細,感興趣的可以了解一下2023-01-01SpringMVC MVC架構(gòu)原理及實現(xiàn)方法詳解
這篇文章主要介紹了SpringMVC MVC架構(gòu)原理及實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用
這篇文章主要介紹了詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用的相關(guān)資料,需要的朋友可以參考下2017-02-02