繁体   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