简体   繁体   English

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

[英]Blackberry - how to resize image?

I wanted to know if we can resize an image. 我想知道我们是否可以调整图像大小。 Suppose if we want to draw an image of 200x200 actual size with a size of 100 x 100 size on our blackberry screen. 假设我们想要在黑莓屏幕上绘制尺寸为100 x 100的200x200实际尺寸的图像。

Thanks 谢谢

You can do this pretty simply using the EncodedImage.scaleImage32() method. 您可以使用EncodedImage.scaleImage32()方法完成此操作。 You'll need to provide it with the factors by which you want to scale the width and height (as a Fixed32 ). 您需要为其提供要扩展宽度和高度的因子(作为Fixed32 )。

Here's some sample code which determines the scale factor for the width and height by dividing the original image size by the desired size, using RIM's Fixed32 class. 下面是一些示例代码,它使用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);
}

If you're lucky enough to be developer for OS 5.0, Marc posted a link to the new APIs that are a lot clearer and more versatile than the one I described above. 如果你有幸成为OS 5.0的开发人员,Marc发布了一个新API的链接,它比我上面描述的更清晰,更通用。 For example: 例如:

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

(Naturally you can substitute the filter/scaling options based on your needs.) (当然,您可以根据需要替换过滤器/缩放选项。)

I'm not a Blackberry programmer, but I believe some of these links will help you out: 我不是黑莓程序员,但我相信其中一些链接会帮助你:

Image Resizing Article 图像调整文章
Resizing a Bitmap on the Blackberry 调整Blackberry上的位图大小
Blackberry Image Scaling Question 黑莓图像缩放问题

Keep in mind that the default image scaling done by BlackBerry is quite primitive and generally doesn't look very good. 请记住,BlackBerry完成的默认图像缩放非常原始,通常看起来不太好。 If you are building for 5.0 there is a new API to do much better image scaling using filters such as bilinear or Lanczos. 如果您正在构建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;
}

I am posting this answers for newbie in Blackberry Application development. 我在Blackberry应用程序开发中发布了这个新手的答案。 Below code is for processing Bitmap images from URL and Resizing them without loass of Aspect Ratio : 下面的代码用于处理来自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;
}

The Method resizeImage is called inside the method imageFromServer(String url). 方法resizeImage在方法imageFromServer(String url)中调用。 1) the image from server is processed using EncodedImage img. 1)使用EncodedImage img处理来自服务器的图像。 2) Bitmap bitmp = resizeImage(img.getBitmap(), 100, 100); 2)位图bitmp = resizeImage(img.getBitmap(),100,100); parameters are passed to resizeImage() and the return value from resizeImage() is set to Bitmap bitmp. 参数传递给resizeImage(),resizeImage()的返回值设置为Bitmap bitmp。

Here is the function or you can say method to resize image, use it as you want : 这是函数或者您可以说调整图像大小的方法,可以根据需要使用它:

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