2010-07-08 60 views
5

嘿夥計們,我正在爲java功能的手機j2ME遊戲工作。我試圖擴展一個透明的PNG以下方法:自定義方法來重新調整PNG失去透明度

// method derived from a Snippet from http://snippets.dzone.com/posts/show/3257 
// scales an image according to the ratios given as parameters 

private Image rescaleImage(Image image, double XRatio, double YRatio) 
{ 
    // the old height and width 
    int sourceWidth = image.getWidth(); 
    int sourceHeight = image.getHeight(); 

    // what the new height and width should be 
    int newWidth = (int)(XRatio * sourceWidth); 
    int newHeight = (int)(YRatio * sourceHeight); 

    Image newImage = Image.createImage(newWidth, newHeight); 
    Graphics g = newImage.getGraphics(); 

    for (int y = 0; y < newHeight; y++) 
    { 
     for (int x = 0; x < newWidth; x++) 
     { 
      g.setClip(x, y, 1, 1); 
      int dx = (x * sourceWidth)/newWidth; 
      int dy = (y * sourceHeight)/newHeight; 
      g.drawImage(image, (x - dx), (y - dy), Graphics.LEFT | Graphics.TOP); 
     } 
    } 
    return Image.createImage(newImage); 
} 

它正確地縮放圖像,不幸的是我似乎失去了與該方法返回的圖像的透明度。我對這些概念相當陌生,任何幫助將不勝感激!請注意,爲了在任何支持Java的移動設備上正確顯示,重新縮放需要用代碼完成,而不是任何圖像編輯器。

在此先感謝!

回答

4

感謝大家一直在尋找解決方案,看似廣泛和未解決的問題。我設法在http://willperone.net/Code/codescaling.php

找到一個很好的解決方案您只需將createRGBImage參數中的「false」更改爲true即可。這標記了將每個像素的高位處理爲alpha值的方法。這是我的實現,與上面的原始鏈接沒有太大的變化。

XRatio和YRatio在畫布初始化時聲明爲常量,其中XRatio = this.getWidth()(當前手機的屏幕寬度)除以原始背景圖像寬度,YRatio和getHeight()/原始背景圖像高度。

// RESCALEIMAGE 
// scales an image according to the ratios given as parameters 
// derived from http://willperone.net/Code/codescaling.php 

public static Image rescaleImage(Image original, double XRatio, double YRatio) 
{ 
    // the original width and height 
    int originalWidth = original.getWidth(); 
    int originalHeight = original.getHeight(); 

    // the target width and height 
    int newWidth = (int)(XRatio * originalWidth); 
    int newHeight = (int)(YRatio * originalHeight); 

    // create and fill the pixel array from the original image 
    int[] rawInput = new int[originalHeight * originalWidth]; 
    original.getRGB(rawInput, 0, originalWidth, 0, 0, originalWidth, originalHeight); 

    // pixel array for the target image 
    int[] rawOutput = new int[newWidth*newHeight]; 

    // YD compensates for the x loop by subtracting the width back out 
    int YD = (originalHeight/newHeight) * originalWidth - originalWidth; 
    int YR = originalHeight % newHeight; 
    int XD = originalWidth/newWidth; 
    int XR = originalWidth % newWidth; 
    int outOffset= 0; 
    int inOffset= 0; 

    for (int y = newHeight, YE = 0; y > 0; y--) 
    { 
     for (int x = newWidth, XE = 0; x > 0; x--) 
     { 
      rawOutput[outOffset++] = rawInput[inOffset]; 

      inOffset += XD; 
      XE += XR; 

      if (XE >= newWidth) 
      { 
       XE -= newWidth; 
       inOffset++; 
      } 
     } 

     inOffset += YD; 
     YE += YR; 

     if (YE >= newHeight) 
     { 
      YE -= newHeight; 
      inOffset += originalWidth; 
     } 
    } 
    return Image.createRGBImage(rawOutput, newWidth, newHeight, true); 
} 
1

這可能是因爲你沒有在像素值區分字母。你可以做的是添加一個額外的例程來處理與alpha的那些,所以他們保持他們可能的alpha值。

基本上它檢查每一個像素,看看它是否有alpha,如果有,檢查它是否會在調整大小的圖像上,如果是的話,應用它的alpha,如果不是,丟棄它。

+0

謝謝Yonathan。我正在嘗試使用Image.GetRGB()和Image.createRGBImage()以及縮放算法來處理兩者之間的ARGB數組。祝我好運:) – jbabey 2010-07-08 21:03:55

+0

關於ARGB方法的更新:除了縮放比率通常不是整數,這使純像素數組操作變得更加困難之外,它的效果非常好。 – jbabey 2010-07-09 17:59:41