简体   繁体   English

Java等效于JavaScript的Canvas getImageData

[英]Java equivalent of JavaScript's Canvas getImageData

I'm porting a HTML5's Canvas sample to Java, so far so good, until i get on this function call : 到目前为止,我正在将HTML5的Canvas示例移植到Java,直到我开始进行此函数调用为止:

Canvas.getContext('2d').getImageData(0, 0, 100, 100).data

I googled for a while and found this page of the canvas specification 我搜索了一段时间,找到了画布规范的此页面

http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#pixel-manipulation http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#pixel-manipulation

After reading it, I created this function below : 阅读后,我在下面创建了此函数:

public int[] getImageDataPort(BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();

    int[] ret = new int[width * height * 4];

    int idx = 0;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int color = image.getRGB(x, y);

            ret[idx++] = getRed(color);
            ret[idx++] = getGreen(color);
            ret[idx++] = getBlue(color);
            ret[idx++] = getAlpha(color);
        }
    }
    return ret;
}

public int getRed(int color) {
    return (color >> 16) & 0xFF;
}

public int getGreen(int color) {
    return (color >> 8) & 0xFF;
}

public int getBlue(int color) {
    return (color >> 0) & 0xFF;
}

public int getAlpha(int color) {
    return (color >> 24) & 0xff;
}

There is any class on Java Graphics API that has this function built-in or i should use the one that i had created? Java Graphics API上有任何内置此功能的类,还是我应该使用自己创建的那个类?

I think the closest thing you'll find in the standard Java API is the Raster class. 我认为您可以在标准Java API中找到的最接近的东西是Raster类。 You can get hold of a WritableRaster (used for low-level image manipulation) through BufferedImage.getRaster . 您可以通过BufferedImage.getRaster持有WritableRaster (用于低级图像操作)。 The Raster class then provides methods such as getSamples which fills an int[] with image data. 然后, Raster类提供诸如getSamples类的方法,该方法将图像数据填充到int[]

Thanks aioobe, i've looked at the WritableRaster class and found the getPixels function which does exactly what i needed, the final result is : 谢谢aioobe,我查看了WritableRaster类,发现正是我需要的getPixels函数,最终结果是:

public int[] getImageDataPort(BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();

    int[] ret = null;

    ret = image.getRaster().getPixels(0, 0, width, height, ret);

    return ret;
}

The only problem that may happen is when the image.getType isn't a type that supports alpha in comparison with the code of the question, resulting in a smaller int[] ret , but one can simply convert the image type with : 可能发生的唯一问题是,与问题代码相比, image.getType不是支持alpha的类型,从而导致较小的int[] ret ,但可以使用以下方法简单地转换图像类型:

public BufferedImage convertType(BufferedImage image,int type){
    BufferedImage ret = new BufferedImage(image.getWidth(), image.getHeight(), type);
    ColorConvertOp xformOp = new ColorConvertOp(null);
    xformOp.filter(image, ret);
    return ret;
}

Try 尝试

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);

where bi - BufferendImage 其中bi-BufferendImage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM