Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于缩放表示像素的字节数组的函数_Java_Arrays_Bytearray_Scale_Pixel - Fatal编程技术网

Java 用于缩放表示像素的字节数组的函数

Java 用于缩放表示像素的字节数组的函数,java,arrays,bytearray,scale,pixel,Java,Arrays,Bytearray,Scale,Pixel,在Java中,我有一个byte[]数组,它本质上是存储具有给定维度的sprite中像素的颜色代码。我的问题是: 如果我想按整数比例缩放那个精灵,我会怎么做?我可以找出如何复制数组中的每个元素的缩放次数,但我需要做的是基本上确保每一行的缩放正确,然后将每一行的缩放数放入数组中 你知道怎么做吗 编辑: 我尝试过此功能,但似乎不起作用: public static byte[] scaleImage(byte[] pix, int width, int scale){ int height =

在Java中,我有一个byte[]数组,它本质上是存储具有给定维度的sprite中像素的颜色代码。我的问题是:

如果我想按整数比例缩放那个精灵,我会怎么做?我可以找出如何复制数组中的每个元素的缩放次数,但我需要做的是基本上确保每一行的缩放正确,然后将每一行的缩放数放入数组中

你知道怎么做吗

编辑:

我尝试过此功能,但似乎不起作用:

public static byte[] scaleImage(byte[] pix, int width, int scale){
    int height = pix.length / width;
    byte[] ret = new byte[pix.length * scale * scale];
    for(int i=0; i<height; i++){
        if(i % scale == 0){
            for(int j=0; j<width; j++){
                if(j % scale == 0)ret[i * width * scale + j] = pix[(i / scale) * width + (j / scale)];
                else ret[i * width * scale + j] = ret[i * width * scale + j -1];
            }
        }
        else for(int j=0; j<width; j++){
            ret[i * width * scale + j] = ret[(i-1) * width * scale + j];
        }
    }
    return ret;
}

可以使用BuffereImage和AffineTransformOp对int[]数组执行此操作。这样做的好处是,数据被视为图像而不是数组,因此可以使用不同的缩放算法、插值等、非整数缩放值等

将2x2图像缩放为4x4的示例

首先将数据写入图像

int[] rawData = new int[2 * 2 * 4]; // input is 4 int per pixel, ARGB 
BufferedImage input = new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB);
((WritableRaster) input.getData()).setPixels(0, 0, 2, 2, rawData);
缩放

int scale = 2;
AffineTransform transform = new AffineTransform();
transform.scale(scale, scale);
AffineTransformOp op = new AffineTransformOp(transform, null);
BufferedImage output = new BufferedImage(input.getWidth() * scale, input.getHeight() * scale, input.getType());
op.filter(input, output);
然后可以访问像素

System.out.println(Arrays.toString(output.getData().getPixels(0, 0, output.getWidth(), output.getHeight(), (int[]) null)));

您的数据是ARGB字节数组吗?不,它们是由另一个类中的函数解释的颜色代码。问题是它们不是ARGB,它们只是用于其他地方的代码。如果我将BuffereImage中的像素设置为当值不是ARGB时的值,只是1-100左右的随机值,然后尝试使用BuffereImage.getRGB,那么如果我改用TYPE_INT_RGB,会返回进入ITI的相同值吗,或者其他什么?