我在JNI-Android Application中从char解析float时遇到问题。这是我的示例数据:
0.00618567 0.00224441 0.002006603 0.001813437 0.003761207 -0.001850192 -0.001011893 -0.00342476 0.003790586 0.002385935 0.002647488 0.004411637 0.005938921 0.00698391 0.004522655 0.001524881 -0.002673242 -0.0002569943 -0.002495839 0.00230171 0.000844161 0.006387557 0.008135659 0.005583601 0.002238941 -0.001932641 -0.003518643 -0.0006784072 0.001636732 0.001213515 0.0021472 0.004911256 0.003613603 0.001362842 -0.0002172031 -0.002115535 -0.0002000824 0.001085831 0.003149634 0.003899722 0.004865647 0.002436467 0.0001896242 -0.001678405 -0.001909177 -0.002954236 0.001802054 0.003751467 0.004150682 0.005844797 0.002612064 0.003680898 -0.0005450704 -0.002621638 -0.002253087 0.0005009398 0.004602027 0.003445318 0.00632045 0.002706638 -0.001308871 -0.002082631 -0.001821213 -0.0005696003 0.002069579 0.006264412 0.004593662 0.005836432 0.0009420562 -0.003753015 -0.004050847 -0.001744672 -0.002664186 0.00101941 0.004568859 0.003175343 0.005315124
这是vector<float>
的一种数据hog.setSVMdetector(const vector<float>& detector)
:
0.05359386f, -0.14721455f, -0.05532170f, 0.05077307f, 0.11547081f, -0.04268804f, 0.04635834f, -0.05468199f, 0.08232084f, 0.10424068f, -0.02294518f, 0.01108519f, 0.01378693f, 0.11193510f, 0.01268418f, 0.08528346f, -0.06309239f, 0.13054633f, 0.08100729f, -0.05209739f, -0.04315529f, 0.09341384f, 0.11035026f, -0.07596218f
这是我在JNI中的示例代码.cpp
:
extern "C"
jboolean
Java_com_example_franksyesipangkar_tourguide_CameraPreview_ImageProcessing
(JNIEnv* env, jobject thiz, jint width, jint height, jbyteArray NV21FrameData, jintArray outPixels, jbyteArray b)
{
LOGD("JNIEnv");
//convert jbyteArray to char
jbyte *cmd = env->GetByteArrayElements(b, 0);
LOGD("JNIEnvFeature");
//char * feature = (char *)cmd;
char feature[90600];
memset(feature,0, sizeof(feature));
memcpy(feature, cmd, strlen((char*)cmd));
在这段代码中,char函数中的数据我想将其解析为数据类型hog.setSVMdetector(const vector<float>& detector)
。所以,你有什么想法吗?
答案 0 :(得分:1)
也许这可以帮到你:
#include <iostream>
#include <vector>
#include <strstream>
#include <string>
std::vector<float> parse_delimeted_list_of_numbers(char* line, char delimeter)
{
std::vector<float> vector_of_numbers;
std::istrstream input_stream(line);
std::string text;
float number;
while (std::getline(input_stream, text, delimeter)) {
sscanf_s(text.c_str(), "%f", &number, text.size());
vector_of_numbers.push_back(number);
}
return vector_of_numbers;
}
// A program to test that the above function works well
int _tmain(int argc, _TCHAR* argv[])
{
// auto vector_of_numbers = parse_delimeted_list_of_numbers("234.0, 345, 465, 46456.0",',');
auto vector_of_numbers = parse_delimeted_list_of_numbers("234.0 34 465 46456.0", ' ');
for (auto number : vector_of_numbers)
std::cout << number << "\n";
return 0;
}