Respuesta

3

Tenga en cuenta que la escala de imagen predeterminada hecha por BlackBerry es bastante primitiva y, en general, no se ve muy bien. Si está compilando para 5.0, hay un new API para mejorar la escala de imagen usando filtros como bilineal o Lanczos.

10

Puede hacer esto simplemente usando el método EncodedImage.scaleImage32(). Necesitará proporcionarle los factores por los cuales desea escalar el ancho y alto (como Fixed32).

Aquí hay un código de muestra que determina el factor de escala para el ancho y la altura dividiendo el tamaño de la imagen original por el tamaño deseado, utilizando la clase Fixed32 de RIM.

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

Si tienes la suerte de ser desarrollador para OS 5.0, Marc publicó un enlace a la new APIs que son mucho más claro y más versátil que el que he descrito anteriormente. Por ejemplo:

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

(Naturalmente se puede sustituir las opciones de filtro/escala en base a sus necesidades.)

+0

@TreeUK: Tengo esa función exacta en el código de trabajo en este momento. ¿Está convirtiendo el 'int's en' Fixed32' y usando 'Fixed32.div()' para descubrir los factores de escala? La división entera normal no lo cortará. – Skrud

+0

Gracias, lo estaba usando incorrectamente. No está relacionado con enteros, pero aún está mal. –

2

para BlackBerry JDE 5.0 o posterior, puede utilizar la API de scaleInto.

0

Aquí es la función o se puede decir método para cambiar el tamaño de la imagen, lo utilizan como desee:

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(); 
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; 
} 
+0

simplemente llame a este método y manténgalo en un mapa de bits ... disfrute – Blackberry

+0

scaleInto() para mapa de bits está disponible desde OS 5.0. – Lucas

+0

@Lucas Sí, ha sido desde BlackBerry API 5.0.0 – Blackberry

0

Estoy publicar respuestas para este novato en el desarrollo de aplicaciones de Blackberry. A continuación es el código para el procesamiento de imágenes de mapa de bits de URL y cambiar el tamaño de ellos sin loass de Relación de aspecto:

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

El Método resizeImage se llama dentro de la imageFromServer (String url). 1) la imagen del servidor se procesa utilizando EncodedImage img. 2) Bitmap bitmp = resizeImage (img.getBitmap(), 100, 100); los parámetros se pasan a resizeImage() y el valor de retorno de resizeImage() se establece en bitmap bitmp.

Cuestiones relacionadas