I am implementing OCR functionality and need to use softwarebitmap to call the function, in case I have the file path there is no issue with creating a softwarebitmap object, but in some cases there is no physical file and I have win32 hbitmap which can be converted to memory buffer, can some one help me to convert the hbitmap or memory buffer to softwarebitmap in order to call OCR function.
std::future<hstring> AsyncSample(const std::wstring &path){
auto file = co_await StorageFile::GetFileFromPathAsync(path);
auto stream = co_await file.OpenAsync(FileAccessMode::Read);
auto decoder = co_await BitmapDecoder::CreateAsync(stream);
auto bitmap = co_await decoder.GetSoftwareBitmapAsync();
auto engine = OcrEngine::TryCreateFromUserProfileLanguages();
auto result = co_await engine.RecognizeAsync(bitmap);
return result.Text();}
I found the solution
winrt::Windows::Storage::Streams::IBuffer*
Conversion::CreateNativeBuffer(LPVOID lpBuffer, DWORD
nNumberOfBytes)
{
Microsoft::WRL::ComPtr<NativeBuffer> nativeBuffer;
Microsoft::WRL::Details::MakeAndInitialize<NativeBuffer>
(&nativeBuffer, (BYTE *)lpBuffer, nNumberOfBytes);
auto iinspectable = (IInspectable *)reinterpret_cast<IInspectable *>
(nativeBuffer.Get());
winrt::Windows::Storage::Streams::IBuffer *buffer =
reinterpret_cast<winrt::Windows::Storage::Streams::IBuffer *>(iinspectable);
return buffer;
}
and in the main function
std::future<hstring> getTextFromBufferImage(CHAR *img, DWORD imgLength)
{
Conversion *tConv = new Conversion();
static Windows::Storage::Streams::IBuffer *stream;//::IBuffer *iBuffer;
stream = tConv->CreateNativeBuffer(img, imgLength);
SoftwareBitmap bitmap(BitmapPixelFormat::Rgba16, 30, 30);
bitmap.CopyFromBuffer(*stream);
auto engine = OcrEngine::TryCreateFromUserProfileLanguages();
auto result = co_await engine.RecognizeAsync(bitmap);
return result.Text();
}