【问题标题】:Convert RAW image into jpg in java在java中将RAW图像转换为jpg
【发布时间】:2019-05-29 12:58:26
【问题描述】:

我有返回 RAW 640x480 BGR 的捕获设备。支持它的文档只有 .net/C# 代码示例。

这是他们在 .net SDK 中的示例代码

Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0,
                                                            bmp.Width,
                                                            bmp.Height),
                                              ImageLockMode.WriteOnly,
                                              bmp.PixelFormat);

Marshal.Copy(faceImage, 0, bmpData.Scan0, faceImage.Length);
bmp.UnlockBits(bmpData);

这是我在 Java 中得到的最接近的,但颜色仍然不正确

int nindex = 0;
int npad = (raw.length / nHeight) - nWidth * 3;

BufferedImage bufferedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_4BYTE_ABGR);
DataBufferByte dataBufferByte = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer());
byte[][] bankData = dataBufferByte.getBankData();
byte brgb[] = new byte[(nWidth + npad) * 3 * nHeight];

System.arraycopy(raw, 0, brgb, 0, raw.length);

for(int j = 0; j < nHeight - 1; j++)
{
    for(int i = 0; i < nWidth; i++)
    {
        int base = (j * nWidth + i) * 4;
        bankData[0][base] = (byte) 255;
        bankData[0][base + 1] = brgb[nindex + 1];
        bankData[0][base + 2] = brgb[nindex + 2];
        bankData[0][base + 3] = brgb[nindex];
        nindex += 3;
    }
    nindex += npad;
}

ImageIO.write(bufferedImage, "png", bs);

红色和绿色似乎颠倒了。感谢您的反馈以解决此问题。谢谢!

【问题讨论】:

  • 欢迎来到 Stack Overflow!看起来您可能需要学习使用调试器。请帮助自己一些complementary debugging techniques。如果您之后仍有问题,请edit您的问题更具体地说明您需要什么帮助。

标签: java c# .net image-processing bufferedimage


【解决方案1】:

您的代码的以下部分对我来说似乎不太正确

bankData[0][base] = (byte) 255;
bankData[0][base + 1] = brgb[nindex + 1];
bankData[0][base + 2] = brgb[nindex + 2];
bankData[0][base + 3] = brgb[nindex];

您正在使用TYPE_4BYTE_ABGR 定义您的缓冲图像。 Java 文档说

字节数据以 A、B、G、R 的顺序在每个像素内从低字节地址到高字节地址交错排列在一个单字节数组中。

根据您的说法,原始图像的格式也应该是 BGR,因此原始图像中的字节应该从最低字节到最高字节的顺序为 B、G、R,对吧?据我从您的 sn-p 可以看出,您正在将红色通道值复制到绿色通道,将蓝色通道值复制到红色通道,将绿色通道值复制到蓝色通道。复制字节应该是相当的

bankData[0][base] = (byte) 255;
bankData[0][base + 1] = brgb[nindex]; // B
bankData[0][base + 2] = brgb[nindex + 1]; // G
bankData[0][base + 3] = brgb[nindex + 2]; // R

【讨论】:

    猜你喜欢
    • 2015-10-06
    • 2018-05-10
    • 2012-06-01
    • 2018-12-05
    • 1970-01-01
    • 2013-05-10
    • 2021-08-21
    • 2018-03-08
    • 2021-09-03
    相关资源
    最近更新 更多