Search code examples
image-processingjava-mecrop

How to crop an image in J2ME


I want to crop an image in J2ME, but I can't seem to find anything online on how to do it. I see some implementations in Java, but they use libraries not available in J2ME.

How it's done in J2ME?


Solution

  • Create a mutable Image with the exact crop size.
    Get the Graphics from the mutable Image.
    Draw the original Image in a position that the crop area fits exactly at the mutable Image.

    For example, if I want to crop the center of an Image:

    Image original = Image.createImage(stream); // 320 x 240
    Image crop = Image.createImage(160, 120);
    Graphics cropGraphics = crop.getGraphics();
    int x = crop.getWidth() / 2;
    int y = crop.getHeight() / 2;
    int anchor = Graphics.HCENTER | Graphics.VCENTER;
    
    cropGraphics.drawImage(original, x, y, anchor);