Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    33

    how to cut some part of Image

    I want to get some part of the image by cutting like i have a square on the screen and i want to cut that part of the image which i think it should be done through cliping but i dnt know how can i use it for that purpose its urgent if any one provide me some sample code its really benificial for me.

    Regards,
    Muhammad Shouab

  2. #2
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,697

    Re: how to cut some part of Image

    hi Muhammad,

    #1
    use Image class:
    createImage
    public static Image createImage(Image image,
    int x,
    int y,
    int width,
    int height,
    int transform)

    Creates an immutable image using pixel data from the specified region of a source image, transformed as specified.
    The source image may be mutable or immutable. For immutable source images, transparency information, if any, is copied to the new image unchanged.

    On some devices, pre-transformed images may render more quickly than images that are transformed on the fly using drawRegion. However, creating such images does consume additional heap space, so this technique should be applied only to images whose rendering speed is critical.

    The transform function used must be one of the following, as defined in the Sprite class:
    Sprite.TRANS_NONE - causes the specified image region to be copied unchanged
    Sprite.TRANS_ROT90 - causes the specified image region to be rotated clockwise by 90 degrees.
    Sprite.TRANS_ROT180 - causes the specified image region to be rotated clockwise by 180 degrees.
    Sprite.TRANS_ROT270 - causes the specified image region to be rotated clockwise by 270 degrees.
    Sprite.TRANS_MIRROR - causes the specified image region to be reflected about its vertical center.
    Sprite.TRANS_MIRROR_ROT90 - causes the specified image region to be reflected about its vertical center and then rotated clockwise by 90 degrees.
    Sprite.TRANS_MIRROR_ROT180 - causes the specified image region to be reflected about its vertical center and then rotated clockwise by 180 degrees.
    Sprite.TRANS_MIRROR_ROT270 - causes the specified image region to be reflected about its vertical center and then rotated clockwise by 270 degrees.

    The size of the returned image will be the size of the specified region with the transform applied. For example, if the region is 100 x 50 pixels and the transform is TRANS_ROT90, the returned image will be 50 x 100 pixels.

    Note: If all of the following conditions are met, this method may simply return the source Image without creating a new one:

    * the source image is immutable;
    * the region represents the entire source image; and
    * the transform is TRANS_NONE.

    Parameters:
    image - the source image to be copied from
    x - the horizontal location of the region to be copied
    y - the vertical location of the region to be copied
    width - the width of the region to be copied
    height - the height of the region to be copied
    transform - the transform to be applied to the region
    Returns:
    the new, immutable image
    Throws:
    NullPointerException - if image is null
    IllegalArgumentException - if the region to be copied exceeds the bounds of the source image
    IllegalArgumentException - if either width or height is zero or less
    IllegalArgumentException - if the transform is not valid
    Since:
    MIDP 2.0
    that is simply create new image using original image with applied "crop" information (start x/y, width/height)

    #2
    use Graphics.drawRegion
    drawRegion

    public void drawRegion(Image src,
    int x_src,
    int y_src,
    int width,
    int height,
    int transform,
    int x_dest,
    int y_dest,
    int anchor)

    Copies a region of the specified source image to a location within the destination, possibly transforming (rotating and reflecting) the image data using the chosen transform function.

    The destination, if it is an image, must not be the same image as the source image. If it is, an exception is thrown. This restriction is present in order to avoid ill-defined behaviors that might occur if overlapped, transformed copies were permitted.

    The transform function used must be one of the following, as defined in the Sprite class:
    Sprite.TRANS_NONE - causes the specified image region to be copied unchanged
    Sprite.TRANS_ROT90 - causes the specified image region to be rotated clockwise by 90 degrees.
    Sprite.TRANS_ROT180 - causes the specified image region to be rotated clockwise by 180 degrees.
    Sprite.TRANS_ROT270 - causes the specified image region to be rotated clockwise by 270 degrees.
    Sprite.TRANS_MIRROR - causes the specified image region to be reflected about its vertical center.
    Sprite.TRANS_MIRROR_ROT90 - causes the specified image region to be reflected about its vertical center and then rotated clockwise by 90 degrees.
    Sprite.TRANS_MIRROR_ROT180 - causes the specified image region to be reflected about its vertical center and then rotated clockwise by 180 degrees.
    Sprite.TRANS_MIRROR_ROT270 - causes the specified image region to be reflected about its vertical center and then rotated clockwise by 270 degrees.

    If the source region contains transparent pixels, the corresponding pixels in the destination region must be left untouched. If the source region contains partially transparent pixels, a compositing operation must be performed with the destination pixels, leaving all pixels of the destination region fully opaque.

    The (x_src, y_src) coordinates are relative to the upper left corner of the source image. The x_src, y_src, width, and height parameters specify a rectangular region of the source image. It is illegal for this region to extend beyond the bounds of the source image. This requires that:


    x_src >= 0
    y_src >= 0
    x_src + width <= source width
    y_src + height <= source height

    The (x_dest, y_dest) coordinates are relative to the coordinate system of this Graphics object. It is legal for the destination area to extend beyond the bounds of the Graphics object. Pixels outside of the bounds of the Graphics object will not be drawn.

    The transform is applied to the image data from the region of the source image, and the result is rendered with its anchor point positioned at location (x_dest, y_dest) in the destination.

    Parameters:
    src - the source image to copy from
    x_src - the x coordinate of the upper left corner of the region within the source image to copy
    y_src - the y coordinate of the upper left corner of the region within the source image to copy
    width - the width of the region to copy
    height - the height of the region to copy
    transform - the desired transformation for the selected region being copied
    x_dest - the x coordinate of the anchor point in the destination drawing area
    y_dest - the y coordinate of the anchor point in the destination drawing area
    anchor - the anchor point for positioning the region within the destination image
    Throws:
    IllegalArgumentException - if src is the same image as the destination of this Graphics object
    NullPointerException - if src is null
    IllegalArgumentException - if transform is invalid
    IllegalArgumentException - if anchor is invalid
    IllegalArgumentException - if the region to be copied exceeds the bounds of the source image
    Since:
    MIDP 2.0
    #3
    use GameCanvas and LayerManager, add your original image as Sprite and then simply call "setViewWindow" to restrict area of your image that are actually drawn on screen,
    setViewWindow

    public void setViewWindow(int x,
    int y,
    int width,
    int height)

    Sets the view window on the LayerManager.

    The view window specifies the region that the LayerManager draws when its paint(javax.microedition.lcdui.Graphics, int, int) method is called. It allows the developer to control the size of the visible region, as well as the location of the view window relative to the LayerManager's coordinate system.

    The view window stays in effect until it is modified by another call to this method. By default, the view window is located at (0,0) in the LayerManager's coordinate system and its width and height are both set to Integer.MAX_VALUE.

    Parameters:
    x - the horizontal location of the view window relative to the LayerManager's origin
    y - the vertical location of the view window relative to the LayerManager's origin
    width - the width of the view window
    height - the height of the view window
    Throws:
    IllegalArgumentException - if the width or height is less than 0
    I think simply creating new image using Image.createImage method will be the easiest one to start with,

    regards,
    Peter

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    33

    Post Re: how to cut some part of Image

    this isuue has been resolved you can use the following function to resolve that issue.

    Code:
    public Image getImageRegion(Image source, int x, int y, int width, int height){
            System.out.println("x="+x);
            System.out.println("y="+y);
            Image results = Image.createImage(width, height);
            results.getGraphics().drawImage(source, -x, -y, Graphics.LEFT |  Graphics.TOP);
            return results;
        }
    Hope it helps.

    Regards,
    Muhammad Shouab

Similar Threads

  1. Showing Part of Image from ByteArray (No Native API)
    By vishwas_dot_h in forum Mobile Java Media (Graphics & Sounds)
    Replies: 5
    Last Post: 2009-08-27, 12:17
  2. HELP: Mutable Image to Immutable Image?
    By rj_cybersilver in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2005-03-26, 10:58
  3. I want to Display an image as part of installation
    By johnward1978 in forum Symbian
    Replies: 1
    Last Post: 2004-10-19, 04:02
  4. Series 40 - draw only a part of the image
    By ionutianasi in forum Mobile Java Media (Graphics & Sounds)
    Replies: 11
    Last Post: 2003-09-08, 04:25
  5. Series 40 - image resizeing and drawing black and white only a part of an image
    By ionutianasi in forum Mobile Java Media (Graphics & Sounds)
    Replies: 3
    Last Post: 2003-07-21, 10:36

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×