-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadNinePatchChunk.java
More file actions
60 lines (58 loc) · 2.18 KB
/
Copy pathReadNinePatchChunk.java
File metadata and controls
60 lines (58 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
private void testNinePatch() {
// String pngName = "addto_bookmarks_bg_complied.9.png";
String pngName = "barcode_actionbar_pressed_background_complied.9.png";
try {
InputStream is = getResources().getAssets().open(pngName);
// Bitmap bitmap = BitmapFactory.decodeStream(is);
// byte[] chunkData1 = bitmap.getNinePatchChunk();
byte[] chunkData2 = loadNinePatchChunk(is);
NinePatchChunk mChunk = chunkData2 == null ? null : NinePatchChunk
.deserialize(chunkData2);
if (mChunk == null) {
throw new RuntimeException("invalid nine-patch image: " + pngName);
}
printArray(mChunk.mDivX);
printArray(mChunk.mDivY);
System.out.println(mChunk.mPaddings.toShortString());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* PNG Chunk struct
* <a href="http://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_PNG_files">The Metadata in PNG files</a>
*
* +--------+---------+
* | Length | 4 bytes |
* +--------+---------+
* | Chunk | 4 bytes |
* | type | |
* +--------+---------+
* | Chunk | Length |
* | data | bytes |
* +--------+---------+
* | CRC | 4 bytes |
* +--------+---------+
*
* @param pngName
* @return chunk
* @throws IOException
*/
private byte[] loadNinePatchChunk(InputStream is) throws IOException {
IntReader reader = new IntReader(is, true);
// check PNG signature
// A PNG always starts with an 8-byte signature: 137 80 78 71 13 10 26 10 (decimal values).
if (reader.readInt() != 0x89504e47 || reader.readInt() != 0x0D0A1A0A) {
return null;
}
while (true) {
int length = reader.readInt();
int type = reader.readInt();
// check for nine patch chunk type (npTc)
if (type != 0x6E705463) {
reader.skip(length + 4/*crc*/);
continue;
}
return reader.readByteArray(length);
}
}