使用Java程序读取JPEG文件(二)
2014-11-24 03:24:22
·
作者:
·
浏览: 1
1, SOF2, SOF3, SOF5, SOF6, SOF7, SOF9, SOF10, SOF11, SOF13, SOF14, SOF15,
DHT, JPG, DAC,
RST0, RST1, RST2, RST3, RST4, RST5, RST6, RST7,
SOI, EOI, SOS, DQT, DNL, DRI, DHP, EXP,
APP0, APP15, JPG0, JPG13, COM, NOP,
};
static HashMap
m_mapSectionType = new HashMap
(); static { m_mapSectionType.put(0x01, SectionType.TEM); m_mapSectionType.put(0xc0, SectionType.SOF0); m_mapSectionType.put(0xc1, SectionType.SOF1); m_mapSectionType.put(0xc2, SectionType.SOF2); m_mapSectionType.put(0xc3, SectionType.SOF3); m_mapSectionType.put(0xc5, SectionType.SOF5); m_mapSectionType.put(0xc6, SectionType.SOF6); m_mapSectionType.put(0xc7, SectionType.SOF7); m_mapSectionType.put(0xc9, SectionType.SOF9); m_mapSectionType.put(0xca, SectionType.SOF10); m_mapSectionType.put(0xcb, SectionType.SOF11); m_mapSectionType.put(0xcd, SectionType.SOF13); m_mapSectionType.put(0xce, SectionType.SOF14); m_mapSectionType.put(0xcf, SectionType.SOF15); m_mapSectionType.put(0xc4, SectionType.DHT); m_mapSectionType.put(0xc8, SectionType.JPG); m_mapSectionType.put(0xcc, SectionType.DAC); m_mapSectionType.put(0xd0, SectionType.RST0); m_mapSectionType.put(0xd1, SectionType.RST1); m_mapSectionType.put(0xd2, SectionType.RST2); m_mapSectionType.put(0xd3, SectionType.RST3); m_mapSectionType.put(0xd4, SectionType.RST4); m_mapSectionType.put(0xd5, SectionType.RST5); m_mapSectionType.put(0xd6, SectionType.RST6); m_mapSectionType.put(0xd7, SectionType.RST7); m_mapSectionType.put(0xd8, SectionType.SOI); m_mapSectionType.put(0xd9, SectionType.EOI); m_mapSectionType.put(0xda, SectionType.SOS); m_mapSectionType.put(0xdb, SectionType.DQT); m_mapSectionType.put(0xdc, SectionType.DNL); m_mapSectionType.put(0xdd, SectionType.DRI); m_mapSectionType.put(0xde, SectionType.DHP); m_mapSectionType.put(0xdf, SectionType.EXP); m_mapSectionType.put(0xe0, SectionType.APP0); m_mapSectionType.put(0xef, SectionType.APP15); m_mapSectionType.put(0xf0, SectionType.JPG0); m_mapSectionType.put(0xfd, SectionType.JPG13); m_mapSectionType.put(0xfe, SectionType.COM); m_mapSectionType.put(0xff, SectionType.NOP); }; enum ERROR_CODE { ERR_OK, ERR_NOT_JEPG_FILE, }; //SOF0: Start Of Frame 0: class JSOF0 { byte m_byPrecision; byte m_byHeight; byte m_byWidth; byte m_byComponentNum; class JComponent { byte m_byId; byte m_byFactor; byte m_byQTId; } ArrayList
m_arrComponents = new ArrayList
(); } JSOF0 m_oSOF0 = new JSOF0(); //DQT: Define Quantization Table: class JDQT { byte m_byQTInfo; class JQT { byte [] m_arrByte = new byte[64]; } ArrayList
m_arrQT = new ArrayList
(); void ReadQTArray(ByteArrayInputStream bais, int n) throws IOException{ for (int i = 0; i < n + 1; ++i) { JQT qt = new JQT(); bais.read(qt.m_arrByte); m_arrQT.add(qt); } } } ArrayList
m_arrDQT = new ArrayList
(); ...... }
类的开始部分定义了各个段的标示符,接下来对SOF0和DQT段分别定义了内部类,这两个段是我们需要读取数据的段,其他的段我们暂时不分析,直接跳过,当然,也可以增加其他段的内部类,来读取段数据。
四、Java的JPEG类读取数据
接下来是JPEGFile类的读取JPEG数据的部分。
class JPEGFile
{
......
SectionType GetSectionType(int iFirstByte, int iSecondByte) {
iFirstByte = iFirstByte & 0xff;
iSecondByte = iSecondByte & 0xff;
System.out.println("GetSectionType(" + iFirstByte + ", " + iSecondByte + ")");
if (0xff != iFirstByte) return SectionType.NOP;
if (!m_mapSectionType.containsKey(iSecondByte)) return SectionType.NOP;
return m_mapSectionType.get(iSecondByte);
}