瀏覽代碼

1、新增测试函数;
2、添加gdp+初始化函数及释放函数;
3、添加读取gdi+图片的函数;
4、添加gdi+图片转mat的函数;

scbc.sat2 5 年之前
父節點
當前提交
14273ecc4c
共有 4 個文件被更改,包括 139 次插入18 次删除
  1. 92 7
      cv/cv/cv.cpp
  2. 25 11
      cv/cv/cv.h
  3. 3 0
      cv/cv/framework.h
  4. 19 0
      cv/cv/testcpp.cpp

+ 92 - 7
cv/cv/cv.cpp

@@ -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;

+ 25 - 11
cv/cv/cv.h

@@ -11,16 +11,30 @@
 #endif
 
 // 此类是从 dll 导出的
-class CV_API Ccv {
-public:
-	Ccv(void);
-	// TODO: 在此处添加方法。
-};
-
-extern CV_API int ncv;
-
-CV_API int fncv(void);
-
+//class CV_API Ccv {
+//public:
+//	Ccv(void);
+//	// TODO: 在此处添加方法。
+//};
+//
+//extern CV_API int ncv;
+//
+//CV_API int fncv(void);
+
+//////////////////////////////////////////////////////////////////////////
+// gdiplus;
+// 初始化gdiplus环境;
+CV_API void InitGdiplus();
+
+// 关闭gdiplus环境;
+CV_API void ShutGdiplus();
+
+// 读取图片,返回gdiplus对象;
+CV_API bool GdiplusReadImage(std::string strImgPath, Gdiplus::Bitmap **bitmap);
+
+CV_API bool GdiReadImage(std::string strImgPath, CBitmap &bitmap);
+//////////////////////////////////////////////////////////////////////////
+// opencv;
 // 1、读取图片;
 CV_API bool ReadImage(std::string strImgPath, cv::Mat& img);
 
@@ -72,7 +86,7 @@ CV_API bool Mat2HBitmap(cv::Mat& img, HBITMAP& bitmap);
 CV_API bool Mat2GdiplusBitmap(cv::Mat& img, Gdiplus::Bitmap& bitmap);
 CV_API bool Bitmap2Mat(CBitmap& bitmap, cv::Mat& img);
 CV_API bool HBitmap2Mat(HBITMAP& bitmap, cv::Mat& img);
-CV_API bool GdiplusBitmap2Mat(Gdiplus::Bitmap& bitmap, cv::Mat& img);
+CV_API bool GdiplusBitmap2Mat(Gdiplus::Bitmap* bitmap, cv::Mat& img);
 
 // 待开发
 // 11、获取图片指定RGB颜色域的轮廓图像结果

+ 3 - 0
cv/cv/framework.h

@@ -35,5 +35,8 @@
 #define max
 #endif
 
+#include <comutil.h>
+#pragma comment(lib,"comsuppw.lib")
+
 #include <gdiplus.h>
 #pragma comment(lib, "gdiplus.lib")

+ 19 - 0
cv/cv/testcpp.cpp

@@ -85,4 +85,23 @@ void test()
 
 	//////////////////////////////////////////////////////////////////////////
 	printf("测试用例结束!\n");
+}
+
+void test2()
+{
+	// 初始化gdiplus环境;
+	InitGdiplus();
+
+	Gdiplus::Bitmap* img = NULL;
+	if (GdiplusReadImage("E:\\bin\\test.jpg", &img)) {
+		cv::Mat mat;
+		GdiplusBitmap2Mat(img, mat);
+		delete img;
+
+		cv::imwrite("E:\\mat.jpg", mat);
+		mat.release();
+	}
+
+	// 关闭gdiplus环境;
+	ShutGdiplus();
 }