【发布时间】:2014-10-09 14:58:55
【问题描述】:
所以我正在从文件中读取 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 数据?
【问题讨论】:
-
您可以阅读规范以查看 jfif 所需的所有数据:jpeg.org/public/jfif.pdf
-
啊,所以,根据阅读,我必须像这样获取RGB数据? Y = 0.299 R + 0.587 G + 0.114 B Cb = - 0.1687 R - 0.3313 G + 0.5 B + 128 Cr = 0.5 R - 0.4187 G - 0.0813 B + 128 @Obicere
-
遵循规范。我还没有读过它,但它可能不止于此。但它将包含解析图像所需的所有信息。