Delphi to Java (biometrics) - java

I'm developing a tool for recognition biometric and I'm using the provided SDK (*.dll), developed in Delphi.
For the access at dll, I'm using JNA.
The template of the digital (the most important part) is an object which refers to this passage(in Delphi):
type
CIS_Digital = packed record
intSize: integer;
pDigital: Pointer
end;
pCIS_Digital = ^CIS_Digital;
How to develop something equivalent in Java?
Thanks.

Basically, the following call DLL functions:
SDK_CIS_Iniciar(int cnpj, int detectaFake);
SDK_CIS_LerDigital(pCIS_Digital digital);
SDK_CIS_CompararDigital(pCIS_Digital sample1, pCIS_Digital sample2);
SDK_CIS_Finalizar ();
Looking at the example provided, developed in Delphi, I saw that the pCIS_Digital object, which is passed on SDK_CIS_LerDigital () and SDK_CIS_CompararDigital () references the code snippet:
type
CIS_Digital = packed record
intSize: integer;
pDigital: Pointer
end;
pCIS_Digital = ^CIS_Digital;
In the example developed in Delphi, before calling the SDK_CIS_LerDigital () method, the pCIS_Digital object is instantiated.
That is, it (the object parameter) will emptiness and is "filled" by function.
The reader is TechMag BioFlex (http://www.techmag.com.br/bioflex/), which is based on Futronic readers (SF-80).
Searching, I saw that the pCIS_Digital object (code snippet), makes memory access to read the information that the reader writes it writes.
After much research, I think I should develop an equivalent object in Java, extending the Structure or Memory class, of the JNA.

Related

How to pass functions as arguments from Dart/Flutter to Android native code?

I'd like to create a Java-based SDK that can be invoked from Flutter, the Flutter side needs to plug into the SDK by providing callbacks to be executed at different stages of the processing, ideally something like:
Future<Result> = doPayment(
amount: 100.00,
currency: 'USD',
onPasscodeEntry: () => _renderInputBox(),
onValidation: () => _doValidation()
);
the processing itself takes some time etc so a Future<PaymentResult> pattern makes sense, the application should react to some points of the processing, so it makes sense to pass in callbacks.
Problems:
how to pass functions to Java code? As far as I can tell all the Java-passable method arguments in Flutter are serializable-ish objects
(less critical/optional) how to return a POJO and handle it in Dart? Judging from the specs I should encode (within Java) the result in a ArrayList/HashMap or as a JSON string and then create the equivalent constructor in Dart that takes in List/Map ? This seems a bit unwieldy, so I'm asking if there's a simpler way, preferably without maintaining a Java & Dart variants of the same object
You can not pass a function.
You can create a map from a function name to a function reference and then pass the function name and look up the function in by name in the map and call it.
You also can't pass a POJO.
You need to serialize it to JSON (or some other supported format) and deserialize it on the other side of the channel. For that you need to have to POJO class implemented in Dart and in Java.
Packages like protobuf or flatbuffer allow to geneerate code for different languages based on an reduced language to specify the data structure.

How to call ALSA allocation #define from JNA?

I am trying to use JNA to call ALSA library functions from Java. I have been successful calling the basic open/close APIs snd_pcm_open() and snd_pcm_close() so I know I have the library loading OK and the basics right.
The first thing my application needs to do with ALSA is call snd_pcm_hw_params_alloca() to allocate a hardware parameter structure. But this is defined in the C header file as a macro, not a symbol:
#define snd_pcm_hw_params_alloca(ptr) __snd_alloca(ptr, snd_pcm_hw_params)
For a C application it would be called like this:
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(&params);
and "params" is then passed to many subsequent API calls.
I am not much of a C expert, I cannot figure out exactly what this is doing. I expected a memory allocation of a structure, but the next line in the header file defines snd_pcm_hw_params like:
int snd_pcm_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
So does that mean the macro is returning a pointer to a function? How can I model this in JPA?
This function returns a pointer to a structure whose definition you do not know. (This is why you need all those accessor functions.)
alloca() allocates memory from the stack, which implies that it's freed as soon as the current function returns. This means that you cannot wrap this into a function to be called from Java.
Instead of snd_pcm_hw_params_alloca(), use snd_pcm_hw_params_malloc(), and don't forget to call snd_pcm_hw_params_free() at the correct time.

Casting a Delphi interface to TObject* in C++ Builder and proper ClassName() function call

I am trying to access Java classes dynamically from *.apk file using C++ Builder (Android development). For this purpose I have found complete guide with example in Delphi (here: http://www.pclviewer.com/android/androidJNI.html). Everything works fine in Delphi, but when I tried to rewrite Pascal source code (at the end of guide) into C++ using C++ Builder XE7, I found curious problem. How to cast Delphi interface into TObject* in C++, properly invoke ClassName() function and get JavaObjectID? I spent a lot of time, but still cannot rewrite these 3 lines of code:
var
JavaObject:JObject;
oTemp:TObject;
JavaObjectID:JNIObject;
:
oTemp:=JavaObject as TObject; //1. ?
JavaObjectID:=tjavaimport(otemp).GetObjectID; //2. ?
memo1.Lines.Add(oTemp.ClassName); //3. ?
:
I have tried differenet type casts, also using GetInterface() method of TObject class, but nothing works. As a C++ programmer I don't understand how is it possible directly cast delphi interface into TObject*.
Androidapi::Jni::Javatypes::_di_JObject JavaObject;
System::TObject* oTemp;
Androidapi::Jni::_JNIObject *JavaObjectID;
:
oTemp = (TObject*)(&JavaObject); //Compiling ok, but segmentation fault
?
Memo1->Lines->Add(oTemp->ClassName()); // after ClassName() invoke.
Does anybody know. how to rewrite these 3 lines of Pascal code into C++ please? Thank you very much for your response.
Casting an interface into an object reference is a Delphi-only feature, there is no equivilent in C++ without delving into the vtable directly. What you can do, however, is write a function in Delphi that takes an interface as input, casts it, and returns the object as output, and then call that function in C++ code. You can add a Delphi .pas file to a C++Builder project, it will generate a C++ .hpp file when compiled.
unit MyJavaHelper;
interface
uses
Androidapi.JNI.JavaTypes;
function JObjectToTObject(JavaObject: JObject): TObject;
implementation
function JObjectToTObject(JavaObject: JObject): TObject;
begin
Result := JavaObject as TObject;
end;
end.
#include "MyJavaHelper.hpp"
Androidapi::Jni::Javatypes::_di_JObject JavaObject;
System::TObject* oTemp;
Androidapi::Jni::_JNIObject *JavaObjectID;
JavaObject = ...;
oTemp = JObjectToTObject(JavaObject);
JavaObjectID = static_cast<TJavaImport*>(otemp)->GetObjectID();
Memo1->Lines->Add(oTemp->ClassName());

Method of binding Java functions to Lua?

I'm working on a Lua wrapper for my Android app, which will allow me to write Lua code to speed up development. I've made a static class called lua with functions like newState and pushString. I manage the Lua state by passing around a long with the pointer to the lua_State. As you can tell, I don't need any fancy stuff that makes interaction easier, like overloads to push variables.
Now, the problem is binding Java functions to Lua variables. I've thought of a few ways to do this, but they're all ugly.
Instead of functions, pass around a table with a reference to the Java function as a userdatum and have a __call metamethod take care of calling the "function".
Alter Lua internals to include a Java reference with Lua C functions.
Is there any better way to go about this? Or should I go with the second method? (I realise the first method is ridiculous, but it manifested itself in my mind as a solution anyways.)
You can have a look at my simple project AndroLua. It contains Lua and LuaJava compiled using the Android NDK.
Because it uses LuaJava, it allows to bind Java functions to Lua, in a similar way like you said, using userdata. Here is an example of how I override the print function to output text into a TextView:
JavaFunction print = new JavaFunction(L) {
#Override
public int execute() throws LuaException {
StringBuilder sb = new StringBuilder();
for (int i = 2; i <= L.getTop(); i++) {
int type = L.type(i);
String val = L.toString(i);
if (val == null)
val = L.typeName(type);
sb.append(val);
sb.append("\t");
}
sb.append("\n");
status.append(sb.toString());
return 0;
}
};
print.register("print");
The downside is that sometimes you cannot pass the print as a function parameter (because it is a userdata, even though it has a __call metamethod). Fortunately, it can be solved in Lua by creating a pure Lua function, like this:
do
local oldprint = print
function print(...) oldprint(...) end
end
I decided to use lua_pushcclosure, as it allows you to 'store' arbitrary values on functions that can be retrieved with the lua_upvalueindex macro.
There is also Kahlua vs LuaJava.
It is mentioned in this book: http://books.google.com/books?id=2v55tfq9rosC&lpg=PA166&ots=9RRVaz5JjP&dq=krka%20kahlua%20blog&pg=PA166#v=onepage&q&f=false
The development blog:
http://krkadev.blogspot.com/2010/05/getting-started-with-kahlua2.html

Safe to pass objects to C functions when working in JNI Invocation API?

I am coding up something using the JNI Invocation API. A C program starts up a JVM and makes calls into it. The JNIenv pointer is global to the C file. I have numerous C functions which need to perform the same operation on a given class of jobject. So I wrote helper functions which take a jobject and process it, returning the needed data (a C data type...for example, an int status value). Is it safe to write C helper functions and pass jobjects to them as arguments?
i.e. (a simple example - designed to illustrate the question):
int getStatusValue(jobject jStatus)
{
return (*jenv)->CallIntMethod(jenv,jStatus,statusMethod);
}
int function1()
{
int status;
jobject aObj = (*jenv)->NewObject
(jenv,
aDefinedClass,
aDefinedCtor);
jobject j = (*jenv)->CallObjectMethod
(jenv,
aObj,
aDefinedObjGetMethod)
status = getStatusValue(j);
(*jenv)->DeleteLocalRef(jenv,aObj);
(*jenv)->DeleteLocalRef(jenv,j);
return status;
}
Thanks.
I'm not acquainted with the details of JNI, but once thing I noticed is this:
return (*jenv)->CallIntMethod(jenv,jStatus,statusMethod);
That looks like the official JNI code and it is taking a jobect as a parameter. If it works for JNI, there is no reason it can't work for your code.
All jni objects are valid until the native method returns. As long as you dont store non global jni objects between two jni calls everything should work.
The invocation of a jni function should work like this:
Java function call
create native local references
call native function
do your stuff
exit native function
release existing local references
return to java
The step 4 can contain any code, local references stay valid until step 6 if not release before.
If you want to store jni objects on the c side between two calls to a native java function you have to create global references and release them later. Not releasing a global reference leads to memory leaks as the garbage collector is unable to free the related java objects.

Categories

Resources