• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.gallery3d.filtershow.tools;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 import android.util.Log;
22 
23 import com.adobe.xmp.XMPException;
24 import com.adobe.xmp.XMPMeta;
25 import com.adobe.xmp.XMPMetaFactory;
26 import com.android.gallery3d.R;
27 import com.android.gallery3d.common.Utils;
28 import com.android.gallery3d.filtershow.imageshow.PrimaryImage;
29 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
30 import com.android.gallery3d.util.XmpUtilHelper;
31 
32 import java.io.File;
33 import java.io.FileNotFoundException;
34 import java.io.InputStream;
35 
36 public class XmpPresets {
37     public static final String
38             XMP_GOOGLE_FILTER_NAMESPACE = "http://ns.google.com/photos/1.0/filter/";
39     public static final String XMP_GOOGLE_FILTER_PREFIX = "AFltr";
40     public static final String XMP_SRC_FILE_URI = "SourceFileUri";
41     public static final String XMP_FILTERSTACK = "filterstack";
42 
43     private static final String LOGTAG = "XmpPresets";
44 
45     public static class XMresults {
46         public String presetString;
47         public ImagePreset preset;
48         public Uri originalimage;
49     }
50 
51     static {
52         try {
53             XMPMetaFactory.getSchemaRegistry().registerNamespace(
54                     XMP_GOOGLE_FILTER_NAMESPACE, XMP_GOOGLE_FILTER_PREFIX);
55         } catch (XMPException e) {
56             Log.e(LOGTAG, "Register XMP name space failed", e);
57         }
58     }
59 
writeFilterXMP( Context context, Uri srcUri, File dstFile, ImagePreset preset)60     public static void writeFilterXMP(
61             Context context, Uri srcUri, File dstFile, ImagePreset preset) {
62         InputStream is = null;
63         XMPMeta xmpMeta = null;
64         try {
65             is = context.getContentResolver().openInputStream(srcUri);
66             xmpMeta = XmpUtilHelper.extractXMPMeta(is);
67         } catch (FileNotFoundException e) {
68 
69         } finally {
70             Utils.closeSilently(is);
71         }
72 
73         if (xmpMeta == null) {
74             xmpMeta = XMPMetaFactory.create();
75         }
76         try {
77             xmpMeta.setProperty(XMP_GOOGLE_FILTER_NAMESPACE,
78                     XMP_SRC_FILE_URI, srcUri.toString());
79             xmpMeta.setProperty(XMP_GOOGLE_FILTER_NAMESPACE,
80                     XMP_FILTERSTACK, preset.getJsonString(ImagePreset.JASON_SAVED));
81         } catch (XMPException e) {
82             Log.v(LOGTAG, "Write XMP meta to file failed:" + dstFile.getAbsolutePath());
83             return;
84         }
85 
86         if (!XmpUtilHelper.writeXMPMeta(dstFile.getAbsolutePath(), xmpMeta)) {
87             Log.v(LOGTAG, "Write XMP meta to file failed:" + dstFile.getAbsolutePath());
88         }
89     }
90 
extractXMPData( Context context, PrimaryImage mPrimaryImage, Uri uriToEdit)91     public static XMresults extractXMPData(
92             Context context, PrimaryImage mPrimaryImage, Uri uriToEdit) {
93         XMresults ret = new XMresults();
94 
95         InputStream is = null;
96         XMPMeta xmpMeta = null;
97         try {
98             is = context.getContentResolver().openInputStream(uriToEdit);
99             xmpMeta = XmpUtilHelper.extractXMPMeta(is);
100         } catch (FileNotFoundException e) {
101         } finally {
102             Utils.closeSilently(is);
103         }
104 
105         if (xmpMeta == null) {
106             return null;
107         }
108 
109         try {
110             String strSrcUri = xmpMeta.getPropertyString(XMP_GOOGLE_FILTER_NAMESPACE,
111                     XMP_SRC_FILE_URI);
112 
113             if (strSrcUri != null) {
114                 String filterString = xmpMeta.getPropertyString(XMP_GOOGLE_FILTER_NAMESPACE,
115                         XMP_FILTERSTACK);
116 
117                 Uri srcUri = Uri.parse(strSrcUri);
118                 ret.originalimage = srcUri;
119 
120                 ret.preset = new ImagePreset();
121                 ret.presetString = filterString;
122                 boolean ok = ret.preset.readJsonFromString(filterString);
123                 if (!ok) {
124                     return null;
125                 }
126                 return ret;
127             }
128         } catch (XMPException e) {
129             e.printStackTrace();
130         }
131 
132         return null;
133     }
134 }
135