Jpg of Windows Phone resolution image size

Source: Internet
Author: User

The previous article introduced how to parse the size of JPG images by using Windows Phone. This article describes how to parse the size of JPG images because the format of JPG images is much more complex than that of png images, therefore, first, we need to clearly understand the data format of the jpg image. The jpg image consists of two parts: SOI and data.
SOI, Start of Image, Image Start, marking Code 2-byte fixed value 0xFFD8.
The data section is divided into many data segments. The general structure of the data segments is as follows.

Segment Data Structure
Name Bytes Description
Segment ID > = 1 0xFF of more than one
Segment type 1 Type encoding (called "tag Code ")
Segment Length 2 Including the content and length of a segment, excluding the ID and type of a segment
Short content <= 65533  

There are 30 types of segments, but only 10 of them must be recognized by all programs. Other types can be ignored. Among so many segments, the size information of the jpg image is stored in the SOF0 (basic image information) segment. Therefore, you need to learn more about the data structure of the SOFO segment.

SOFO Segment Structure
Name Bytes Description
Segment ID 1 0XFF
Segment type 1 0XCO JFIF format: 0XC2
Segment Length 2 Value = 8 + component quantity X 3
Sample precision 1 8 number of digits per sample (most software does not support 12 or 16)
Picture height 2 The Motorola format is used, that is, the high is in the front, the low is in the back
Image Width 2 The Motorola format is used, that is, the high is in the front, the low is in the back

Because we want to parse the width and height information of the jpg image, the SOFO section structure in the above table only lists the structure information up to the width, and other information about the image is not listed here.
Based on the above analysis of the jpg image format, we can take a few steps to parse the size information of the jpg image.
1. Read the two-byte SOI, that is, 0xFFD8. Determine whether the image is a jpg image based on the two bytes. If not, exit the parsing process.

View Code

// Read two-byte SOI, that is, 0xFFD8
Byte [] header = new byte [2];
Stream. Read (header, 0, 2 );
// Determine whether the image is JPG, not to exit the resolution
If (! (Header [0] = 0xFF &&
Header [1] = 0xD8 ))
{
// Not jpg image
Return;
}

2. next, we need to parse the data part of the image. Because the data part is composed of many different data segments, the data segment has some common features, so here we need to make a loop to traverse and find the SOFO Data Segment one by one.

View Code

// Segment type
Int type =-1;
Int ff =-1;
// Record the current read location
Long ps = 0;
// Traverse the segments one by one and find the SOFO segments
Do
{
Do
{
// The start of each new segment is marked as oxff. Find the next new segment.
Ff = stream. ReadByte ();
If (ff <0) // end of the file
{
Return;
}
} While (ff! = 0xff );

Do
{
// One or more oxff intervals exist between segments. bytes after skipping these oxff are segment identifiers.
Type = stream. ReadByte ();
} While (type = 0xff );

// Record the current location
Ps = stream. Position;
Switch (type)
{
Case 0x00:
Case 0x01:
Case 0xD0:
Case 0xD1:
Case 0xD2:
Case 0xD3:
Case 0xD4:
Case 0xD5:
Case 0xD6:
Case 0xD7:
Break;
Case 0xc0: // SOF0 segment (basic image information)
Case 0xc2: // SOF0 segment in JFIF format
{
// Locate the SOFO segment and parse the width and height Information
GetJpgSize (stream );
Return;
}
Default: // skip all other segments
// Get the segment length and skip it directly
Ps = stream. ReadByte () * 256;
Ps = stream. Position + ps + stream. ReadByte ()-2;
Break;
}
If (ps + 1> = stream. Length) // end of the file
{
Return;
}
Stream. Position = ps; // move the pointer
} While (type! = 0xda); // starts scanning rows

3. After finding the SOFO data segment, You can parse the image width and height information.

View Code

/// <Summary>
/// Parse the size of the jpg image
/// </Summary>
/// <Param name = "stream"> </param>
Private void getJpgSize (Stream stream)
{
// Skip two length information and One-byte precision Information
Stream. Seek (3, SeekOrigin. Current );

// The height occupies 2-Byte Low-Level Interchange
Height _ = stream. ReadByte () * 256;
Height _ + = stream. ReadByte ();
// The width occupies 2-Byte Low-Level Interchange
Width _ = stream. ReadByte () * 256;
Width _ + = stream. ReadByte ();
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.