forked from m0ver/tinystruct-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage2.java
More file actions
94 lines (75 loc) · 2.4 KB
/
image2.java
File metadata and controls
94 lines (75 loc) · 2.4 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
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
package tinystruct.examples;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.tinystruct.AbstractApplication;
import org.tinystruct.ApplicationContext;
import org.tinystruct.ApplicationException;
import org.tinystruct.system.ApplicationManager;
/**
* Image to Base64 conversion
* Usage: dispatcher --image-path=/path/to/image image2base64
* @author m0ver
*/
public class image2 extends AbstractApplication {
private String imagePath;
@Override
public void init() {
this.setAction("image2base64", "image2base64");
}
@Override
public String version() {
return "1.0";
}
public String image2base64() throws ApplicationException {
if(this.context.getAttribute("--image-path")==null || this.context.getAttribute("--image-path").toString().trim().length()==0) {
throw new ApplicationException("Invalid file");
}
this.imagePath = this.context.getAttribute("--image-path").toString();
return this.image2base64(this.imagePath);
}
public String image2base64(String f) throws ApplicationException {
File file = new File(f);
String name = file.getName();
if(!name.endsWith(".jpeg") && !name.endsWith(".jpg")&& !name.endsWith(".png")&& !name.endsWith(".gif")&& !name.endsWith(".bmp"))
{
throw new ApplicationException("Invalid file");
}
ByteArrayOutputStream ous = null;
FileInputStream fs = null;
byte[] data = new byte[]{};
try {
byte[] buffer = new byte[4096];
ous = new ByteArrayOutputStream();
int read = 0;
fs = new FileInputStream(file);
while ((read = fs.read(buffer)) != -1) {
ous.write(buffer, 0, read);
}
data = ous.toByteArray();
if(data.length > 0)
return "<img src=\"data:image/png;base64," + org.tinystruct.system.util.Base64.encode(data) + "\" />";
} catch (FileNotFoundException e) {
throw new ApplicationException(e.getMessage(), e);
} catch (IOException e) {
throw new ApplicationException(e.getMessage(), e);
} finally {
try {
if (ous != null)
ous.close();
if (fs != null)
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void main(String[]args) throws ApplicationException {
ApplicationContext ctx = new ApplicationContext();
ApplicationManager.install(new image2());
ApplicationManager.call("image2base64", ctx);
}
}