Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): return and store exif data in blob/file #14074

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ public void onPictureTaken(byte[] data, Camera camera)
previewRect.put(TiC.PROPERTY_HEIGHT, 0);
}
response.put("previewRect", previewRect);

response.put("exif", blob.getExif());
successCallback.callAsync(callbackContext, response);
}
} catch (Throwable t) {
Expand Down
30 changes: 30 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import android.graphics.Matrix;
import android.media.ThumbnailUtils;
import android.util.Base64;

import androidx.exifinterface.media.ExifInterface;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand All @@ -28,6 +31,7 @@
import org.appcelerator.kroll.util.KrollStreamHelper;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.util.TiBlobLruCache;
import org.appcelerator.titanium.util.TiExifHelper;
import org.appcelerator.titanium.util.TiImageHelper;
import org.appcelerator.titanium.util.TiMimeTypeHelper;

Expand Down Expand Up @@ -60,6 +64,7 @@ public class TiBlob extends KrollProxy
private int uprightWidth;
private int uprightHeight;
private Object rotation;
private static ExifInterface exifData;

// This handles the memory cache of images.
private final TiBlobLruCache mMemoryCache = TiBlobLruCache.getInstance();
Expand Down Expand Up @@ -95,6 +100,10 @@ public static TiBlob blobFromString(String data)
*/
public static TiBlob blobFromFile(TiBaseFile file)
{
try (InputStream stream = file.getInputStream()) {
// set exif data from stream if available
exifData = new ExifInterface(stream);
} catch (Exception ex) {}
return blobFromFile(file, TiMimeTypeHelper.getMimeType(file.nativePath()));
}

Expand All @@ -110,6 +119,12 @@ public static TiBlob blobFromFile(TiBaseFile file, String mimeType)
if (mimeType == null) {
mimeType = TiMimeTypeHelper.getMimeType(file.nativePath());
}

try (InputStream stream = file.getInputStream()) {
// set exif data from stream if available
exifData = new ExifInterface(stream);
} catch (Exception ex) {}

TiBlob blob = new TiBlob(TYPE_FILE, file, mimeType);
blob.loadBitmapInfo();
return blob;
Expand Down Expand Up @@ -758,6 +773,21 @@ public TiBlob imageAsResized(Number width, Number height)
return null;
}

@Kroll.getProperty
public KrollDict getExif()
{
KrollDict kd = new KrollDict();
String[] attributes = TiExifHelper.attributes;
if (exifData != null) {
for (int i = 0; i < attributes.length; i++) {
String value = exifData.getAttribute(attributes[i]);
if (value != null)
kd.put(attributes[i], value);
}
}
return kd;
}

@Kroll.method
public TiBlob imageAsCompressed(Number compressionQuality)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@
import java.util.Iterator;
import java.util.List;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFileFactory;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiExifHelper;
import org.appcelerator.titanium.util.TiFileHelper2;
import org.appcelerator.titanium.util.TiUrl;

import ti.modules.titanium.stream.FileStreamProxy;

import android.content.ContentResolver;
import android.net.Uri;

import androidx.exifinterface.media.ExifInterface;

@Kroll.proxy
public class TiFileProxy extends KrollProxy
{
Expand Down Expand Up @@ -291,7 +297,17 @@ public boolean write(Object[] args)
}

if (args[0] instanceof TiBlob) {
ContentResolver contentResolver = TiApplication.getInstance().getContentResolver();
tbf.write((TiBlob) args[0], append);
ExifInterface exifData = new ExifInterface(contentResolver
.openFileDescriptor(Uri.fromFile(tbf.getNativeFile()), "rw").getFileDescriptor());

String[] attributes = TiExifHelper.attributes;
KrollDict kd = ((TiBlob) args[0]).getExif();
for (int i = 0; i < attributes.length; i++) {
exifData.setAttribute(attributes[i], kd.getString(attributes[i]));
}
exifData.saveAttributes();
} else if (args[0] instanceof String) {
tbf.write((String) args[0], append);
} else if (args[0] instanceof TiFileProxy) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.appcelerator.titanium.util;

import androidx.exifinterface.media.ExifInterface;

public class TiExifHelper
{
public static String[] attributes = new String[]
{
ExifInterface.TAG_DATETIME,
ExifInterface.TAG_DATETIME_DIGITIZED,
ExifInterface.TAG_EXPOSURE_TIME,
ExifInterface.TAG_FLASH,
ExifInterface.TAG_FOCAL_LENGTH,
ExifInterface.TAG_GPS_ALTITUDE,
ExifInterface.TAG_GPS_ALTITUDE_REF,
ExifInterface.TAG_GPS_DATESTAMP,
ExifInterface.TAG_GPS_LATITUDE,
ExifInterface.TAG_GPS_LATITUDE_REF,
ExifInterface.TAG_GPS_LONGITUDE,
ExifInterface.TAG_GPS_LONGITUDE_REF,
ExifInterface.TAG_GPS_PROCESSING_METHOD,
ExifInterface.TAG_GPS_TIMESTAMP,
ExifInterface.TAG_IMAGE_LENGTH,
ExifInterface.TAG_IMAGE_WIDTH,
ExifInterface.TAG_MAKE,
ExifInterface.TAG_MODEL,
ExifInterface.TAG_ORIENTATION,
ExifInterface.TAG_SUBSEC_TIME,
ExifInterface.TAG_WHITE_BALANCE
};
}
7 changes: 7 additions & 0 deletions apidoc/Titanium/Blob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ properties:
since: {android: "12.3.0"}
permission: read-only

- name: exif
type: Object
summary: In the camera `success` event you will get the EXIF information from the media blob if available.
platforms: [android]
since: {android: "12.7.0"}
permission: read-only

methods:
- name: toString
returns:
Expand Down