Support UTF8 to UTF16 LE.
Support Unicode helper Fix ANSFR to show 2 same faces in 1 image
This commit is contained in:
@@ -7,7 +7,8 @@
|
||||
"Bash(grep -l \"EnginePoolManager\" /c/Projects/CLionProjects/ANSCORE/modules/ANSODEngine/*.cpp)",
|
||||
"Bash(grep -n \"g_processExiting\" /c/Projects/CLionProjects/ANSCORE/engines/TensorRTAPI/include/engine/*.h /c/Projects/CLionProjects/ANSCORE/modules/ANSODEngine/engine.h)",
|
||||
"Bash(ssh -T git@anscenter.ddns.net -p 2222)",
|
||||
"Bash(ssh-add -l)"
|
||||
"Bash(ssh-add -l)",
|
||||
"Bash(dotnet build:*)"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
125
dotnet/ANSUnicodeHelper/ANSUnicodeHelper.cs
Normal file
125
dotnet/ANSUnicodeHelper/ANSUnicodeHelper.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
/// <summary>
|
||||
/// Unicode helper for LabVIEW .NET interop.
|
||||
/// All methods are static — no instance creation needed.
|
||||
/// In LabVIEW: use Invoke Node with class type ANSCENTER.ANSUnicodeHelper.
|
||||
/// Thread-safe: no shared mutable state.
|
||||
/// </summary>
|
||||
public class ANSUnicodeHelper
|
||||
{
|
||||
// Pre-compiled regex — compiled once at class load, zero per-call cost
|
||||
private static readonly Regex UnicodeEscapeRegex =
|
||||
new Regex(@"\\u([0-9a-fA-F]{4})", RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for LabVIEW (if needed). Lightweight, no state.
|
||||
/// </summary>
|
||||
public ANSUnicodeHelper() { }
|
||||
|
||||
/// <summary>
|
||||
/// Decodes \uXXXX escape sequences to Unicode characters.
|
||||
/// ASCII strings pass through unchanged (zero allocation).
|
||||
/// </summary>
|
||||
public static string Decode(string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input)) return input ?? string.Empty;
|
||||
if (input.IndexOf("\\u", StringComparison.Ordinal) < 0) return input;
|
||||
return UnicodeEscapeRegex.Replace(input, match =>
|
||||
{
|
||||
int codepoint = Convert.ToInt32(match.Groups[1].Value, 16);
|
||||
return ((char)codepoint).ToString();
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws Unicode string at position (x, y).
|
||||
/// </summary>
|
||||
public static void DrawString(Graphics g, string escapedText, Font font, Brush brush, float x, float y)
|
||||
{
|
||||
if (g == null || string.IsNullOrEmpty(escapedText)) return;
|
||||
g.DrawString(Decode(escapedText), font, brush, x, y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws Unicode string at position (x, y) with StringFormat.
|
||||
/// </summary>
|
||||
public static void DrawString(Graphics g, string escapedText, Font font, Brush brush, float x, float y, StringFormat format)
|
||||
{
|
||||
if (g == null || string.IsNullOrEmpty(escapedText)) return;
|
||||
g.DrawString(Decode(escapedText), font, brush, x, y, format);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws Unicode string at PointF with StringFormat.
|
||||
/// </summary>
|
||||
public static void DrawString(Graphics g, string escapedText, Font font, Brush brush, PointF point, StringFormat format)
|
||||
{
|
||||
if (g == null || string.IsNullOrEmpty(escapedText)) return;
|
||||
g.DrawString(Decode(escapedText), font, brush, point, format);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws Unicode string inside a RectangleF with word wrapping.
|
||||
/// </summary>
|
||||
public static void DrawString(Graphics g, string escapedText, Font font, Brush brush, RectangleF layoutRect)
|
||||
{
|
||||
if (g == null || string.IsNullOrEmpty(escapedText)) return;
|
||||
g.DrawString(Decode(escapedText), font, brush, layoutRect);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws Unicode string inside a RectangleF with StringFormat.
|
||||
/// </summary>
|
||||
public static void DrawString(Graphics g, string escapedText, Font font, Brush brush, RectangleF layoutRect, StringFormat format)
|
||||
{
|
||||
if (g == null || string.IsNullOrEmpty(escapedText)) return;
|
||||
g.DrawString(Decode(escapedText), font, brush, layoutRect, format);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Measures Unicode string size after decoding.
|
||||
/// </summary>
|
||||
public static SizeF MeasureString(Graphics g, string escapedText, Font font)
|
||||
{
|
||||
if (g == null || string.IsNullOrEmpty(escapedText)) return SizeF.Empty;
|
||||
return g.MeasureString(Decode(escapedText), font);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Measures Unicode string size with layout area and StringFormat.
|
||||
/// </summary>
|
||||
public static SizeF MeasureString(Graphics g, string escapedText, Font font, SizeF layoutArea, StringFormat format)
|
||||
{
|
||||
if (g == null || string.IsNullOrEmpty(escapedText)) return SizeF.Empty;
|
||||
return g.MeasureString(Decode(escapedText), font, layoutArea, format);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if string contains \uXXXX escapes.
|
||||
/// </summary>
|
||||
public static bool HasUnicodeEscapes(string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input)) return false;
|
||||
return input.IndexOf("\\u", StringComparison.Ordinal) >= 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if string is pure ASCII.
|
||||
/// </summary>
|
||||
public static bool IsAsciiOnly(string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input)) return true;
|
||||
foreach (char c in input)
|
||||
{
|
||||
if (c > 127) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
dotnet/ANSUnicodeHelper/ANSUnicodeHelper.csproj
Normal file
21
dotnet/ANSUnicodeHelper/ANSUnicodeHelper.csproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Drawing" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net48</TargetFramework>
|
||||
<AssemblyName>ANSUnicodeHelper</AssemblyName>
|
||||
<RootNamespace>ANSCENTER</RootNamespace>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<AssemblyVersion>1.0.0.0</AssemblyVersion>
|
||||
<FileVersion>1.0.0.0</FileVersion>
|
||||
<Company>ANSCENTER</Company>
|
||||
<Product>ANSUnicodeHelper</Product>
|
||||
<Description>Unicode helper for LabVIEW .NET interop - decodes \uXXXX escapes to .NET System.String</Description>
|
||||
<OutputPath>..\..\cmake-build-release\bin\</OutputPath>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"format": 1,
|
||||
"restore": {
|
||||
"C:\\Projects\\CLionProjects\\ANSCORE\\dotnet\\ANSUnicodeHelper\\ANSUnicodeHelper.csproj": {}
|
||||
},
|
||||
"projects": {
|
||||
"C:\\Projects\\CLionProjects\\ANSCORE\\dotnet\\ANSUnicodeHelper\\ANSUnicodeHelper.csproj": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "C:\\Projects\\CLionProjects\\ANSCORE\\dotnet\\ANSUnicodeHelper\\ANSUnicodeHelper.csproj",
|
||||
"projectName": "ANSUnicodeHelper",
|
||||
"projectPath": "C:\\Projects\\CLionProjects\\ANSCORE\\dotnet\\ANSUnicodeHelper\\ANSUnicodeHelper.csproj",
|
||||
"packagesPath": "C:\\Users\\nghia\\.nuget\\packages\\",
|
||||
"outputPath": "C:\\Projects\\CLionProjects\\ANSCORE\\dotnet\\ANSUnicodeHelper\\obj\\",
|
||||
"projectStyle": "PackageReference",
|
||||
"fallbackFolders": [
|
||||
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
|
||||
],
|
||||
"configFilePaths": [
|
||||
"C:\\Users\\nghia\\AppData\\Roaming\\NuGet\\NuGet.Config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net48"
|
||||
],
|
||||
"sources": {
|
||||
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
|
||||
"https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/onnxruntime-cuda-11/nuget/v3/index.json": {},
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
"net48": {
|
||||
"targetAlias": "net48",
|
||||
"projectReferences": {}
|
||||
}
|
||||
},
|
||||
"warningProperties": {
|
||||
"warnAsError": [
|
||||
"NU1605"
|
||||
]
|
||||
},
|
||||
"restoreAuditProperties": {
|
||||
"enableAudit": "true",
|
||||
"auditLevel": "low",
|
||||
"auditMode": "direct"
|
||||
},
|
||||
"SdkAnalysisLevel": "10.0.200"
|
||||
},
|
||||
"frameworks": {
|
||||
"net48": {
|
||||
"targetAlias": "net48",
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.200\\RuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
|
||||
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
||||
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\nghia\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
|
||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">7.0.0</NuGetToolVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<SourceRoot Include="C:\Users\nghia\.nuget\packages\" />
|
||||
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
|
||||
64
dotnet/ANSUnicodeHelper/obj/project.assets.json
Normal file
64
dotnet/ANSUnicodeHelper/obj/project.assets.json
Normal file
@@ -0,0 +1,64 @@
|
||||
{
|
||||
"version": 3,
|
||||
"targets": {
|
||||
".NETFramework,Version=v4.8": {}
|
||||
},
|
||||
"libraries": {},
|
||||
"projectFileDependencyGroups": {
|
||||
".NETFramework,Version=v4.8": []
|
||||
},
|
||||
"packageFolders": {
|
||||
"C:\\Users\\nghia\\.nuget\\packages\\": {},
|
||||
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages": {}
|
||||
},
|
||||
"project": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "C:\\Projects\\CLionProjects\\ANSCORE\\dotnet\\ANSUnicodeHelper\\ANSUnicodeHelper.csproj",
|
||||
"projectName": "ANSUnicodeHelper",
|
||||
"projectPath": "C:\\Projects\\CLionProjects\\ANSCORE\\dotnet\\ANSUnicodeHelper\\ANSUnicodeHelper.csproj",
|
||||
"packagesPath": "C:\\Users\\nghia\\.nuget\\packages\\",
|
||||
"outputPath": "C:\\Projects\\CLionProjects\\ANSCORE\\dotnet\\ANSUnicodeHelper\\obj\\",
|
||||
"projectStyle": "PackageReference",
|
||||
"fallbackFolders": [
|
||||
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
|
||||
],
|
||||
"configFilePaths": [
|
||||
"C:\\Users\\nghia\\AppData\\Roaming\\NuGet\\NuGet.Config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
|
||||
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net48"
|
||||
],
|
||||
"sources": {
|
||||
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
|
||||
"https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/onnxruntime-cuda-11/nuget/v3/index.json": {},
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
"net48": {
|
||||
"targetAlias": "net48",
|
||||
"projectReferences": {}
|
||||
}
|
||||
},
|
||||
"warningProperties": {
|
||||
"warnAsError": [
|
||||
"NU1605"
|
||||
]
|
||||
},
|
||||
"restoreAuditProperties": {
|
||||
"enableAudit": "true",
|
||||
"auditLevel": "low",
|
||||
"auditMode": "direct"
|
||||
},
|
||||
"SdkAnalysisLevel": "10.0.200"
|
||||
},
|
||||
"frameworks": {
|
||||
"net48": {
|
||||
"targetAlias": "net48",
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.200\\RuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
dotnet/ANSUnicodeHelper/obj/project.nuget.cache
Normal file
8
dotnet/ANSUnicodeHelper/obj/project.nuget.cache
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"version": 2,
|
||||
"dgSpecHash": "5VJRzMAJt58=",
|
||||
"success": true,
|
||||
"projectFilePath": "C:\\Projects\\CLionProjects\\ANSCORE\\dotnet\\ANSUnicodeHelper\\ANSUnicodeHelper.csproj",
|
||||
"expectedPackageFiles": [],
|
||||
"logs": []
|
||||
}
|
||||
@@ -2161,6 +2161,9 @@ namespace ANSCENTER {
|
||||
START_TIMER(postprocess);
|
||||
resultObjects=UpdateFaceAttributes(recognizedFaces, camera_id);
|
||||
END_TIMER(postprocess, "Update Face Attributes Time");
|
||||
|
||||
// Deduplicate: if two faces matched the same userId, keep only the highest confidence
|
||||
ensureUniqueUserIdWithHighestConfidence(resultObjects);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
@@ -2405,7 +2408,7 @@ namespace ANSCENTER {
|
||||
END_TIMER(json_build, "JSON Build Time");
|
||||
|
||||
START_TIMER(json_serialize);
|
||||
std::string result = root.dump(-1, ' ', true);
|
||||
std::string result = root.dump();
|
||||
END_TIMER(json_serialize, "JSON Serialize Time");
|
||||
|
||||
END_TIMER(json_total, "JSON Conversion Total Time");
|
||||
@@ -2442,7 +2445,7 @@ namespace ANSCENTER {
|
||||
|
||||
root["results"] = detectedObjects;
|
||||
|
||||
return root.dump(-1, ' ', true);
|
||||
return root.dump();
|
||||
}
|
||||
|
||||
// Validation helper methods
|
||||
|
||||
@@ -352,7 +352,7 @@ extern "C" ANSFR_API int DeleteFacesByUser(ANSCENTER::ANSFacialRecognition *
|
||||
extern "C" ANSFR_API double BlurCalculation(unsigned char* jpeg_string, unsigned int bufferLength);
|
||||
|
||||
// Unicode conversion utilities for LabVIEW wrapper classes
|
||||
extern "C" ANSFR_API int ANSFR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result);
|
||||
extern "C" ANSFR_API int ANSFR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM = 1);
|
||||
extern "C" ANSFR_API int ANSFR_ConvertUTF16LEToUTF8(const unsigned char* utf16leBytes, int byteLen, LStrHandle result);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -751,27 +751,20 @@ extern "C" ANSFR_API double BlurCalculation(unsigned char* jpeg_string, un
|
||||
|
||||
|
||||
// Unicode conversion utilities for LabVIEW wrapper classes
|
||||
extern "C" ANSFR_API int ANSFR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result) {
|
||||
extern "C" ANSFR_API int ANSFR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM) {
|
||||
try {
|
||||
if (!utf8Str || !result) return -1;
|
||||
int len = (int)strlen(utf8Str);
|
||||
if (len == 0) return 0;
|
||||
const char bom[2] = { '\xFF', '\xFE' };
|
||||
bool hasUnicodeEscapes = false;
|
||||
bool hasNonAscii = false;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if ((unsigned char)utf8Str[i] >= 0x80) hasNonAscii = true;
|
||||
if (i + 1 < len && utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') hasUnicodeEscapes = true;
|
||||
}
|
||||
if (!hasNonAscii && !hasUnicodeEscapes) {
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + len * sizeof(uChar));
|
||||
if (error != noErr) return -2;
|
||||
(*result)->cnt = len;
|
||||
memcpy((*result)->str, utf8Str, len);
|
||||
return 1;
|
||||
for (int i = 0; i + 1 < len; i++) {
|
||||
if (utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') { hasUnicodeEscapes = true; break; }
|
||||
}
|
||||
if (hasUnicodeEscapes) {
|
||||
std::string utf16le;
|
||||
utf16le.reserve(len * 2);
|
||||
if (includeBOM) utf16le.assign(bom, 2);
|
||||
utf16le.reserve(len * 2 + 2);
|
||||
for (int i = 0; i < len; ) {
|
||||
if (i + 5 < len && utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') {
|
||||
char hex[5] = { utf8Str[i + 2], utf8Str[i + 3], utf8Str[i + 4], utf8Str[i + 5], 0 };
|
||||
@@ -797,11 +790,14 @@ extern "C" ANSFR_API int ANSFR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHan
|
||||
if (wideLen <= 0) return 0;
|
||||
std::wstring wideStr(wideLen, 0);
|
||||
MultiByteToWideChar(CP_UTF8, 0, utf8Str, len, &wideStr[0], wideLen);
|
||||
int size = wideLen * (int)sizeof(wchar_t);
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + size * sizeof(uChar));
|
||||
int dataSize = wideLen * (int)sizeof(wchar_t);
|
||||
int bomSize = includeBOM ? 2 : 0;
|
||||
int totalSize = bomSize + dataSize;
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + totalSize * sizeof(uChar));
|
||||
if (error != noErr) return -2;
|
||||
(*result)->cnt = size;
|
||||
memcpy((*result)->str, wideStr.data(), size);
|
||||
(*result)->cnt = totalSize;
|
||||
if (includeBOM) memcpy((*result)->str, bom, 2);
|
||||
memcpy((*result)->str + bomSize, wideStr.data(), dataSize);
|
||||
return 1;
|
||||
#else
|
||||
return 0;
|
||||
|
||||
@@ -460,7 +460,7 @@ namespace ANSCENTER {
|
||||
{"kps", KeypointsToString(det.kps)}
|
||||
});
|
||||
}
|
||||
return root.dump(-1, ' ', true);
|
||||
return root.dump();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSALPR::VectorDetectionToJsonString", e.what(), __FILE__, __LINE__);
|
||||
|
||||
@@ -143,7 +143,7 @@ extern "C" ANSLPR_API int ANSALPR_SetFormats(ANSCENTER::ANSALPR** Handle, co
|
||||
extern "C" ANSLPR_API int ANSALPR_GetFormats(ANSCENTER::ANSALPR** Handle, LStrHandle formats);// comma separated formats
|
||||
|
||||
// Unicode conversion utilities for LabVIEW wrapper classes
|
||||
extern "C" ANSLPR_API int ANSLPR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result);
|
||||
extern "C" ANSLPR_API int ANSLPR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM = 1);
|
||||
extern "C" ANSLPR_API int ANSLPR_ConvertUTF16LEToUTF8(const unsigned char* utf16leBytes, int byteLen, LStrHandle result);
|
||||
|
||||
#endif
|
||||
@@ -773,27 +773,20 @@ extern "C" ANSLPR_API int ANSALPR_GetFormats(ANSCENTER::ANSALPR** Handle, LS
|
||||
}
|
||||
|
||||
// Unicode conversion utilities for LabVIEW wrapper classes
|
||||
extern "C" ANSLPR_API int ANSLPR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result) {
|
||||
extern "C" ANSLPR_API int ANSLPR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM) {
|
||||
try {
|
||||
if (!utf8Str || !result) return -1;
|
||||
int len = (int)strlen(utf8Str);
|
||||
if (len == 0) return 0;
|
||||
const char bom[2] = { '\xFF', '\xFE' };
|
||||
bool hasUnicodeEscapes = false;
|
||||
bool hasNonAscii = false;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if ((unsigned char)utf8Str[i] >= 0x80) hasNonAscii = true;
|
||||
if (i + 1 < len && utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') hasUnicodeEscapes = true;
|
||||
}
|
||||
if (!hasNonAscii && !hasUnicodeEscapes) {
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + len * sizeof(uChar));
|
||||
if (error != noErr) return -2;
|
||||
(*result)->cnt = len;
|
||||
memcpy((*result)->str, utf8Str, len);
|
||||
return 1;
|
||||
for (int i = 0; i + 1 < len; i++) {
|
||||
if (utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') { hasUnicodeEscapes = true; break; }
|
||||
}
|
||||
if (hasUnicodeEscapes) {
|
||||
std::string utf16le;
|
||||
utf16le.reserve(len * 2);
|
||||
if (includeBOM) utf16le.assign(bom, 2);
|
||||
utf16le.reserve(len * 2 + 2);
|
||||
for (int i = 0; i < len; ) {
|
||||
if (i + 5 < len && utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') {
|
||||
char hex[5] = { utf8Str[i + 2], utf8Str[i + 3], utf8Str[i + 4], utf8Str[i + 5], 0 };
|
||||
@@ -819,11 +812,14 @@ extern "C" ANSLPR_API int ANSLPR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrH
|
||||
if (wideLen <= 0) return 0;
|
||||
std::wstring wideStr(wideLen, 0);
|
||||
MultiByteToWideChar(CP_UTF8, 0, utf8Str, len, &wideStr[0], wideLen);
|
||||
int size = wideLen * (int)sizeof(wchar_t);
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + size * sizeof(uChar));
|
||||
int dataSize = wideLen * (int)sizeof(wchar_t);
|
||||
int bomSize = includeBOM ? 2 : 0;
|
||||
int totalSize = bomSize + dataSize;
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + totalSize * sizeof(uChar));
|
||||
if (error != noErr) return -2;
|
||||
(*result)->cnt = size;
|
||||
memcpy((*result)->str, wideStr.data(), size);
|
||||
(*result)->cnt = totalSize;
|
||||
if (includeBOM) memcpy((*result)->str, bom, 2);
|
||||
memcpy((*result)->str + bomSize, wideStr.data(), dataSize);
|
||||
return 1;
|
||||
#else
|
||||
return 0;
|
||||
|
||||
@@ -259,7 +259,7 @@ namespace ANSCENTER {
|
||||
});
|
||||
}
|
||||
|
||||
return root.dump(-1, ' ', true);
|
||||
return root.dump();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
// Add your error logging here if needed
|
||||
@@ -893,7 +893,7 @@ namespace ANSCENTER {
|
||||
for (const auto& part : res.parts) {
|
||||
alprInfo[part.first] = part.second;
|
||||
}
|
||||
std::string extraInfoStr = alprInfo.dump(-1, ' ', true);
|
||||
std::string extraInfoStr = alprInfo.dump();
|
||||
|
||||
// Use the same field layout as OCRDetectionToJsonString
|
||||
jsonResults.push_back({
|
||||
@@ -912,7 +912,7 @@ namespace ANSCENTER {
|
||||
{"kps", ""}
|
||||
});
|
||||
}
|
||||
return root.dump(-1, ' ', true);
|
||||
return root.dump();
|
||||
} catch (const std::exception&) {
|
||||
return R"({"results":[],"error":"ALPR serialization failed"})";
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ extern "C" ANSOCR_API int SetANSOCRCountry(ANSCENTER::ANSOCRBase** Handl
|
||||
extern "C" ANSOCR_API int SetANSOCRALPRFormat(ANSCENTER::ANSOCRBase** Handle, const char* formatJson);
|
||||
|
||||
// Unicode conversion utilities for LabVIEW wrapper classes
|
||||
extern "C" ANSOCR_API int ANSOCR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result);
|
||||
extern "C" ANSOCR_API int ANSOCR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM = 1);
|
||||
extern "C" ANSOCR_API int ANSOCR_ConvertUTF16LEToUTF8(const unsigned char* utf16leBytes, int byteLen, LStrHandle result);
|
||||
|
||||
// V2 Create / Release — handle as uint64_t by value (no pointer-to-pointer)
|
||||
|
||||
@@ -427,33 +427,25 @@ extern "C" ANSOCR_API int SetANSOCRALPRFormat(ANSCENTER::ANSOCRBase** Handle, co
|
||||
// - JSON Unicode escapes (\uXXXX) from ensure_ascii=true output
|
||||
// - Raw UTF-8 encoded strings
|
||||
// Pure ASCII input is passed through directly (no conversion overhead).
|
||||
extern "C" ANSOCR_API int ANSOCR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result) {
|
||||
extern "C" ANSOCR_API int ANSOCR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM) {
|
||||
try {
|
||||
if (!utf8Str || !result) return -1;
|
||||
int len = (int)strlen(utf8Str);
|
||||
if (len == 0) return 0;
|
||||
|
||||
// Check if input contains \uXXXX escapes or non-ASCII bytes
|
||||
// Always output UTF-16LE (required for LabVIEW "Force Unicode Text" indicators)
|
||||
const char bom[2] = { '\xFF', '\xFE' };
|
||||
|
||||
// Check if input contains \uXXXX escapes
|
||||
bool hasUnicodeEscapes = false;
|
||||
bool hasNonAscii = false;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if ((unsigned char)utf8Str[i] >= 0x80) hasNonAscii = true;
|
||||
if (i + 1 < len && utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') hasUnicodeEscapes = true;
|
||||
for (int i = 0; i + 1 < len; i++) {
|
||||
if (utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') { hasUnicodeEscapes = true; break; }
|
||||
}
|
||||
|
||||
// Pure ASCII with no escapes — pass through directly
|
||||
if (!hasNonAscii && !hasUnicodeEscapes) {
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + len * sizeof(uChar));
|
||||
if (error != noErr) return -2;
|
||||
(*result)->cnt = len;
|
||||
memcpy((*result)->str, utf8Str, len);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// If contains \uXXXX escapes, decode them to UTF-16LE directly
|
||||
if (hasUnicodeEscapes) {
|
||||
std::string utf16le;
|
||||
utf16le.reserve(len * 2);
|
||||
if (includeBOM) utf16le.assign(bom, 2);
|
||||
utf16le.reserve(len * 2 + 2);
|
||||
for (int i = 0; i < len; ) {
|
||||
if (i + 5 < len && utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') {
|
||||
char hex[5] = { utf8Str[i + 2], utf8Str[i + 3], utf8Str[i + 4], utf8Str[i + 5], 0 };
|
||||
@@ -462,7 +454,6 @@ extern "C" ANSOCR_API int ANSOCR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrH
|
||||
utf16le += static_cast<char>((cp >> 8) & 0xFF);
|
||||
i += 6;
|
||||
} else {
|
||||
// ASCII or raw UTF-8 byte — convert as single char
|
||||
utf16le += utf8Str[i];
|
||||
utf16le += '\0';
|
||||
i++;
|
||||
@@ -476,17 +467,19 @@ extern "C" ANSOCR_API int ANSOCR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrH
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Raw UTF-8 — convert via Windows API
|
||||
#ifdef _WIN32
|
||||
int wideLen = MultiByteToWideChar(CP_UTF8, 0, utf8Str, len, nullptr, 0);
|
||||
if (wideLen <= 0) return 0;
|
||||
std::wstring wideStr(wideLen, 0);
|
||||
MultiByteToWideChar(CP_UTF8, 0, utf8Str, len, &wideStr[0], wideLen);
|
||||
int size = wideLen * (int)sizeof(wchar_t);
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + size * sizeof(uChar));
|
||||
int dataSize = wideLen * (int)sizeof(wchar_t);
|
||||
int bomSize = includeBOM ? 2 : 0;
|
||||
int totalSize = bomSize + dataSize;
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + totalSize * sizeof(uChar));
|
||||
if (error != noErr) return -2;
|
||||
(*result)->cnt = size;
|
||||
memcpy((*result)->str, wideStr.data(), size);
|
||||
(*result)->cnt = totalSize;
|
||||
if (includeBOM) memcpy((*result)->str, bom, 2);
|
||||
memcpy((*result)->str + bomSize, wideStr.data(), dataSize);
|
||||
return 1;
|
||||
#else
|
||||
return 0;
|
||||
|
||||
@@ -1135,7 +1135,7 @@ namespace ANSCENTER {
|
||||
});
|
||||
}
|
||||
// ensure_ascii=true escapes non-ASCII chars as \uXXXX for LabVIEW compatibility
|
||||
return root.dump(-1, ' ', true);
|
||||
return root.dump();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
return R"({"results":[],"error":"Serialization failed"})";
|
||||
@@ -2243,27 +2243,20 @@ namespace ANSCENTER {
|
||||
}
|
||||
|
||||
// Unicode conversion utilities for LabVIEW wrapper classes
|
||||
extern "C" ANSENGINE_API int ANSEngine_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result) {
|
||||
extern "C" ANSENGINE_API int ANSEngine_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM) {
|
||||
try {
|
||||
if (!utf8Str || !result) return -1;
|
||||
int len = (int)strlen(utf8Str);
|
||||
if (len == 0) return 0;
|
||||
const char bom[2] = { '\xFF', '\xFE' };
|
||||
bool hasUnicodeEscapes = false;
|
||||
bool hasNonAscii = false;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if ((unsigned char)utf8Str[i] >= 0x80) hasNonAscii = true;
|
||||
if (i + 1 < len && utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') hasUnicodeEscapes = true;
|
||||
}
|
||||
if (!hasNonAscii && !hasUnicodeEscapes) {
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + len * sizeof(uChar));
|
||||
if (error != noErr) return -2;
|
||||
(*result)->cnt = len;
|
||||
memcpy((*result)->str, utf8Str, len);
|
||||
return 1;
|
||||
for (int i = 0; i + 1 < len; i++) {
|
||||
if (utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') { hasUnicodeEscapes = true; break; }
|
||||
}
|
||||
if (hasUnicodeEscapes) {
|
||||
std::string utf16le;
|
||||
utf16le.reserve(len * 2);
|
||||
if (includeBOM) utf16le.assign(bom, 2);
|
||||
utf16le.reserve(len * 2 + 2);
|
||||
for (int i = 0; i < len; ) {
|
||||
if (i + 5 < len && utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') {
|
||||
char hex[5] = { utf8Str[i + 2], utf8Str[i + 3], utf8Str[i + 4], utf8Str[i + 5], 0 };
|
||||
@@ -2289,11 +2282,14 @@ extern "C" ANSENGINE_API int ANSEngine_ConvertUTF8ToUTF16LE(const char* utf8Str,
|
||||
if (wideLen <= 0) return 0;
|
||||
std::wstring wideStr(wideLen, 0);
|
||||
MultiByteToWideChar(CP_UTF8, 0, utf8Str, len, &wideStr[0], wideLen);
|
||||
int size = wideLen * (int)sizeof(wchar_t);
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + size * sizeof(uChar));
|
||||
int dataSize = wideLen * (int)sizeof(wchar_t);
|
||||
int bomSize = includeBOM ? 2 : 0;
|
||||
int totalSize = bomSize + dataSize;
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + totalSize * sizeof(uChar));
|
||||
if (error != noErr) return -2;
|
||||
(*result)->cnt = size;
|
||||
memcpy((*result)->str, wideStr.data(), size);
|
||||
(*result)->cnt = totalSize;
|
||||
if (includeBOM) memcpy((*result)->str, bom, 2);
|
||||
memcpy((*result)->str + bomSize, wideStr.data(), dataSize);
|
||||
return 1;
|
||||
#else
|
||||
return 0;
|
||||
|
||||
@@ -1154,7 +1154,7 @@ namespace ANSCENTER
|
||||
}
|
||||
|
||||
// Unicode conversion utilities for LabVIEW wrapper classes
|
||||
extern "C" ANSENGINE_API int ANSEngine_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result);
|
||||
extern "C" ANSENGINE_API int ANSEngine_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM = 1);
|
||||
extern "C" ANSENGINE_API int ANSEngine_ConvertUTF16LEToUTF8(const unsigned char* utf16leBytes, int byteLen, LStrHandle result);
|
||||
|
||||
#endif
|
||||
@@ -244,7 +244,7 @@ extern "C" ANSULT_API int SendEmail(const char* smtpServer, int port,
|
||||
extern "C" ANSULT_API int RebootSystem();
|
||||
|
||||
// Unicode conversion utilities for LabVIEW
|
||||
extern "C" ANSULT_API int ANSConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result);
|
||||
extern "C" ANSULT_API int ANSConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM = 1);
|
||||
extern "C" ANSULT_API int ANSDecodeJsonUnicodeToUTF16LE(const char* escapedStr, LStrHandle result);
|
||||
extern "C" ANSULT_API int ANSConvertUTF16LEToUTF8(const unsigned char* utf16leBytes, int byteLen, LStrHandle result);
|
||||
|
||||
|
||||
@@ -798,27 +798,20 @@ extern "C" ANSULT_API int RebootSystem() {
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" ANSULT_API int ANSConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result) {
|
||||
extern "C" ANSULT_API int ANSConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM) {
|
||||
try {
|
||||
if (!utf8Str || !result) return -1;
|
||||
int len = (int)strlen(utf8Str);
|
||||
if (len == 0) return 0;
|
||||
const char bom[2] = { '\xFF', '\xFE' };
|
||||
bool hasUnicodeEscapes = false;
|
||||
bool hasNonAscii = false;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if ((unsigned char)utf8Str[i] >= 0x80) hasNonAscii = true;
|
||||
if (i + 1 < len && utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') hasUnicodeEscapes = true;
|
||||
}
|
||||
if (!hasNonAscii && !hasUnicodeEscapes) {
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + len * sizeof(uChar));
|
||||
if (error != noErr) return -2;
|
||||
(*result)->cnt = len;
|
||||
memcpy((*result)->str, utf8Str, len);
|
||||
return 1;
|
||||
for (int i = 0; i + 1 < len; i++) {
|
||||
if (utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') { hasUnicodeEscapes = true; break; }
|
||||
}
|
||||
if (hasUnicodeEscapes) {
|
||||
std::string utf16le;
|
||||
utf16le.reserve(len * 2);
|
||||
if (includeBOM) utf16le.assign(bom, 2);
|
||||
utf16le.reserve(len * 2 + 2);
|
||||
for (int i = 0; i < len; ) {
|
||||
if (i + 5 < len && utf8Str[i] == '\\' && utf8Str[i + 1] == 'u') {
|
||||
char hex[5] = { utf8Str[i + 2], utf8Str[i + 3], utf8Str[i + 4], utf8Str[i + 5], 0 };
|
||||
@@ -841,11 +834,14 @@ extern "C" ANSULT_API int ANSConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandl
|
||||
}
|
||||
std::string converted = ANSCENTER::ANSUtilities::ConvertUTF8ToUTF16LE(utf8Str);
|
||||
if (converted.empty()) return 0;
|
||||
int size = static_cast<int>(converted.size());
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + size * sizeof(uChar));
|
||||
int dataSize = static_cast<int>(converted.size());
|
||||
int bomSize = includeBOM ? 2 : 0;
|
||||
int totalSize = bomSize + dataSize;
|
||||
MgErr error = DSSetHandleSize(result, sizeof(int32) + totalSize * sizeof(uChar));
|
||||
if (error != noErr) return -2;
|
||||
(*result)->cnt = size;
|
||||
memcpy((*result)->str, converted.data(), size);
|
||||
(*result)->cnt = totalSize;
|
||||
if (includeBOM) memcpy((*result)->str, bom, 2);
|
||||
memcpy((*result)->str + bomSize, converted.data(), dataSize);
|
||||
return 1;
|
||||
}
|
||||
catch (...) { return -1; }
|
||||
|
||||
Reference in New Issue
Block a user