[轉]J2ME中對Image的縮放

 

a sniplet from the article "Taking Pictures with MMAPI"<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

http://developers.sun.com/techtopics/mobility/midp/articles/picture/

 [email protected]

創建縮略圖

MIDP2.0中可以對圖片中的像素進行操作,在MIDP1.0中則不然。本例用Graphics.setClip()實現每一次對一個像素進行繪製。

private Image createThumbnail(Image image) {

  int sourceWidth = image.getWidth();

  int sourceHeight = image.getHeight();

 

  int thumbWidth = 64;

  int thumbHeight = -1;

 

  if (thumbHeight == -1)

    thumbHeight = thumbWidth * sourceHeight / sourceWidth;

 

  Image thumb = Image.createImage(thumbWidth, thumbHeight);

  Graphics g = thumb.getGraphics();

 

  for (int y = 0; y < thumbHeight; y++) {

    for (int x = 0; x < thumbWidth; x++) {

      g.setClip(x, y, 1, 1);

      int dx = x * sourceWidth / thumbWidth;

      int dy = y * sourceHeight / thumbHeight;

      g.drawImage(image, x - dx, y - dy,

          Graphics.LEFT | Graphics.TOP);

    }

  }

 

  Image immutableThumb = Image.createImage(thumb);

 

  return immutableThumb;

}

發佈了35 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章