因此,我从一个文件中读取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数据?
https://stackoverflow.com/questions/26281695
复制相似问题