Free Support Forum - aspose.com

Barcode Reader Cpp with OpenCV

Hi,
i currently try to connect my OpenCV image pipeline with the Aspose Barcode Reader in c++.
But i cant figure out how to do it. Please can you give me some advice what im doing wrong here:
My plan ist to convert my opencv image into an “Aspose” BitMap.

//INPUT OPENCV
cv::Mat cameraImage = cv::imread(“data/1.png”);

//Convert to Grayscale 16bit
if(cameraImage.channels()!=1) cv::cvtColor(cameraImage, cameraImage, cv::COLOR_BGR2GRAY);
cameraImage.convertTo(cameraImage, CV_16UC1,255);

System::SmartPtrSystem::Drawing::Bitmap BitMap = System::MakeObjectSystem::Drawing::Bitmap(cameraImage.rows, cameraImage.cols, System::Drawing::Imaging::PixelFormat::Format16bppGrayScale, cameraImage.data);
BitMap->Save(u"testBitMap.png");

barCodeReader->SetBarCodeImage(BitMap);

Im receiving an error:
Exception thrown at 0x00007FFE51169709 in ConsoleApplication1.exe: Microsoft C++ exception: System::ExceptionWrapperSystem::Details_ArgumentOutOfRangeException at memory location 0x000000FDBB2FEC90.
Unhandled exception at 0x00007FFE51169709 in ConsoleApplication1.exe: Microsoft C++ exception: System::ExceptionWrapperSystem::Details_ArgumentOutOfRangeException at memory location 0x000000FDBB2FEC90.

Thank you in advance!

@LukasSEW,
Thank you for your query.
Could you please share the file saved as “testBitMap.png” for our reference? We will analyze this file and share our feedback soon.

Just for your understanding. The program crashs at the lines where the BitMap will initialized. So the saved file “testBitMap.png” is empty!
So there are problems with the initialization of the BitMap.

Is there no example in c++ available - which uses opencv for image grapping and preprocessing?

//load and show OpenCV Mat
cv::Mat cameraImage = cv::imread("data/" + std::to_string(i) + ".png");
if (cameraImage.channels() != 1)
{
  std::cout << "convert to greyscale image" << std::endl;
  cv::cvtColor(cameraImage, cameraImage, cv::COLOR_BGR2GRAY);
}
cv::imshow("test", cameraImage);
cv::waitKey(1);

//Mat data into puffer pointer
int image_size = cameraImage.total() * cameraImage.elemSize();
void* image_uchar = new uchar[image_size];
std::memcpy(image_uchar, cameraImage.data, image_size);

//Initialize new object of BitMap
System::SmartPtr<System::Drawing::Bitmap> BitMap = System::MakeObject<System::Drawing::Bitmap>(cameraImage.cols, cameraImage.rows, System::Drawing::Imaging::PixelFormat::Format8bppIndexed, image_uchar);

//Try to save BitMap - just to control if the image is correct
BitMap->Save(u"BitMap.png");<a class="attachment" 

asposeForum.PNG (183.4 KB)

@LukasSEW,
Thank you for providing more information. I am afraid that no specific example is available which uses opencv for image source. This is in fact beyond the scope of our product and no assistance can be provided in this regard. You may please try to sort this issue at your end and if face any issue related to Aspose.BarCode, share the details with us. We will try to reproduce the issue here and provide assistance at the earliest.

We are sorry for the inconvenience caused to you.

Maybe you can control if there is any bug in the initialization of a BitMap with an void* Buffer.
Because its independent where the data came frome. In my case the data is provided by opencv Mat.

@LukasSEW,
I have checked the documents thoroughly but could not find any such feature which can control the bug in the initialization of a BitMap. You may please perform tests at your end and provide us with some image created by OpenCV. Once we get some image then we will provide you further assistance to read text from it using Aspose.BarCode.

Hi,
can you give me an example how to use the BitMap initialization with an void* buffer? It doesnt matter if the image data comes from OpenCV or any other source.
The following data is available
:

  • void* buffer on image data (vector)
  • width
  • heigth
  • format (8Bit grayscale) for example

Now i want to use the function to initialize the BitMap.
System::SmartPtrSystem::Drawing::Bitmap BitMap = System::MakeObjectSystem::Drawing::Bitmap(width, heigth, System::Drawing::Imaging::PixelFormat, void* buffer);

So I want to build an application where i will load the image directly from a camera and not from the disk. I guess my problem isnt specific on a OpenCV image -> its more or less a problem with the initialization of an BitMap.

In the attachement you can find an image i created by OpenCV image acquisition. But in the end its just a normal image file.

6.jpg (2.5 MB)

@LukasSEW,
Could you please give a try to the following sample codes and share the feedback if these fulfill your requirement or not? After preparing the image you can read the barcode text from the image using these two samples.

Bitmap with Proceed Recangle Set Up Sample

System::SharedPtr<System::IO::FileStream> lStream = System::MakeObject<System::IO::FileStream>(u"c:\\test.jpg", System::IO::FileMode::Open, System::IO::FileAccess::Read, System::IO::FileShare::Read);
 
System::SharedPtr<BarCodeReader> reader = System::MakeObject<BarCodeReader>();
reader->SetBarCodeImage(lStream);
while (reader->Read())
{
    System::Console::WriteLine(System::String(u"BarCode CodeText: ") + reader->GetCodeText());
}

Bitmap With Proceed Rectangles List Set Up Sample

System::SharedPtr<System::Drawing::Bitmap> lBitmap = System::MakeObject<System::Drawing::Bitmap>(u"c:\\test.jpg");
 
System::SharedPtr<BarCodeReader> reader = System::MakeObject<BarCodeReader>();
reader->SetBarCodeImage(lBitmap, System::MakeArray<System::Drawing::Rectangle>({System::Drawing::Rectangle(0, 0, lBitmap->get_Width(), lBitmap->get_Height())}));
while (reader->Read())
{
    System::Console::WriteLine(System::String(u"BarCode CodeText: ") + reader->GetCodeText());
}

I really appreciate your help and fast response!! Thank you very much! This is a positive fact in my decision making if we will purchase this barcode Reader.

But i need an example with the void* buffer. Both Code samples you provide load an .jpg from the disk!
But i dont wanna load an image from my disk. I want to convert image data provided by any Acquisition stored in buffer “directly” into an BitMap.
I dont want to save it as a .jpg and load it again from the disk. This isnt possible in an time-critical application.

So I need an exapmle without loading an Image from the disk… only with the void* buffer…

Sorry but i still need an advice.

@LukasSEW,
We have understood your requirement but we need to look into it more. We have logged the issue in our database for investigation and for a fix. Once, we will have some news for you, we will update you in this topic.

This issue has been logged as

BARCODECPP-420 – Sample code for loading image from a void pointer

1 Like

Hi - are there any updates?

@LukasSEW,
We are still working on this issue and it may take one more week. We will write back here once any update is ready for sharing.

Please - I cant wait no longer for an answer!

@LukasSEW,
I am afraid that due to the complexity of the issue, it is still not resolved and may take a couple of weeks to accomplish. I will write back here as soon as some feedback is ready to share.

I really cant understand whats the complexity of this problem. Every other SDK can provide “input from Datastream” out of the box… But still thanks for your effort!

@LukasSEW,
It needs some time so we will be scheduling to resolve it soon. Once, we will have some news for you, we will update you in this topic.