-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageResource.java
147 lines (127 loc) · 4.45 KB
/
ImageResource.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.dynamicpdf.api;
import java.io.InputStream;
/**
* Represents an image resource used to create an <code>ImageInput</code>
* object to create PDF from images.
*/
public class ImageResource extends Resource {
ResourceType resourceType = ResourceType.IMAGE;
String mimeType;
/**
* Initializes a new instance of the <code>ImageResource</code> class.
* @param filePath The image file path.
* @param resourceName The name of the resource.
*/
public ImageResource(String filePath, String resourceName) {
super(filePath, resourceName);
}
/**
* Initializes a new instance of the <code>ImageResource</code> class.
* @param filePath The image file path.
*/
public ImageResource(String filePath) {
super(filePath, null);
}
/**
* Initializes a new instance of the <code>ImageResource</code> class.
* @param value The byte array of the image file.
* @param resourceName The name of the resource.
*/
public ImageResource(byte[] value, String resourceName) {
super(value, resourceName);
}
/**
* Initializes a new instance of the <code>ImageResource</code> class.
* @param value The byte array of the image file.
*/
public ImageResource(byte[] value) {
super(value, null);
}
/**
* Initializes a new instance of the <code>ImageResource</code> class.
* @param data The stream of the image file.
* @param resourceName The name of the resource.
*/
public ImageResource(InputStream data, String resourceName) {
super(data, resourceName);
}
/**
* Initializes a new instance of the <code>ImageResource</code> class.
* @param data The stream of the image file.
*/
public ImageResource(InputStream data) {
super(data, null);
}
ResourceType getType() {
return resourceType;
}
String getMimeType() {
return mimeType;
}
void setMimeType(String value) {
mimeType = value;
}
String getFileExtension() {
byte[] fileHeader = new byte[16];
System.arraycopy(getData(), 0, fileHeader, 0, 16);
if (isPngImage(fileHeader)) {
mimeType = "image/png";
return ".png";
} else if (isJpegImage(fileHeader)) {
mimeType = "image/jpeg";
return ".jpeg";
} else if (isGifImage(fileHeader)) {
mimeType = "image/gif";
return ".gif";
} else if (isTiffImage(fileHeader)) {
mimeType = "image/tiff";
return ".tiff";
} else if (isJpeg2000Image(fileHeader)) {
mimeType = "image/jpeg";
return ".jpeg";
} else if (isValidBitmapImage(fileHeader)) {
mimeType = "image/bmp";
return ".bmp";
} else {
throw new EndpointException("Not supported image type or invalid image.");
}
}
static boolean isJpeg2000Image(byte[] header) {
return ( checkBytes(header[0])==0x00 && checkBytes(header[1])==0x00
&& checkBytes(header[2])==0x00 && checkBytes(header[3])==0x0C
&& checkBytes(header[4])==0x6A && checkBytes(header[5])==0x50
&& ( checkBytes(header[6])==0x1A || checkBytes(header[6])==0x20 )
&& ( checkBytes(header[7])==0x1A || checkBytes(header[7])==0x20 )
&& checkBytes(header[8])==0x0D && checkBytes(header[9])==0x0A
&& checkBytes(header[10])==0x87 && checkBytes(header[11])==0x0A )
||( checkBytes(header[0])==0xFF && checkBytes(header[1])==0x4F
&& checkBytes(header[2])==0xFF && checkBytes(header[3])==0x51
&& checkBytes(header[6])==0x00 && checkBytes(header[7])==0x00 );
}
static boolean isPngImage(byte[] header) {
return checkBytes(header[0])==0x89 && checkBytes(header[1])==0x50
&& checkBytes(header[2])==0x4E && checkBytes(header[3])==0x47
&& checkBytes(header[4])==0x0D && checkBytes(header[5])==0x0A
&& checkBytes(header[6])==0x1A && checkBytes(header[7])==0x0A;
}
static boolean isTiffImage(byte[] header) {
return (checkBytes(header[0])==0x49 && checkBytes(header[1])==0x49
&& checkBytes(header[2])==0x2A && checkBytes(header[3])==0x00)
|| (checkBytes(header[0])==0x4D && checkBytes(header[1])==0x4D
&& checkBytes(header[2])==0x00 && checkBytes(header[3])==0x2A);
}
static boolean isGifImage(byte[] header) {
return header[0] == 0x47 && header[1] == 0x49 && header[2] == 0x46 && header[3] == 0x38 && (header[4] == 0x37 || header[4] == 0x39) && header[5] == 0x61;
}
static boolean isJpegImage(byte[] header) {
return checkBytes(header[0]) == 0xFF && checkBytes(header[1]) == 0xD8 && checkBytes(header[2]) == 0xFF;
}
static boolean isValidBitmapImage(byte[] header) {
return header[0] == 0x42 && header[1] == 0x4D;
}
private static int checkBytes( byte bytVal ) {
int val = (int)bytVal;
if( val < 0 ) { val = 256 + val; }
return val;
}
}