Are there any chances that I could resize an image using the fileconnection inputstream?

FileConnection fc = (FileConnection) Connector.open("file:///" + filePath + "/",Connector.READ);
InputStream fis = (InputStream) fc.openInputStream();

I want to resize the image from here because if I use

Image.createImage(fis);

I am encountering "Out of Memory error", if the file size is too large.

What I wanna do is have a function similar to resize(fis, percent) before using Image.createImage(fis) so that image is resized before creating it.

Originally this code is developed by Mr. Heriman. I hope this will help you.

public static Image resizeImage(Image src, int screenWidth, int screenHeight)
{
    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 x = 0; x < screenWidth; x++)
    {
        g.setClip(x, 0, 1, srcHeight);
        g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
        pos += ratio;
    }

    Image resizedImage = Image.createImage(screenWidth, screenHeight);
    g = resizedImage.getGraphics();
    ratio = (srcHeight << 16) / screenHeight;
    pos = ratio / 2;

    //Vertical resize
    for (int y = 0; y < screenHeight; y++) {
        g.setClip(0, y, screenWidth, 1);
        g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
        pos += ratio;
    }

    return resizedImage;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.