欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決

 更新時間:2017年03月24日 11:03:24   作者:10km  
這篇文章主要介紹了java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決的相關(guān)資料,需要的朋友可以參考下

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ù)

    這篇文章主要介紹了基于mybatis batch實現(xiàn)批量提交大量數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • Tomcat 實現(xiàn)WebSocket詳細介紹

    Tomcat 實現(xiàn)WebSocket詳細介紹

    這篇文章主要介紹了Tomcat 如何實現(xiàn)WebSocket的相關(guān)資料,對WebSocket協(xié)議通信的過程進行了詳細介紹,需要的朋友可以參考下
    2016-12-12
  • Java 動態(tài)編譯在項目中的實踐分享

    Java 動態(tài)編譯在項目中的實踐分享

    在 Java 中,動態(tài)編譯是指在運行時動態(tài)地編譯 Java 源代碼,生成字節(jié)碼,并加載到 JVM 中執(zhí)行,動態(tài)編譯可以用于實現(xiàn)動態(tài)代碼生成、動態(tài)加載、插件化等功能,本文將給大家分享一下Java 動態(tài)編譯在項目中的實踐,感興趣的同學跟著小編一起來看看吧
    2023-07-07
  • MyBatis實現(xiàn)表連接查詢寫法(三種對應(yīng)關(guān)系)的方法總結(jié)

    MyBatis實現(xiàn)表連接查詢寫法(三種對應(yīng)關(guān)系)的方法總結(jié)

    這篇文章主要介紹了MyBatis實現(xiàn)表連接查詢寫法(一對一關(guān)系、一對多關(guān)系、多對多關(guān)系)的方法,文中的示例代碼講解詳細,感興趣的可以了解一下
    2023-01-01
  • 利用JavaMail發(fā)送HTML模板郵件

    利用JavaMail發(fā)送HTML模板郵件

    這篇文章主要為大家詳細介紹了利用JavaMail發(fā)送HTML模板郵件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Java實現(xiàn)記事本功能

    Java實現(xiàn)記事本功能

    這篇文章主要為大家詳細介紹了Java實現(xiàn)記事本功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • JPA之QueryDSL-JPA使用指南

    JPA之QueryDSL-JPA使用指南

    Springdata-JPA是對JPA使用的封裝,Querydsl-JPA也是基于各種ORM之上的一個通用查詢框架,使用它的API類庫可以寫出Java代碼的sql,下面就來介紹一下JPA之QueryDSL-JPA使用指南
    2023-11-11
  • SpringMVC MVC架構(gòu)原理及實現(xiàn)方法詳解

    SpringMVC MVC架構(gòu)原理及實現(xiàn)方法詳解

    這篇文章主要介紹了SpringMVC MVC架構(gòu)原理及實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • 淺談Servlet開發(fā)技術(shù)基礎(chǔ)

    淺談Servlet開發(fā)技術(shù)基礎(chǔ)

    這篇文章主要介紹了淺談Servlet開發(fā)技術(shù)基礎(chǔ),具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用

    詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用

    這篇文章主要介紹了詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用的相關(guān)資料,需要的朋友可以參考下
    2017-02-02

最新評論