簡體   English   中英

黑莓 - 如何調整圖像大小?

[英]Blackberry - how to resize image?

我想知道我們是否可以調整圖像大小。 假設我們想要在黑莓屏幕上繪制尺寸為100 x 100的200x200實際尺寸的圖像。

謝謝

您可以使用EncodedImage.scaleImage32()方法完成此操作。 您需要為其提供要擴展寬度和高度的因子(作為Fixed32 )。

下面是一些示例代碼,它使用RIM的Fixed32類,通過將原始圖像大小除以所需大小來確定寬度和高度的比例因子。

public static EncodedImage resizeImage(EncodedImage image, int newWidth, int newHeight) {
    int scaleFactorX = Fixed32.div(Fixed32.toFP(image.getWidth()), Fixed32.toFP(newWidth));
    int scaleFactorY = Fixed32.div(Fixed32.toFP(image.getHeight()), Fixed32.toFP(newHeight));
    return image.scaleImage32(scaleFactorX, scaleFactorY);
}

如果你有幸成為OS 5.0的開發人員,Marc發布了一個新API的鏈接,它比我上面描述的更清晰,更通用。 例如:

public static Bitmap resizeImage(Bitmap originalImage, int newWidth, int newHeight) {
    Bitmap newImage = new Bitmap(newWidth, newHeight);
    originalImage.scaleInto(newImage, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
    return newImage;
}

(當然,您可以根據需要替換過濾器/縮放選項。)

我不是黑莓程序員,但我相信其中一些鏈接會幫助你:

圖像調整文章
調整Blackberry上的位圖大小
黑莓圖像縮放問題

請記住,BlackBerry完成的默認圖像縮放非常原始,通常看起來不太好。 如果您正在構建5.0,那么有一個新的API可以使用雙線性或Lanczos等過濾器進行更好的圖像縮放。

對於BlackBerry JDE 5.0或更高版本,您可以使用scaleInto API。

in this there is two bitmap.temp is holding the old bitmap.In this method you just pass
bitmap ,width,height.it return new bitmap of your choice.

  Bitmap ImgResizer(Bitmap bitmap , int width , int height){
    Bitmap temp=new Bitmap(width,height);
    Bitmap resized_Bitmap = bitmap;
    temp.createAlpha(Bitmap.HOURGLASS);
    resized_Bitmap.scaleInto(temp , Bitmap.FILTER_LANCZOS);
    return temp;
}

我在Blackberry應用程序開發中發布了這個新手的答案。 下面的代碼用於處理來自URL的位圖圖像並調整它們的大小而不是寬高比:

   public static Bitmap imageFromServer(String url)
{
Bitmap bitmp = null;
try{
HttpConnection fcon = (HttpConnection)Connector.open(url);
int rc = fcon.getResponseCode();
if(rc!=HttpConnection.HTTP_OK)
{
    throw new IOException("Http Response Code : " + rc);            
}
InputStream httpInput = fcon.openDataInputStream();
InputStream inp = httpInput;
byte[] b = IOUtilities.streamToBytes(inp);
EncodedImage img = EncodedImage.createEncodedImage(b, 0, b.length);
bitmp = resizeImage(img.getBitmap(), 100, 100);
}
catch(Exception e)
{
Dialog.alert("Exception : " + e.getMessage());          
}
return bitmp;
}

public static Bitmap resizeImage(Bitmap originalImg, int newWidth, int newHeight)
{
    Bitmap scaledImage = new Bitmap(newWidth, newHeight);
    originalImg.scaleInto(scaledImage, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT);
    return scaledImage;
}

方法resizeImage在方法imageFromServer(String url)中調用。 1)使用EncodedImage img處理來自服務器的圖像。 2)位圖bitmp = resizeImage(img.getBitmap(),100,100); 參數傳遞給resizeImage(),resizeImage()的返回值設置為Bitmap bitmp。

這是函數或者您可以說調整圖像大小的方法,可以根據需要使用它:

int olddWidth;
int olddHeight;
int dispplayWidth;
int dispplayHeight;

EncodedImage ei2 = EncodedImage.getEncodedImageResource("add2.png");
olddWidth = ei2.getWidth();
olddHeight = ei2.getHeight();
dispplayWidth = 40;\\here pass the width u want in pixels
dispplayHeight = 80;\\here pass the height u want in pixels again

int numeerator = net.rim.device.api.math.Fixed32.toFP(olddWidth);
int denoominator = net.rim.device.api.math.Fixed32.toFP(dispplayWidth);
int widtthScale = net.rim.device.api.math.Fixed32.div(numeerator, denoominator);
numeerator = net.rim.device.api.math.Fixed32.toFP(olddHeight);
denoominator = net.rim.device.api.math.Fixed32.toFP(dispplayHeight);
int heighhtScale = net.rim.device.api.math.Fixed32.div(numeerator, denoominator);
EncodedImage newEi2 = ei2.scaleImage32(widtthScale, heighhtScale); 
Bitmap _add =newEi2.getBitmap();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM