首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从JFIF读取图像数据?

从JFIF读取图像数据?
EN

Stack Overflow用户
提问于 2014-10-09 14:58:55
回答 1查看 2.9K关注 0票数 0

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

到目前为止,我的代码只是为了上下文:

代码语言:javascript
复制
// 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数据?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-09 15:39:44

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

y

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

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

  1. 赫夫曼译码
  2. 游程解码
  3. 反量化
  4. 列表项目
  5. 逆离散余弦变换
  6. 向上样本
  7. YCbCr到RGB转换

这是在顺序的简单情况下。

你不可能在这个论坛上解释所有这些步骤。

我也建议你

tmb

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26281695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档