黑莓 - 如何调整图像大小?

时间:2009-11-20 11:02:31

标签: graphics blackberry drawing custom-controls image-scaling

我想知道我们是否可以调整图片大小。假设我们想要在黑莓屏幕上绘制尺寸为100 x 100的200x200实际尺寸的图像。

由于

8 个答案:

答案 0 :(得分:10)

您可以使用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发布了一个new APIs的链接,它比我上面描述的更清晰,更通用。例如:

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;
}

(当然,您可以根据需要替换过滤器/缩放选项。)

答案 1 :(得分:7)

答案 2 :(得分:3)

我不是黑莓程序员,但我相信其中一些链接会帮助你:

Image Resizing Article
Resizing a Bitmap on the Blackberry
Blackberry Image Scaling Question

答案 3 :(得分:3)

请记住,BlackBerry完成的默认图像缩放非常原始,通常看起来不太好。如果您使用5.0进行构建,则使用双线性或Lanczos等过滤器可以new API进行更好的图像缩放。

答案 4 :(得分:2)

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

答案 5 :(得分:1)

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;
}

答案 6 :(得分:0)

这是函数或者您可以说调整图像大小的方法,可以根据需要使用它:

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();

答案 7 :(得分:0)

我在黑莓应用程序开发中发布了这个新手的答案。下面的代码用于处理来自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。