Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crop image using libgdx

Tags:

android

libgdx

I need to crop image like this enter image description here

I need to draw partial image from center

I know there is draw() method of batch with a lot of params, but there is no good documentation about all this params, so i can't figure out how to use it.

Here is what i implemented:

public class TexturePart {

    Texture tex;
    Vector2 position;

// Target Dimension of image

    int targetWidth;
    int targetHeight;

// Src Dimensions of Image

    int srcWidth;
    int srcHeight;
    int srcX;
    int srcY;

// Ratio of dimension of target and source

    float srcTargetRatioX;
    float srcTargetRatioY;

// ImagePart variables with values between 0-100 to draw part of image

    int startPercentX;
    int endPercentX;
    int startPercentY;
    int endPercentY;

    int clipWidth;
    int clipHeight;

    int clipSrcWidth;
    int clipSrcHeight;

    public TexturePart(TextureRegion reg, float x, float y) {

        tex = reg.getTexture();
        position = new Vector2(x, y);

        srcX = reg.getRegionX();
        srcY = reg.getRegionY();

        srcWidth = reg.getRegionWidth();
        srcHeight = reg.getRegionHeight();

        clipSrcWidth = srcWidth;
        clipSrcHeight = srcHeight;
        startPercentX = 28;
        startPercentY = 28;
        endPercentX = 72;
        endPercentY = 72;
        SetTargetDimension(srcWidth, srcHeight);
    }

    public void setSrcWidthHeight(int width, int height){
        this.srcWidth=width;
        this.srcHeight=height;
    }

    public void setSrcHeight(int height){
        this.srcHeight=height;
    }

    public void SetTargetDimension(int targetWidth, int targetHeight) {
        this.targetWidth = targetWidth;
        this.targetHeight = targetHeight;
        clipWidth = targetWidth;
        clipHeight = targetHeight;
        srcTargetRatioX = (float) targetWidth / (float) srcWidth;
        srcTargetRatioY = (float) targetHeight / (float) srcHeight;
    }

    public void SetStart(int x, int y) {
        startPercentX = x;
        startPercentY = y;
    }

    public void SetEnd(int x, int y) {
        endPercentX = x;
        endPercentY = y;
    }

    public void Draw(SpriteBatch sp) {

        clipSrcWidth = (int) (Math.abs(startPercentX - endPercentX) / 100f * srcWidth);
        clipSrcHeight = (int) (Math.abs(startPercentX - endPercentY) / 100f * srcHeight);
        int startX = clipWidth/2 + (int) ((float) startPercentX / 100f * (float) srcX);
        int startY = clipHeight/2 + (int) ((float) startPercentY / 100f * (float) srcY);
        clipWidth = (int) (srcTargetRatioX * clipSrcWidth);
        clipHeight = (int) (srcTargetRatioY * clipSrcHeight);
        sp.begin();

        float scaleX=targetWidth/(srcWidth+0.f);
        float scaleY=targetHeight/(srcHeight+0.f);

        sp.draw(tex, 0, 0, srcWidth, srcHeight, srcWidth, srcHeight, 1, 1, 0, startX, startY, clipSrcWidth, clipSrcHeight, false, false);

        //sp.draw(tex, 0,0,clipWidth, clipHeight, clipWidth, clipHeight, clipSrcWidth, clipSrcHeight, false, false);
        sp.end();
    }

But its not working as expected

like image 570
Jenya Kirmiza Avatar asked Sep 16 '15 13:09

Jenya Kirmiza


1 Answers

To crop the texture you just need to use TextureRegion

    TextureRegion(Texture texture, int x, int y, int width, int height) 

in your case it should look like:

    Texture texture; //this is your original image

    ...

    TextureRegion region = new TextureRegion(texture, texture.getWidth()*0.28f, 0, texture.getWidth()*0.44f, texture.getHeight() );

    ...

    //now you can just draw your texture region
    sp.draw(region); //you can also use other versions of draw to set region position on screen and so on

Why i set x as texture.getWidth()*0.28f? Because if want it centered it should have left margin = 50% of original texture width - texture region width.

    (1 - 0.44) / 2 = 0.28
like image 171
m.antkowicz Avatar answered Sep 28 '22 09:09

m.antkowicz