arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-camera/src/android/ExifHelper.java
plugins/cordova-plugin-camera/src/android/ExifHelper.javaRaw
  1/*
  2       Licensed to the Apache Software Foundation (ASF) under one
  3       or more contributor license agreements.  See the NOTICE file
  4       distributed with this work for additional information
  5       regarding copyright ownership.  The ASF licenses this file
  6       to you under the Apache License, Version 2.0 (the
  7       "License"); you may not use this file except in compliance
  8       with the License.  You may obtain a copy of the License at
  9
 10         http://www.apache.org/licenses/LICENSE-2.0
 11
 12       Unless required by applicable law or agreed to in writing,
 13       software distributed under the License is distributed on an
 14       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15       KIND, either express or implied.  See the License for the
 16       specific language governing permissions and limitations
 17       under the License.
 18*/
 19package org.apache.cordova.camera;
 20
 21import java.io.IOException;
 22
 23import android.media.ExifInterface;
 24
 25public class ExifHelper {
 26    private String aperture = null;
 27    private String datetime = null;
 28    private String exposureTime = null;
 29    private String flash = null;
 30    private String focalLength = null;
 31    private String gpsAltitude = null;
 32    private String gpsAltitudeRef = null;
 33    private String gpsDateStamp = null;
 34    private String gpsLatitude = null;
 35    private String gpsLatitudeRef = null;
 36    private String gpsLongitude = null;
 37    private String gpsLongitudeRef = null;
 38    private String gpsProcessingMethod = null;
 39    private String gpsTimestamp = null;
 40    private String iso = null;
 41    private String make = null;
 42    private String model = null;
 43    private String orientation = null;
 44    private String whiteBalance = null;
 45
 46    private ExifInterface inFile = null;
 47    private ExifInterface outFile = null;
 48
 49    /**
 50     * The file before it is compressed
 51     *
 52     * @param filePath
 53     * @throws IOException
 54     */
 55    public void createInFile(String filePath) throws IOException {
 56        this.inFile = new ExifInterface(filePath);
 57    }
 58
 59    /**
 60     * The file after it has been compressed
 61     *
 62     * @param filePath
 63     * @throws IOException
 64     */
 65    public void createOutFile(String filePath) throws IOException {
 66        this.outFile = new ExifInterface(filePath);
 67    }
 68
 69    /**
 70     * Reads all the EXIF data from the input file.
 71     */
 72    public void readExifData() {
 73        this.aperture = inFile.getAttribute(ExifInterface.TAG_APERTURE);
 74        this.datetime = inFile.getAttribute(ExifInterface.TAG_DATETIME);
 75        this.exposureTime = inFile.getAttribute(ExifInterface.TAG_EXPOSURE_TIME);
 76        this.flash = inFile.getAttribute(ExifInterface.TAG_FLASH);
 77        this.focalLength = inFile.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);
 78        this.gpsAltitude = inFile.getAttribute(ExifInterface.TAG_GPS_ALTITUDE);
 79        this.gpsAltitudeRef = inFile.getAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF);
 80        this.gpsDateStamp = inFile.getAttribute(ExifInterface.TAG_GPS_DATESTAMP);
 81        this.gpsLatitude = inFile.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
 82        this.gpsLatitudeRef = inFile.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
 83        this.gpsLongitude = inFile.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
 84        this.gpsLongitudeRef = inFile.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
 85        this.gpsProcessingMethod = inFile.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD);
 86        this.gpsTimestamp = inFile.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP);
 87        this.iso = inFile.getAttribute(ExifInterface.TAG_ISO);
 88        this.make = inFile.getAttribute(ExifInterface.TAG_MAKE);
 89        this.model = inFile.getAttribute(ExifInterface.TAG_MODEL);
 90        this.orientation = inFile.getAttribute(ExifInterface.TAG_ORIENTATION);
 91        this.whiteBalance = inFile.getAttribute(ExifInterface.TAG_WHITE_BALANCE);
 92    }
 93
 94    /**
 95     * Writes the previously stored EXIF data to the output file.
 96     *
 97     * @throws IOException
 98     */
 99    public void writeExifData() throws IOException {
100        // Don't try to write to a null file
101        if (this.outFile == null) {
102            return;
103        }
104
105        if (this.aperture != null) {
106            this.outFile.setAttribute(ExifInterface.TAG_APERTURE, this.aperture);
107        }
108        if (this.datetime != null) {
109            this.outFile.setAttribute(ExifInterface.TAG_DATETIME, this.datetime);
110        }
111        if (this.exposureTime != null) {
112            this.outFile.setAttribute(ExifInterface.TAG_EXPOSURE_TIME, this.exposureTime);
113        }
114        if (this.flash != null) {
115            this.outFile.setAttribute(ExifInterface.TAG_FLASH, this.flash);
116        }
117        if (this.focalLength != null) {
118            this.outFile.setAttribute(ExifInterface.TAG_FOCAL_LENGTH, this.focalLength);
119        }
120        if (this.gpsAltitude != null) {
121            this.outFile.setAttribute(ExifInterface.TAG_GPS_ALTITUDE, this.gpsAltitude);
122        }
123        if (this.gpsAltitudeRef != null) {
124            this.outFile.setAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF, this.gpsAltitudeRef);
125        }
126        if (this.gpsDateStamp != null) {
127            this.outFile.setAttribute(ExifInterface.TAG_GPS_DATESTAMP, this.gpsDateStamp);
128        }
129        if (this.gpsLatitude != null) {
130            this.outFile.setAttribute(ExifInterface.TAG_GPS_LATITUDE, this.gpsLatitude);
131        }
132        if (this.gpsLatitudeRef != null) {
133            this.outFile.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, this.gpsLatitudeRef);
134        }
135        if (this.gpsLongitude != null) {
136            this.outFile.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, this.gpsLongitude);
137        }
138        if (this.gpsLongitudeRef != null) {
139            this.outFile.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, this.gpsLongitudeRef);
140        }
141        if (this.gpsProcessingMethod != null) {
142            this.outFile.setAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD, this.gpsProcessingMethod);
143        }
144        if (this.gpsTimestamp != null) {
145            this.outFile.setAttribute(ExifInterface.TAG_GPS_TIMESTAMP, this.gpsTimestamp);
146        }
147        if (this.iso != null) {
148            this.outFile.setAttribute(ExifInterface.TAG_ISO, this.iso);
149        }
150        if (this.make != null) {
151            this.outFile.setAttribute(ExifInterface.TAG_MAKE, this.make);
152        }
153        if (this.model != null) {
154            this.outFile.setAttribute(ExifInterface.TAG_MODEL, this.model);
155        }
156        if (this.orientation != null) {
157            this.outFile.setAttribute(ExifInterface.TAG_ORIENTATION, this.orientation);
158        }
159        if (this.whiteBalance != null) {
160            this.outFile.setAttribute(ExifInterface.TAG_WHITE_BALANCE, this.whiteBalance);
161        }
162
163        this.outFile.saveAttributes();
164    }
165
166    public int getOrientation() {
167        int o = Integer.parseInt(this.orientation);
168
169        if (o == ExifInterface.ORIENTATION_NORMAL) {
170            return 0;
171        } else if (o == ExifInterface.ORIENTATION_ROTATE_90) {
172            return 90;
173        } else if (o == ExifInterface.ORIENTATION_ROTATE_180) {
174            return 180;
175        } else if (o == ExifInterface.ORIENTATION_ROTATE_270) {
176            return 270;
177        } else {
178            return 0;
179        }
180    }
181
182    public void resetOrientation() {
183        this.orientation = "" + ExifInterface.ORIENTATION_NORMAL;
184    }
185}