从图像切割形状

时间:2015-07-14 08:53:21

标签: java graphics bufferedimage java-2d clipping

我有一个缓冲图像,以及像拼图一样的形状数组。我想从图像中剪切出那些形状。我尝试剪裁图像,但它没有正常工作。到目前为止我所做的是:

// gives an integer type fitting in N bytes
template <int N>
struct base_int_t
{
    typedef int type;
};
// specializations
template <>
struct base_int_t<1>
{
    typedef unsigned char type;
};

template <>
struct base_int_t<2>
{
    typedef unsigned short type;
};
// add suitable definitions for N = 3,4...8. For N = 3 and 4 type is unsigned int

template <int EXP_BITS, int MANTISSA_BITS>
struct fp_float
{
    // template argument is the number of bytes required
    typedef typename base_int_t<(EXP_BITS + MANTISSA_BITS + 7) / 8>::type type;
    type mantissa : MANTISSA_BITS;
    type exponent : EXP_BITS;
};

typedef fp_float<3, 11> fp_3_11_t;
fp_3_11_t fp;

它仅适用于第一个形状,然后将透明图像写为输出。

1 个答案:

答案 0 :(得分:3)

实际上有很多方法可以做到这一点。这是我的方法:)

int width = 0;
int height = 0;
int rows = 0;
int columns = 0;
BufferedImage sheet = // you image goes here 
BufferedImage[] pieces = new BufferedImage[rows * cols];
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        pieces[(i * cols) + j] = sheet.getSubimage(j * width, i * height, width, height);    
    }
}

请注意,这些部分都在一个数组中。如果你仍然想要你的作品在二维数组中:

// Switch these lines
BufferedImage[][] pieces = new BufferedImage[rows][cols];
pieces[i][j] = sheet.getSubimage(j * width, i * height, width, height);   

补充阅读: Spritesheets和动画
点击here