2013年8月21日水曜日

Crop Images into pieces for java applet

By using CropImageFilter class, images can be cropped easily with few lines. According to Java docs, parameter of CropImageFilter constructor is
        CropImageFilter(int x, int y, int w, int h)

First, load the image from folder. Here's is the method I prepared.
    private Image loadImage(String img){
        return getImage(app.getDocumentBase(),"img/"+img);
    }

For eclipse IDE, I put my images file inside the src/img package.


After load the image, let's say "image_want_chop.png" is loaded and chop it with following code.
        Image originalImage = loadImage("image_want_chop.png");
        ImageFilter imageFilter = new CropImageFilter(x,y,width,height); 
        Image choppedImage = createImage(new FilteredImageSource(originalImage.getSource(),
                        imageFilter ));

Thank you for reading.

0 件のコメント:

コメントを投稿