【问题标题】:Conversion of a byte[] BGRA array into something more useful/speedy (JAVA)将 byte[] BGRA 数组转换为更有用/更快速的东西(JAVA)
【发布时间】:2011-08-28 11:49:23
【问题描述】:

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

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

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

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

【问题讨论】:

  • Raster 开始,点击链接。快乐编码。不幸的是,Java 缺乏一种定义新的“可打包”类型(例如“值类型”)的方法,并且大多数处理 RGB[A] 像素的方法对于高级语言来说都不符合标准。

标签: java arrays colors byte raster


【解决方案1】:

我不知道这是否很快,但它肯定更有用。我的源数据数组来自 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;
}

【讨论】:

    【解决方案2】:

    您可以看到这个other question,它对良好的 Java 图像处理库有响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 2012-07-26
      • 2010-10-05
      相关资源
      最近更新 更多