2015-04-01 58 views
-1

我的C#代碼轉換jbyte *到陣列<Byte>^

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:'getTemplet':不能將 參數1從'jbyte *'轉換爲'cli :: array ^'

請幫助將JNI字節數組(jbyte)轉換爲數組,以便我可以全部使用c#函數?

回答

1

看起來您的代碼中有多個問題(即getTemplet返回String^,您試圖從Java_test5_TempletGenerator_getTemplate方法返回爲jstring)。

關於你提到的具體問題:我怎麼轉換imageArray的東西,可以在調用中使用的參數1到getTemplet方法,你可以做到以下幾點:

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。

+0

感謝這就是即時尋找。是。你現在正面臨下一個問題。我可以知道如何將jstring轉換爲String嗎? – Soft 2015-04-01 23:26:34