Skip to main content

The crop operation seems to ignore (x,y) offset

3 replies [Last post]
letfunbegin
Offline
Joined: 2008-10-31

Hi there!

Firstly I'd like to assure that I've scanned the net quite extensively without finding anything out. :)

I've written a short (could probably be shorter) method that crops an image (byte[]).

<br />
public byte[] crop(byte[] bytes) throws Exception<br />
{<br />
    /*<br />
        7.3 Cropping an Image<br />
        The Crop operation crops a rendered or renderable image to a specified rectangular area.<br />
        The x, y, width, and height values are clipped to the source image's bounding box.<br />
        These values are rounded to type int for rendered images.<br />
        The Crop operation takes one rendered or renderable source image and four parameters.<br />
        None of the parameters have default values; all must be supplied.</p>
<p>        x      10<br />
        y      20<br />
        width  100<br />
        height 200<br />
     */</p>
<p>    ParameterBlock parameters = new ParameterBlock();<br />
    parameters.addSource(JAI.create("url", new URL("some_jpeg_image")));<br />
    parameters.add(10f);<br />
    parameters.add(20f);<br />
    parameters.add(100f);<br />
    parameters.add(200f);</p>
<p>    RenderedOp cropOperation = JAI.create("crop", parameters);</p>
<p>    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();<br />
    JAI.create("encode", cropOperation, outputStream, "JPEG", null);</p>
<p>    return outputStream.toByteArray();<br />
}<br />

I have tested this code out. My observation is that the "crop" operation respects the width and height parameters. However it seems to totally ignore the x and y parameters, and so long as I set them to so something within the ranges of the image supplied, it neither complains nor does anything to the produced image.

Perhaps I've slipped somewhere in the documentation?

I couldn't find anything about this in known bugs, so I'm assuming it's either an undiscovered bug, a feature or simply just me not understanding.

I'd very much like some help sorting this out.

Documentation found on http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Image-enhance.doc.html#73080

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
letfunbegin
Offline
Joined: 2008-10-31

I believe I found the answer in this thread:

http://forums.java.net/jive/thread.jspa?threadID=73130&tstart=30

Have not tested it out yet as I'm currently headed out.

letfunbegin
Offline
Joined: 2008-10-31

So it works now. Apparently it's because you need to retrieve it as a buffered image. When this is done the cropped part of the image gets copied into the new object - at least that's my understanding of it - and then the writer won't have any trouble writing the right bytes.

[code]
JAI.create("encode", cropOperation.getAsBufferedImage(), outputStream, "JPEG", null);
[/code]

If anyone has any further insight or corrections to what I just said, I'd be delighted to hear it.

Håkan Jacobsson

Yep,correct.Took me a while to figure that one out.;)

22 mar 2010 kl. 03.31 skrev jai-interest@javadesktop.org:

> So it works now. Apparently it's because you need to retrieve it as
> a buffered image. When this is done the cropped part of the image
> gets copied into the new object - at least that's my understanding
> of it - and then the writer won't have any trouble writing the right
> bytes.
>
> [code]
> JAI.create("encode", cropOperation.getAsBufferedImage(),
> outputStream, "JPEG", null);
> [/code]
>
> If anyone has any further insight or corrections to what I just
> said, I'd be delighted to hear it.
> [Message sent by forum member 'letfunbegin']
>
> http://forums.java.net/jive/thread.jspa?messageID=393040
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net