|
@@ -11,6 +11,7 @@
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
extern void test();
|
|
|
+extern void test2();
|
|
|
#endif
|
|
|
// 唯一的应用程序对象
|
|
|
|
|
@@ -37,7 +38,7 @@ int main()
|
|
|
{
|
|
|
// TODO: 在此处为应用程序的行为编写代码。
|
|
|
#ifdef _DEBUG
|
|
|
- test();
|
|
|
+ test2();
|
|
|
#endif
|
|
|
}
|
|
|
}
|
|
@@ -51,6 +52,79 @@ int main()
|
|
|
return nRetCode;
|
|
|
}
|
|
|
|
|
|
+// gdiplus token;
|
|
|
+ULONG_PTR g_gdiplusToken;
|
|
|
+// 初始化gid+环境;
|
|
|
+void InitGdiplus() {
|
|
|
+ Gdiplus::GdiplusStartupInput gdiplusStartupInput;
|
|
|
+ Gdiplus::GdiplusStartup(&g_gdiplusToken, &gdiplusStartupInput, NULL);
|
|
|
+}
|
|
|
+
|
|
|
+void ShutGdiplus() {
|
|
|
+ Gdiplus::GdiplusShutdown(g_gdiplusToken);
|
|
|
+}
|
|
|
+
|
|
|
+CV_API bool GdiplusReadImage(std::string strImgPath, Gdiplus::Bitmap** bitmap)
|
|
|
+{
|
|
|
+ // 参数有效性判断;
|
|
|
+ if (!PathFileExists(strImgPath.c_str())) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ OutputDebugString("参数无效!");
|
|
|
+#endif
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+#ifdef UNICODE
|
|
|
+ *bitmap = Image::FromFile(strImgPath.c_str());
|
|
|
+#else
|
|
|
+ // #include <comutil.h>
|
|
|
+ BSTR strtmp = _bstr_t(strImgPath.c_str());
|
|
|
+ *bitmap = Gdiplus::Bitmap::FromFile(strtmp, TRUE);
|
|
|
+ SysFreeString(strtmp);
|
|
|
+
|
|
|
+ if (*bitmap == NULL)
|
|
|
+ return false;
|
|
|
+#endif
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+CV_API bool GdiReadImage(std::string strImgPath, CBitmap& bitmap)
|
|
|
+{
|
|
|
+ // 参数有效性判断;
|
|
|
+ if (!PathFileExists(strImgPath.c_str())) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ OutputDebugString("参数无效!");
|
|
|
+#endif
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 注意:CBitmap是无法直接加载文件;
|
|
|
+ HBITMAP hBitmap;
|
|
|
+ //从文件中加载,使用HBITMAP;
|
|
|
+ hBitmap = (HBITMAP)::LoadImage(
|
|
|
+ //::AfxGetInstanceHandle(),
|
|
|
+ NULL, // oem image
|
|
|
+ strImgPath.c_str(),
|
|
|
+ IMAGE_BITMAP,
|
|
|
+ 0, // cx
|
|
|
+ 0, // cy
|
|
|
+ // 如果是兼容位图,去掉LR_CREATEDIBSECTION
|
|
|
+ LR_LOADFROMFILE|LR_CREATEDIBSECTION
|
|
|
+ );
|
|
|
+
|
|
|
+ // 附加资源;
|
|
|
+ bitmap.Attach(hBitmap);
|
|
|
+ if ( bitmap.m_hObject == NULL ) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ hBmp=(HBITMAP)bmp.GetSafeHandle();
|
|
|
+ */
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
|
|
|
CV_API bool ReadImage(std::string strImgPath, cv::Mat& img)
|
|
|
{
|
|
@@ -611,6 +685,16 @@ CV_API bool Mat2HBitmap(cv::Mat& img, HBITMAP& bitmap)
|
|
|
|
|
|
CV_API bool Mat2GdiplusBitmap(cv::Mat& img, Gdiplus::Bitmap& bitmap)
|
|
|
{
|
|
|
+ cv::Mat temp = img.clone();
|
|
|
+ if (temp.channels() == 1 ) {
|
|
|
+ cv::cvtColor(img, temp, cv::COLOR_GRAY2BGR);
|
|
|
+ }
|
|
|
+
|
|
|
+ Gdiplus::Bitmap bitmap2(temp.cols, temp.rows, temp.step1(), PixelFormat24bppRGB, temp.data);
|
|
|
+ //bitmap = bitmap2;
|
|
|
+ //bitmap2.
|
|
|
+ Gdiplus::Rect rect(0,0, temp.cols, temp.rows);
|
|
|
+
|
|
|
return CV_API bool();
|
|
|
}
|
|
|
|
|
@@ -624,25 +708,26 @@ CV_API bool HBitmap2Mat(HBITMAP& bitmap, cv::Mat& img)
|
|
|
return CV_API bool();
|
|
|
}
|
|
|
|
|
|
-CV_API bool GdiplusBitmap2Mat(Gdiplus::Bitmap& bitmap, cv::Mat& img)
|
|
|
+// 能转,但转出来的gdiplus的图片与opencv的大小存在差异;
|
|
|
+CV_API bool GdiplusBitmap2Mat(Gdiplus::Bitmap* bitmap, cv::Mat& img)
|
|
|
{
|
|
|
- Gdiplus::PixelFormat format = bitmap.GetPixelFormat();
|
|
|
+ Gdiplus::PixelFormat format = bitmap->GetPixelFormat();
|
|
|
if (format != PixelFormat24bppRGB)
|
|
|
return false;
|
|
|
|
|
|
- int width = bitmap.GetWidth();
|
|
|
- int height = bitmap.GetHeight();
|
|
|
+ int width = bitmap->GetWidth();
|
|
|
+ int height = bitmap->GetHeight();
|
|
|
|
|
|
Gdiplus::Rect rcLock(0,0,width,height);
|
|
|
Gdiplus::BitmapData bmpData;
|
|
|
|
|
|
- if ( !bitmap.LockBits(&rcLock, Gdiplus::ImageLockModeRead, format, &bmpData) == Gdiplus::Ok )
|
|
|
+ if ( !bitmap->LockBits(&rcLock, Gdiplus::ImageLockModeRead, format, &bmpData) == Gdiplus::Ok )
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
img = cv::Mat(height, width, CV_8UC3, static_cast<unsigned char*>(bmpData.Scan0), bmpData.Stride).clone();
|
|
|
- bitmap.UnlockBits(&bmpData);
|
|
|
+ bitmap->UnlockBits(&bmpData);
|
|
|
|
|
|
if (img.empty())
|
|
|
return false;
|