2011年12月26日

Java BufferedImage Low Quality 解決方法


Java中常常使用java.awt.Image庫所提供的方法進行圖像的縮放、及裁切。我們可以使用Graphics2D g = image.createGraphics()的方式得到Graphics物件,並且可以對其繪製圖案上去,但是縮放圖片時,若直接使用draw方法繪製會產生極大的問題,如題Qualtiy降低的問題,在這裡我也提供了解決的方案與原始程式碼的比照。

低畫質的縮圖程式碼
if (imgtype == BufferedImage.TYPE_BYTE_INDEXED || imgtype == BufferedImage.TYPE_BYTE_BINARY) {
     IndexColorModel icm = (IndexColorModel) source.getColorModel();
     image = new BufferedImage(width, height, imgtype, icm);

} else {
     image = new BufferedImage(width, height, imgtype);
}
Graphics g = image.getGraphics();
g.drawImage(source, 0, 0, width, height, null);

Smooth解法程式碼
ColorModel dstCM = source.getColorModel();
image = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(width, height),
         dstCM.isAlphaPremultiplied(), null);

Image scaleImage = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);
Graphics2D g = image.createGraphics();
g.drawImage(scaleImage, 0, 0, width, height, null);

縮圖品質比較 :



總表比較 :
總類
畫質
檔案大小
低畫質縮圖
7.84kb
Smooth解法
7.84kb


由此可以下個結論,直接使用draw繪圖不僅得到低畫質的圖片,也無法得到較低的檔案大小,所以以後使用縮圖就要使用Smooth解法來解。

2011年12月15日

[剪貼]取得TinyMce中的所有img標籤


var srcs = new Array();

// 取得tinyMce內容
var editContent = $('#elm1').html();

// 取得全部的img標籤
var imgs = $(editContent).find('img');


// 去重複
imgs.each(function(index){
srcs[index] = $(this).attr('src');
});

function removeRepeat(a) {
var b = [], n = a.length, i, j;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++)
if (a[i] === a[j]){j=false;break;}
if(j)b.push(a[i]);
}
//console.info((new Date).getTime()-a1)  
return b.sort(function(a,b){return a-b});
}

srcs = removeRepeat(srcs);

2011年12月6日

jQuery事件縮寫



n   blur
n   change
n   click
n   dblclick
n   error
n   focus
n   keydown
n   keypress
n   keyup
n   load
n   mousedown
n   mousemove
n   mouseout
n   mouseover
n   mouseup
n   resize
n   scroll
n   select
n   submit
n   unload

2011年12月5日

TextLayoutFramework 簡單測試



TextLayoutFramework是Flex上一功能齊全的文字編輯器。
它比RichTextEditor的功能還要齊全,可以顯示使用者電腦上的字體,排版等。


// 宣告一個TextArea對象                   
var textarea:TextArea = new TextArea();
textarea.editable = false;
textarea.percentHeight = 100;
textarea.percentWidth = 100;
                      
// TextLayout Framework編輯出來的字串
var string:String = "<flow:TextFlow direction='ltr' blockProgression='tb' whiteSpaceCollapse='preserve' xmlns:flow='http://ns.adobe.com/textLayout/2008'><flow:p><flow:span fontSize='48' fontFamily='文泉驛微米黑'><flow:tab/>測試</flow:span></flow:p><flow:p><flow:span fontSize='48' fontFamily='文泉驛微米黑'></flow:span></flow:p><flow:p><flow:span fontSize='48' fontFamily='文泉驛微米黑'></flow:span></flow:p><flow:p textAlign='center'><flow:span fontSize='48' fontFamily='華康巧風體W1'></flow:span><flow:span fontSize='48' fontFamily='Mixed'></flow:span></flow:p><flow:p><flow:span fontSize='48' fontFamily='華康巧風體W1'></flow:span></flow:p><flow:p><flow:span fontSize='48' fontFamily='文泉驛微米黑'></flow:span></flow:p><flow:p><flow:span fontSize='48' fontFamily='文泉驛微米黑'></flow:span></flow:p></flow:TextFlow>";
                      
// 宣告一個TextFlow對象
var textflow:TextFlow = new TextFlow();
textflow = TextFlowUtil.importFromString(string, WhiteSpaceCollapse.PRESERVE);
                      
// TextFlow對象設定給TextArea
textarea.textFlow = textflow;




預覽圖片 :

2011年11月20日

Java 源代碼分析系列 - ArrayList remove(Object object)


  public boolean remove(Object paramObject)
  {
    int i;
    if (paramObject == null)
      for (i = 0; i < this.size; ++i)
      {
        if (this.elementData[i] != null)
          continue;
        fastRemove(i);
        return true;
      }
    else
      for (i = 0; i < this.size; ++i)
      {
        if (!(paramObject.equals(this.elementData[i])))
          continue;
        fastRemove(i);
        return true;
      }
    return false;
  }

從代碼可以看到,remove這個方法可以刪除目標之object物件,並且可以使用它來刪除我們陣列null值,null刪除與非null刪除使用不同的判斷式來完成。
當迴圈中找到刪除目標時,會呼叫fastRemove(int)這個方法將該位置的元素刪除,fastRemove這個方法與ArrayList.remove(int)方法極為相似(參考Java - ArrayList remove(int pst)),但fastRemove如其名,式快速的刪除,因此裡面並沒有多餘的判斷(超出大小等),即沒有回傳值。代碼如下:

  private void fastRemove(int paramInt)
  {
    this.modCount += 1;
    int i = this.size - paramInt - 1;
    if (i > 0)
      System.arraycopy(this.elementData, paramInt + 1, this.elementData, paramInt, i);
    this.elementData[(--this.size)] = null;
  }


欲刪除paramInt位置











使用System.arraycopy(this.elementData, paramInt + 1, this.elementData, paramInt, i);將剩餘的陣列連接起來














 +







該方法會將欲刪除部分之後(黃色)陣列連接到原來陣列欲刪除的位置(紅色)上。











this.elementData[(--this.size)] = null;
刪除最後一個元素












Java 源代碼分析系列 - ArrayList remove(int pst)


  public E remove(int paramInt)
  {
    RangeCheck(paramInt);
    this.modCount += 1;
    Object localObject = this.elementData[paramInt];
    int i = this.size - paramInt - 1;
    if (i > 0)
      System.arraycopy(this.elementData, paramInt + 1, this.elementData, paramInt, i);
    this.elementData[(--this.size)] = null;
    return localObject;
  }


  private void RangeCheck(int paramInt)
  {
    if (paramInt < this.size)
      return;
    throw new IndexOutOfBoundsException("Index: " + paramInt + ", Size: " + this.size);
  }

首先必須先確定使用者要刪除的位置是否有超出陣列範圍,這裡使用RangeCheck(int)來檢查,若是超出範圍則會丟出IndexOutOfBoundsException例外。

如果範圍合理沒有超出範圍,這時候會算出刪除位置後所剩下的元素個數,接著使用System.arraycopy()這個方法將欲刪除元素後的所有元素加入到欲刪除的位置上。
實際操作如下面圖解:

欲刪除紅色











使用System.arraycopy(this.elementData, paramInt + 1, this.elementData, paramInt, i);將剩餘的陣列連接起來














 +







該方法會將欲刪除部分之後(黃色)陣列連接到原來陣列欲刪除的位置(紅色)上。











this.elementData[(--this.size)] = null;
刪除最後一個元素










return localObject;
回傳值為取代後,位置pst上的新元素。






下一篇 :  Java 源代碼分析系列 - ArrayList remove(Object object)



ShareThis