java-melcdui

How to Set Image height and width in j2me(java)


i have created an image from the image url(lcdui image)

HttpConnection c = (HttpConnection) Connector.open(imageurl);
int len = (int)c.getLength();

if (len > 0) 
{
is = c.openDataInputStream();
byte[] data = new byte[len];
is.readFully(data);

img = Image.createImage(data, 0, len);

i want to set height and width to this?and i want to display


Solution

  • You can't set width and height to Image. But, you can resize the image using the below method.

    public Image resizeImage(Image src, int screenHeight, int screenWidth) {
            int srcWidth = src.getWidth();
    
            int srcHeight = src.getHeight();
            Image tmp = Image.createImage(screenWidth, srcHeight);
            Graphics g = tmp.getGraphics();
            int ratio = (srcWidth << 16) / screenWidth;
            int pos = ratio / 2;
    
            //Horizontal Resize        
    
            for (int index = 0; index < screenWidth; index++) {
                g.setClip(index, 0, 1, srcHeight);
                g.drawImage(src, index - (pos >> 16), 0);
                pos += ratio;
            }
    
            Image resizedImage = Image.createImage(screenWidth, screenHeight);
            g = resizedImage.getGraphics();
            ratio = (srcHeight << 16) / screenHeight;
            pos = ratio / 2;
    
            //Vertical resize
    
            for (int index = 0; index < screenHeight; index++) {
                g.setClip(0, index, screenWidth, 1);
                g.drawImage(tmp, 0, index - (pos >> 16));
                pos += ratio;
            }
            return resizedImage;
    
        }