我的c#代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SourceAFIS.Simple;
using System.Windows.Media.Imaging;
namespace TempSample
{
public class TempletExtractorSample
{
// Inherit from Fingerprint in order to add Filename field
[Serializable]
class MyFingerprint : Fingerprint
{
public string Filename;
}
// Inherit from Person in order to add Name field
[Serializable]
class MyPerson : Person
{
public string Name;
}
static AfisEngine Afis;
// Take fingerprint image file and create Person object from the image
public static string getTemplate(byte[] byteArray, string name)
{
Console.WriteLine("Enrolling {0}...", name);
// Initialize empty fingerprint object and set properties
MyFingerprint fp = new MyFingerprint();
//BitmapImage image = new BitmapImage(new Uri(filename, UriKind.RelativeOrAbsolute));
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = new System.IO.MemoryStream(byteArray);
image.EndInit();
fp.AsBitmapSource = image;
// Above update of fp.AsBitmapSource initialized also raw image in fp.Image
// Check raw image dimensions, Y axis is first, X axis is second
Console.WriteLine(" Image size = {0} x {1} (width x height)", fp.Image.GetLength(1), fp.Image.GetLength(0));
// Initialize empty person object and set its properties
MyPerson person = new MyPerson();
person.Name = name;
// Add fingerprint to the person
person.Fingerprints.Add(fp);
// Execute extraction in order to initialize fp.Template
Console.WriteLine(" Extracting template...");
Afis = new AfisEngine();
Afis.Extract(person);
// Check template size
Console.WriteLine(" Template size = {0} bytes", fp.Template.Length);
byte[] b = fp.Template;
string base64String = System.Convert.ToBase64String(b, 0, b.Length);
return base64String;
}
}
}
VC ++代码
#include "stdafx.h"
#include "TempletGen.h"
#include "test5_TempletGenerator.h"
#include <string>
using System::Text::Encoding;
String^ toString(const char *chars){
int len=(int)strlen(chars);
array<unsigned char>^ a = gcnew array<unsigned char>(len);
int i=0;
while(i<len){
a[i] = chars[i];
i++;
}
return Encoding::UTF8->GetString(a);
}
String^ getTemplet(array<Byte>^ byte,const char* p){
return TempSample::TempletExtractorSample::getTemplate(byte,toString(p));
}
JNIEXPORT jstring JNICALL Java_test5_TempletGenerator_getTemplate
(JNIEnv *env, jobject c, jbyteArray imageArray, jstring name){
jbyte* bufferPtr = env->GetByteArrayElements(imageArray, NULL);
jboolean isCopyName;
const char *nm = env->GetStringUTFChars(name,&isCopyName);
return getTemplet(bufferPtr, nm);
}
VC ++代码在JNI代码中显示编译错误
TempletGen.cpp(39):错误C2664:&#39; getTemplet&#39; :无法转换 参数1来自&#39; jbyte *&#39;到&#39; cli :: array ^&#39;
请帮助将JNI字节数组(jbyte)转换为数组,以便我可以全部使用c#函数?
答案 0 :(得分:1)
您的代码中似乎有多个问题(即getTemplet
会返回String^
,您尝试从jstring
方法返回Java_test5_TempletGenerator_getTemplate
关于您的具体问题:如何在调用imageArray
方法时将getTemplet
转换为可用作参数1的内容,您可以执行以下操作:
int len = env->GetArrayLength(imageArray);
unsigned char* bufferPtr = reinterpret_cast<unsigned char*>(env->GetByteArrayElements(imageArray, NULL));
array<Byte>^ byteArray = gcnew array<Byte>(len);
Marshal::Copy((IntPtr)bufferPtr, byteArray, 0, len);
现在,您可以在byteArray
的调用中使用getTemplet
作为参数1。