gpt4 book ai didi

java - 从 JFIF 读取图像数据?

转载 作者:行者123 更新时间:2023-12-01 18:25:41 30 4
gpt4 key购买 nike

所以我正在从文件中读取 JFIF (JPEG) 数据,作为练习(我知道已经有一些库可以做到这一点,我不是在寻找那些库)。我已经获得了图像文件大小、颜色深度和尺寸。但是,我不太确定如何获取实际的图像数据。我在十六进制编辑器中查看了数据,并将其与实际图像进行比较,结果毫无结果。如果有人有一个很好的资源来开始这件事(我知道这可能是一个艰巨而有启发性的过程,但这就是我这样做的原因),那就太棒了。

到目前为止我的代码,仅供引用:

// check header data, assign header data to important fields

// Start Of Image (SOI) must be FFD8 and the next marker must be FF
if(!(this.data[0] == (byte) 0xFF && this.data[1] == (byte) 0xD8
&& this.data[2] == (byte) 0xFF))
this.isValid = false;

// check if file is not valid
if(!isValid)
log.log(Level.SEVERE,
String.format("ERROR: File %s is not registered as a JFIF!\n", this.filename),
new IllegalArgumentException());

// If the next values are correct, then the data stream starts at SOI
// If not, the data stream is raw
this.isRawDataStream = !(this.data[3] == (byte) 0xE0
&& this.data[6] == (byte) 0x4A
&& this.data[7] == (byte) 0x46
&& this.data[8] == (byte) 0x49
&& this.data[9] == (byte) 0x46
&& this.data[10] == (byte) 0x00);

// Read until SOF0 marker (0xC0)
int i = 11;
while(this.data[i] != (byte) 0xC0) {
i++;
}
System.out.println("SOF0 marker at offset " + i);

// Skip two bytes, next byte is the color depth
this.colorDepth = this.data[i+3];

// Next two bytes are the image height
String h = String.format("%02X", this.data[i+4]) + String.format("%02X", this.data[i+5]);
this.height = hexStringToInt(h);
System.out.println("Height: " + this.height);

// Next two bytes are the image width
String w = String.format("%02X", this.data[i+6]) + String.format("%02X", this.data[i+7]);
this.width = hexStringToInt(w);
System.out.println("Width: " + this.width);

System.out.println("Color depth: " + this.colorDepth);
// load pixels into an image
this.image = new BufferedImage(this.width,
this.height,
BufferedImage.TYPE_INT_RGB);

然后,我需要获取每个像素并将其发送到图像。我如何获取每个像素及其各自的 RGB 数据?

最佳答案

你想做的并不是一个简单的下午项目。这本书解释了这个过程:JPEG压缩数据和像素值之间有很多代码。

http://www.amazon.com/Compressed-Image-File-Formats-JPEG/dp/0201604434/ref=pd_bxgy_b_img_y

首先,您必须处理两种独立但相关的压缩方法:顺序压缩和渐进压缩。

当您读取位数据时,您必须

  1. 霍夫曼解码
  2. 游程长度解码
  3. 逆量化
  4. 列出项目
  5. 离散余弦逆变换
  6. 上样
  7. YCbCr 到 RGB 转换

这是简单的顺序情况。

您不会在此论坛上获得所有这些步骤的解释。

我也推荐

http://www.amazon.com/dp/1558514341/ref=rdr_ext_tmb

关于java - 从 JFIF 读取图像数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26281695/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com