XapThumbnailsProcessor.java
3.88 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
95
96
97
98
99
100
101
102
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.xmp.XMPException
* com.adobe.xmp.XMPIterator
* com.adobe.xmp.XMPMeta
* com.adobe.xmp.XMPMetaFactory
* com.adobe.xmp.options.IteratorOptions
* com.adobe.xmp.properties.XMPProperty
* com.adobe.xmp.properties.XMPPropertyInfo
* com.day.cq.dam.api.Context
* com.day.cq.dam.api.Processor
* com.day.cq.dam.api.ProcessorException
* com.day.image.Layer
* org.apache.commons.codec.binary.Base64
*/
package com.day.cq.dam.commons.thumbnail;
import com.adobe.xmp.XMPException;
import com.adobe.xmp.XMPIterator;
import com.adobe.xmp.XMPMeta;
import com.adobe.xmp.XMPMetaFactory;
import com.adobe.xmp.options.IteratorOptions;
import com.adobe.xmp.properties.XMPProperty;
import com.adobe.xmp.properties.XMPPropertyInfo;
import com.day.cq.dam.api.Context;
import com.day.cq.dam.api.Processor;
import com.day.cq.dam.api.ProcessorException;
import com.day.cq.dam.commons.handler.Filter;
import com.day.cq.dam.commons.handler.FilterStateListener;
import com.day.cq.dam.commons.handler.XPacketFilter;
import com.day.image.Layer;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.codec.binary.Base64;
public class XapThumbnailsProcessor
implements Processor,
FilterStateListener {
private static final String XMP_SCHEMA_NS = "http://ns.adobe.com/xap/1.0/";
private static final String PN_THUMBNAILS = "Thumbnails";
private static final String XMPIMG_SCHEMA_NS = "http://ns.adobe.com/xap/1.0/g/img/";
private static final String PN_IMAGE = "image";
private Context context;
private XPacketFilter xmpFilter;
public XapThumbnailsProcessor(Context context) {
this.context = context;
this.xmpFilter = new XPacketFilter();
this.xmpFilter.setFilterStateListener(this);
this.xmpFilter.setAutoReset(true);
}
public final void process(byte[] buf, int off, int len) throws IOException {
this.xmpFilter.filter(buf, off, len);
}
@Override
public OutputStream started(Filter filter) {
return new ByteArrayOutputStream(8192);
}
@Override
public void ended(Filter filter, OutputStream out) {
ByteArrayOutputStream bos = (ByteArrayOutputStream)out;
byte[] data = bos.toByteArray();
XMPMeta xmp = null;
try {
xmp = XMPMetaFactory.parseFromBuffer((byte[])data);
}
catch (XMPException e) {
String msg = "Unable to get XMP object from input";
this.context.addException(new ProcessorException(msg, (Throwable)e, (Processor)this));
return;
}
try {
XMPIterator iter = xmp.iterator("http://ns.adobe.com/xap/1.0/", "Thumbnails", new IteratorOptions().setJustChildren(true));
while (iter.hasNext()) {
XMPPropertyInfo thumbnail = (XMPPropertyInfo)iter.next();
XMPProperty image = xmp.getStructField("http://ns.adobe.com/xap/1.0/", thumbnail.getPath(), "http://ns.adobe.com/xap/1.0/g/img/", "image");
if (image == null) continue;
byte[] encoded = ((String)image.getValue()).getBytes();
byte[] decoded = Base64.decodeBase64((byte[])encoded);
this.context.addThumbnail(new Layer((InputStream)new ByteArrayInputStream(decoded)).getImage());
}
}
catch (XMPException e) {
String msg = "Unable to locate image inside thumbnail";
this.context.addException(new ProcessorException(msg, (Throwable)e, (Processor)this));
}
catch (IOException e) {
String msg = "Unable to get buffered image from JPEG";
this.context.addException(new ProcessorException(msg, (Throwable)e, (Processor)this));
}
}
}