Java 相当于 JavaScript 的 Canvas getImageData

发布于 2024-10-16 17:12:37 字数 1391 浏览 2 评论 0 原文

我正在将 HTML5 的 Canvas 示例移植到 Java,到目前为止一切顺利,直到我进行此函数调用:

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

我用 google 搜索了一段时间,找到了画布规范的此页面

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

读完后,我在下面创建了这个函数:

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;
}

Java Graphics API 上有任何类内置了这个函数,否则我应该使用这个函数我创造的?

I'm porting a HTML5's Canvas sample to Java, so far so good, until i get on this function call :

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁的年少不轻狂 2024-10-23 17:12:37

我认为您在标准 Java API 中找到的最接近的东西是 Raster 类。您可以获取 WritableRaster< /code>(用于低级图像操作)通过 BufferedImage.getRaster。然后,Raster 类提供以下方法:getSamples 填充 int[] 与图像数据。

I think the closest thing you'll find in the standard Java API is the Raster class. You can get hold of a WritableRaster (used for low-level image manipulation) through BufferedImage.getRaster. The Raster class then provides methods such as getSamples which fills an int[] with image data.

输什么也不输骨气 2024-10-23 17:12:37

谢谢 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;
}

唯一可能发生的问题是当与问题代码相比,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;
}

Thanks aioobe, i've looked at the WritableRaster class and found the getPixels function which does exactly what i needed, the final result is :

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 :

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;
}
虐人心 2024-10-23 17:12:37

尝试

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

在哪里 bi - BufferendImage

Try

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

where bi - BufferendImage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文