comparison src/name/blackcap/exifwasher/exiv2/native.cpp @ 0:db63d01a23c6

JNI calls and test case (finally!) seem to work.
author David Barts <n5jrn@me.com>
date Tue, 31 Mar 2020 13:24:48 -0700
parents
children 42277ce58ace
comparison
equal deleted inserted replaced
-1:000000000000 0:db63d01a23c6
1 #include <jni.h>
2 #include <exiv2/exiv2.hpp>
3 #include <iostream>
4 #include <iomanip>
5 #include <cassert>
6
7 /* Functions for class name_blackcap_exifwasher_exiv2_Image */
8
9 #ifndef _Included_name_blackcap_exifwasher_exiv2_Image
10 #define _Included_name_blackcap_exifwasher_exiv2_Image
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14
15 /*
16 * Utility function to get pointer field.
17 */
18 jlong getPointer(JNIEnv *jEnv, jobject jThis) {
19 return jEnv->GetLongField(jThis, jEnv->GetFieldID(jEnv->GetObjectClass(jThis), "pointer", "J"));
20 }
21
22 /*
23 * Class: name_blackcap_exifwasher_exiv2_Image
24 * Method: _ctor
25 * Signature: (Ljava/lang/String;)J
26 */
27 JNIEXPORT jlong JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1ctor
28 (JNIEnv *jEnv, jobject jThis, jstring path) {
29 const char *cPath = jEnv->GetStringUTFChars(path, NULL);
30 jlong ret = 0;
31 try {
32 ret = reinterpret_cast<jlong> (Exiv2::ImageFactory::open(cPath).release());
33 } catch (...) {
34 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
35 const char *pfx = "unable to open ";
36 char *message = (char *) malloc(strlen(cPath) + strlen(pfx) + 1);
37 strcpy(message, pfx);
38 strcat(message, cPath);
39 jEnv->ThrowNew(ex, message);
40 free(message);
41 }
42 jEnv->ReleaseStringUTFChars(path, cPath);
43 return ret;
44 }
45
46 /*
47 * Class: name_blackcap_exifwasher_exiv2_Image
48 * Method: _writeMetadata
49 * Signature: ()V
50 */
51 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1writeMetadata
52 (JNIEnv *jEnv, jobject jThis) {
53 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
54 try {
55 image->writeMetadata();
56 } catch (...) {
57 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
58 jEnv->ThrowNew(ex, "unable to write metadata");
59 }
60 }
61
62 /*
63 * Class: name_blackcap_exifwasher_exiv2_Image
64 * Method: _getMetadata
65 * Signature: ()J
66 */
67 JNIEXPORT jlong JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1getMetadata
68 (JNIEnv *jEnv, jobject jThis) {
69 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
70 try {
71 image->readMetadata();
72 } catch (...) {
73 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
74 jEnv->ThrowNew(ex, "unable to read metadata");
75 }
76 return reinterpret_cast<jlong> (&(image->exifData()));
77 }
78
79 /*
80 * Class: name_blackcap_exifwasher_exiv2_Image
81 * Method: _dtor
82 * Signature: ()V
83 */
84 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1dtor
85 (JNIEnv *jEnv, jobject jThis) {
86 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
87 delete image;
88 }
89
90 #ifdef __cplusplus
91 }
92 #endif
93 #endif
94 /* Header for class name_blackcap_exifwasher_exiv2_ExifData */
95
96 #ifndef _Included_name_blackcap_exifwasher_exiv2_ExifData
97 #define _Included_name_blackcap_exifwasher_exiv2_ExifData
98 #ifdef __cplusplus
99 extern "C" {
100 #endif
101 /*
102 * Class: name_blackcap_exifwasher_exiv2_ExifData
103 * Method: _erase
104 * Signature: (Ljava/lang/String;)V
105 */
106 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1erase
107 (JNIEnv *jEnv, jobject jThis, jstring key) {
108 Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis));
109 const char *cKey = jEnv->GetStringUTFChars(key, NULL);
110 Exiv2::ExifData::iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey)));
111 try {
112 data->erase(found);
113 } catch (...) {
114 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
115 const char *pfx = "unable to delete ";
116 char *message = (char *) malloc(strlen(cKey) + strlen(pfx) + 1);
117 strcpy(message, pfx);
118 strcat(message, cKey);
119 jEnv->ThrowNew(ex, message);
120 free(message);
121 }
122 jEnv->ReleaseStringUTFChars(key, cKey);
123 }
124
125 /*
126 * Class: name_blackcap_exifwasher_exiv2_ExifData
127 * Method: _value
128 * Signature: (Ljava/lang/String;)Lname/blackcap/exifwasher/exiv2/ExifData/Value;
129 */
130 JNIEXPORT jobject JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1value
131 (JNIEnv *jEnv, jobject jThis, jstring key) {
132 Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis));
133 const char *cKey = jEnv->GetStringUTFChars(key, NULL);
134 Exiv2::ExifData::const_iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey)));
135 jEnv->ReleaseStringUTFChars(key, cKey);
136 if (found == data->end()) {
137 return NULL;
138 }
139 jclass klass = jEnv->FindClass("name/blackcap/exifwasher/exiv2/ExifData$Value");
140 jstring type = jEnv->NewStringUTF(found->typeName());
141 jstring value = jEnv->NewStringUTF(found->toString().c_str());
142 jmethodID method = jEnv->GetMethodID(klass, "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
143 return jEnv->NewObject(klass, method, type, value);
144 }
145
146 /*
147 * Class: name_blackcap_exifwasher_exiv2_ExifData
148 * Method: _keys
149 * Signature: ()[Ljava/lang/String;
150 */
151 JNIEXPORT jobjectArray JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1keys
152 (JNIEnv *jEnv, jobject jThis) {
153 Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis));
154 jclass klass = jEnv->FindClass("java/lang/String");
155 jobjectArray ret = jEnv->NewObjectArray(data->count(), klass, NULL);
156 Exiv2::ExifData::const_iterator end = data->end();
157 jsize j = 0;
158 for (Exiv2::ExifData::const_iterator i = data->begin(); i != end; ++i) {
159 jEnv->SetObjectArrayElement(ret, j++, jEnv->NewStringUTF(i->key().c_str()));
160 }
161 return ret;
162 }
163
164 #ifdef __cplusplus
165 }
166 #endif
167 #endif