Cleanup BitmapNet class.

This commit is contained in:
Peter Rekdal Sunde
2015-08-12 15:41:32 +02:00
parent 4015050512
commit cd1c55c8b0
2 changed files with 27 additions and 68 deletions

View File

@@ -5,23 +5,37 @@ using namespace openalprnet;
BitmapMat::BitmapMat(array<Byte>^ byteArray) BitmapMat::BitmapMat(array<Byte>^ byteArray)
{ {
this->m_bitmap = ByteArrayToMat(byteArray); m_bitmap = new cv::Mat();
std::vector<char> buffer = AlprHelper::ToVector(byteArray);
cv::imdecode(buffer, CV_LOAD_IMAGE_COLOR, this->m_bitmap);
} }
BitmapMat::BitmapMat(Bitmap^ bitmap) BitmapMat::BitmapMat(Bitmap^ bitmap)
{ {
this->m_bitmap = BitmapToMat(bitmap); m_bitmap = new cv::Mat();
MemoryStream^ ms = gcnew MemoryStream();
bitmap->Save(ms, ImageFormat::Png);
std::vector<char> buffer = AlprHelper::ToVector(ms->ToArray());
cv::imdecode(buffer, CV_LOAD_IMAGE_COLOR, this->m_bitmap);
delete ms;
} }
BitmapMat::BitmapMat(MemoryStream^ memoryStream) BitmapMat::BitmapMat(MemoryStream^ memoryStream)
{ {
this->m_bitmap = MemoryStreamBitmapToMat(memoryStream); m_bitmap = new cv::Mat();
std::vector<char> buffer = AlprHelper::ToVector(memoryStream->ToArray());
cv::imdecode(buffer, CV_LOAD_IMAGE_COLOR, this->m_bitmap);
} }
BitmapMat::BitmapMat(String^ filename) BitmapMat::BitmapMat(String^ filename)
{ {
Bitmap^ bitmap = gcnew Bitmap(filename); m_bitmap = new cv::Mat();
this->m_bitmap = BitmapToMat(bitmap); array<Byte>^ byteArray = File::ReadAllBytes(filename);
delete bitmap; std::vector<char> buffer = AlprHelper::ToVector(byteArray);
cv::imdecode(buffer, CV_LOAD_IMAGE_COLOR, this->m_bitmap);
delete byteArray;
} }

View File

@@ -1,6 +1,9 @@
#pragma once #pragma once
#include "opencv2/imgproc/imgproc.hpp" #include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "helper-net.h"
using namespace System; using namespace System;
using namespace System::Drawing; using namespace System::Drawing;
@@ -33,74 +36,16 @@ namespace openalprnet {
!BitmapMat() !BitmapMat()
{ {
delete[] m_bitmap->data; m_bitmap->release();
delete m_bitmap;
} }
property cv::Mat Value { property cv::Mat Value {
cv::Mat get() cv::Mat get()
{ {
cv::Mat value = this->m_bitmap->clone(); return *m_bitmap;
return value;
} }
} }
private:
static cv::Mat* BitmapToMat(Bitmap^ bitmap)
{
int channels = 0;
switch (bitmap->PixelFormat)
{
case PixelFormat::Format8bppIndexed:
case PixelFormat::Format1bppIndexed:
channels = 1;
break;
case PixelFormat::Format24bppRgb:
channels = 3;
break;
case PixelFormat::Format32bppRgb:
case PixelFormat::Format32bppArgb:
case PixelFormat::Format32bppPArgb:
channels = 4;
break;
default:
throw gcnew NotSupportedException(bitmap->PixelFormat.ToString());
}
BitmapData^ bitmapData = bitmap->LockBits(
System::Drawing::Rectangle(0, 0, bitmap->Width, bitmap->Height),
ImageLockMode::ReadOnly,
bitmap->PixelFormat
);
const int totalBytes = bitmap->Height * bitmapData->Stride;
char *dst = new char[totalBytes];
::memcpy(dst, bitmapData->Scan0.ToPointer(), totalBytes);
cv::Mat* dstMat = new cv::Mat(cv::Size(bitmap->Width, bitmap->Height), CV_8UC(channels), dst);
bitmap->UnlockBits(bitmapData);
return dstMat;
}
static cv::Mat* MemoryStreamBitmapToMat(MemoryStream^ memoryStream)
{
Bitmap^ bitmap = gcnew Bitmap(memoryStream);
cv::Mat* mat = BitmapToMat(bitmap);
delete bitmap;
return mat;
}
static cv::Mat* ByteArrayToMat(array<Byte>^ byteArray)
{
MemoryStream^ ms = gcnew MemoryStream(byteArray);
cv::Mat* mat = MemoryStreamBitmapToMat(ms);
delete ms;
return mat;
}
}; };
} }