Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
zapping
Posts: 4
Registered: ‎10-20-2009
My Device: Not Specified
Accepted Solution

Image resizing

[ Edited ]

Greetings,

 Can one of you help out in resizing image maintaining the aspect ratio or to proportionately scale down image. The following code works with pixels alright but the toFP() conversion does not seem to go right. Thanks in advance.

 

  //makes http connection and downloads the image. Attempts to scale the image if the image is a big one.

  public static EncodedImage getImage(String fileName, int width, int height) {
    try {

      //open http connection
      HttpConnection conn = sendHTTP(fileName);

 

      //get stream to read
      DataInputStream in = conn.openDataInputStream();
      int length = (int)conn.getLength();
      byte[] data = null;

 

      //read in full or as chunks
      if (length != -1) { 
        data = new byte[length];
        in.read(data);
      } else {
        // If content length is not given, read in chunks.
        int chunkSize = 512;
        int index = 0;
        int readLength = 0;
        data = new byte[chunkSize];
        do {
          if (data.length < index + chunkSize) {
            byte[] newData = new byte[index + chunkSize];
            System.arraycopy(data, 0, newData, 0, data.length);
            data = newData;
          }
          readLength = in.read(data, index, chunkSize);
          index += readLength;
        } while (readLength == chunkSize);
        length = index;
      }

 

      //encode the image data, resize and return
      EncodedImage image = EncodedImage.createEncodedImage(data, 0, data.length);
      image = resizeImage(image, width, height);
      return image;
    }
    catch (Exception ex) {
        return null;     
    }

 

   //image resizer

  private static EncodedImage resizeImage(EncodedImage image, int width, int height) {
    if (image == null) {
      return image;
    }

    //return if image does not need a resizing
    if (image.getWidth() <= width && image.getHeight() <= height) {
      return image;
    }
   
    double scaleHeight, scaleWidth;
    if (image.getWidth() > width && image.getHeight() > height) {  //actual image is bigger than scale size
      if (image.getWidth() > image.getHeight()) {  //actual image width is more that height then scale with width
        scaleWidth = width;
        scaleHeight = (double)width / image.getWidth() * image.getHeight();
      } else { //scale with height
        scaleHeight = height;
        scaleWidth = (double)height / image.getHeight() * image.getWidth();
      }
    } else if (width < image.getWidth()) { //scale with scale width or height
      scaleWidth = width;
      scaleHeight = (double)width / image.getWidth() * image.getHeight();
    } else {
      scaleHeight = height;
      scaleWidth = (double)height / image.getHeight() * image.getWidth();
    }
    int w = Fixed32.toFP((int)scaleWidth);
    int h = Fixed32.toFP((int)scaleHeight);
    return image.scaleImage32(w, h);
  }

 

In the resizeImage() call the scalWidth and scaleHeight turns out right but the image.scaleImage() does not seems to create the image after doing the toFP() conversion.

Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Image resizing

New Developer
zapping
Posts: 4
Registered: ‎10-20-2009
My Device: Not Specified

Re: Image resizing

Thx bikas that was a nice lead. The resize function needed a change as follows, now it works just fine. Thx a lot.

 

//image resizer

  private static EncodedImage resizeImage(EncodedImage image, int width, int height) {
    if (image == null) {
      return image;
    }

    //return if image does not need a resizing
    if (image.getWidth() <= width && image.getHeight() <= height) {
      return image;
    }
   
    double scaleHeight, scaleWidth;
    if (image.getWidth() > width && image.getHeight() > height) {  //actual image is bigger than scale size
      if (image.getWidth() > image.getHeight()) {  //actual image width is more that height then scale with width
        scaleWidth = width;
        scaleHeight = (double)width / image.getWidth() * image.getHeight();
      } else { //scale with height
        scaleHeight = height;
        scaleWidth = (double)height / image.getHeight() * image.getWidth();
      }
    } else if (width < image.getWidth()) { //scale with scale width or height
      scaleWidth = width;
      scaleHeight = (double)width / image.getWidth() * image.getHeight();
    } else {
      scaleHeight = height;
      scaleWidth = (double)height / image.getHeight() * image.getWidth();
    }
    int w = Fixed32.div(Fixed32.toFP(image.getWidth()), Fixed32.toFP((int)scaleWidth));
    int h = Fixed32.div(Fixed32.toFP(image.getHeight()), Fixed32.toFP((int)scaleHeight));

    return image.scaleImage32(w, h);
  }

Developer
Posts: 144
Registered: ‎12-20-2008
My Device: Not Specified

Re: Image resizing

I tried but does not resize

 bitmap = EncodedImage.createEncodedImage(dataArray, 0,
                                 dataArray.length);  //aqui el resize
                    bitmap = resizeImage( bitmap, 150,150);

// does not change the width or height and int ancho =bitmap.getWidth();                 

New Member
inzamam
Posts: 1
Registered: ‎01-15-2011
My Device: Not Specified

Re: Image resizing

if  you want to resize image than i will you suggest you to visit the below link
http://www.raiseitsolutions.com/forum/viewtopic.php?f=4&t=3
please visit the link, here you can know about how to resize image.
 thank you very much.