将 byte[] BGRA 数组转换为更有用/更快速的内容 (JAVA)

发布于 2024-11-08 23:45:20 字数 299 浏览 0 评论 0原文

我有一个包含 BGRA 栅格数据的 byte[] 数组(例如,第一个字节 = 蓝色分量,第二个 = 绿色,第五个 = 下一个像素,蓝色),并且想使用它。

具体来说,是否有一个 Java 类已经被设计来包装这样的东西?我想知道,因为我想让我的代码尽可能整洁/正确,如果 Java 已经有一个更快的编译版本,那么我会选择它。

更具体地说,我想将 byte[] 数组转换为 2 个数组,其中 BGR1[] + BGR2[] = BGR,并且 A1 = A2 = A。有什么建议吗?

我当然可以为此编写原始代码,但也许有一种更简洁/更快的方法。

I have a byte[] array that contains BGRA raster data (e.g. first byte = blue component, second = green, fifth = next pixel, blue) and would like to play with it.

Specifically, is there a Java class that's already designed to wrap something like this? I'm wondering, because I'd like to make my code as neat/correct as possible, and if Java already has a compiled version that's faster, then I'd go with that.

Even more specifically, I want to transform the byte[] array into 2 arrays, where BGR1[] + BGR2[] = BGR, and A1 = A2 = A. Any suggestions?

I could of course just write raw code for this, but perhaps there is a neater/faster way.

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

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

发布评论

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

评论(2

孤千羽 2024-11-15 23:45:21

我不知道这是否很快,但它肯定更有用。我的源数据数组来自 Kinect 的颜色流,使用 J4KSDK

我使用此方法的目标是读取图像的二进制字节。我确信您可以对其进行修改以供自己使用。

/* Reference imports */
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/* method */
public byte[] getImage(byte[] bytes) throws IOException {
    int width = 640;
    int height = 480;

    int[] shifted = new int[width * height];

    // (byte) bgra to rgb (int)
    for (int i = 0, j = 0; i < bytes.length; i = i + 4, j++) {
        int b, g, r;

        b = bytes[i] & 0xFF;
        g = bytes[i + 1] & 0xFF;
        r = bytes[i + 2] & 0xFF;

        shifted[j] = (r << 16) | (g << 8) | b;
    }

    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    bufferedImage.getRaster().setDataElements(0, 0, width, height, shifted);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "JPG", baos);
    byte[] ret = baos.toByteArray();

    return ret;
}

I don't know if this is speedy, but it is sure more useful. My source data array came from the Color Stream from Kinect, using J4KSDK.

My goal with this method was to read the binary bytes of an image. I'm sure you can modify it for your own uses.

/* Reference imports */
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/* method */
public byte[] getImage(byte[] bytes) throws IOException {
    int width = 640;
    int height = 480;

    int[] shifted = new int[width * height];

    // (byte) bgra to rgb (int)
    for (int i = 0, j = 0; i < bytes.length; i = i + 4, j++) {
        int b, g, r;

        b = bytes[i] & 0xFF;
        g = bytes[i + 1] & 0xFF;
        r = bytes[i + 2] & 0xFF;

        shifted[j] = (r << 16) | (g << 8) | b;
    }

    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    bufferedImage.getRaster().setDataElements(0, 0, width, height, shifted);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "JPG", baos);
    byte[] ret = baos.toByteArray();

    return ret;
}
梅窗月明清似水 2024-11-15 23:45:21

您可以看到这个其他问题,其中有以下答案良好的 Java 图像处理库。

You could see this other question which has responses for good Java image manipulation libraries.

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