Repository navigation
Expand file tree
/
Copy pathMPAImageCut.cpp
More file actions
185 lines (152 loc) · 4.09 KB
/
Copy pathMPAImageCut.cpp
File metadata and controls
185 lines (152 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// MPAImageCut.cpp : Implementation of CMPAImageCut
#include "stdafx.h"
#include "MPAImageCut.h"
#include ".\mpaimagecut.h"
#include <comutil.h>
#include <string>
#include "json/json.h"
#ifdef _DEBUG
#pragma comment(lib, "libjson_mdd.lib")
#else
#pragma comment(lib, "libjson_md.lib")
#endif
// CMPAImageCut
using namespace std;
static bool stream_to_mem( IStream *stream, void **outbuf, size_t *size )
{
ULARGE_INTEGER ulnSize;
LARGE_INTEGER lnOffset;
lnOffset.QuadPart = 0;
/* get the stream size */
if( stream->Seek( lnOffset, STREAM_SEEK_END, &ulnSize ) != S_OK )
{
return false;
}
if( stream->Seek( lnOffset, STREAM_SEEK_SET, NULL ) != S_OK )
{
return false;
}
/* read it */
*outbuf = malloc( (size_t)ulnSize.QuadPart );
*size = (size_t) ulnSize.QuadPart;
ULONG bytesRead;
if( stream->Read( *outbuf, (ULONG)ulnSize.QuadPart, &bytesRead ) != S_OK )
{
free( *outbuf );
return false;
}
return true;
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
void *mi_to_memory( Gdiplus::Image *image, void **outbuf, size_t *size )
{
IStream *stream = NULL;
if( ::CreateStreamOnHGlobal( NULL, TRUE, &stream ) != S_OK )
{
return NULL;
}
/* get the jpg encoder */
::CLSID jpgClsid;
GetEncoderClsid( L"image/jpeg", &jpgClsid );
/* save the image to stream */
Gdiplus::Status save_s = image->Save( stream, &jpgClsid );
if( save_s != Gdiplus::Ok )
{
stream->Release();
return NULL;
}
/* read the stream to buffer */
if( !stream_to_mem( stream, outbuf, size ) )
{
stream->Release();
return NULL;
}
return *outbuf;
}
void CMPAImageCut::CutImage(Gdiplus::Bitmap& obj)
{
std::vector<pos_rect>::iterator it = m_rt_array.begin();
std::vector<pos_rect>::iterator it_e = m_rt_array.end();
for (int i=0;it!=it_e;it++)
{
image_info img;
size_t len = 0;
pos_rect& rt = *it;
Gdiplus::Bitmap* trg_bmp = new Gdiplus::Bitmap(rt.width,rt.height);;
Gdiplus::Graphics* pMemTrg = Graphics::FromImage(trg_bmp);
pMemTrg->DrawImage(&obj,0,0,rt.X,rt.Y,rt.width,rt.height,UnitPixel);
img.img_buf_ = (char*)mi_to_memory(trg_bmp,(void**)&img.img_buf_,&len);
img.img_size_ = (int)len;
delete pMemTrg;
delete trg_bmp;
m_image_array.push_back(img);
}
}
STDMETHODIMP CMPAImageCut::GetFaceCount(LONG* lCount)
{
*lCount = (LONG)m_image_array.size();
return S_OK;
}
STDMETHODIMP CMPAImageCut::Create(BSTR bsImage,BSTR bsFaceTag)
{
Json::Value root;
Json::Reader reader;
Json::FastWriter w;
std::string faceTag = _bstr_t(bsFaceTag);
bool bRet = reader.parse(faceTag.c_str(), root);
if (!bRet)
return E_FAIL;
int cnt = root.get("result_face_info","").size();
for(int i=0;i<cnt;i++)
{
pos_rect rt;
rt.X = root["result_face_info"][i].get("details.Face.Rectangle.X","").asInt();
rt.Y = root["result_face_info"][i].get("details.Face.Rectangle.Y","").asInt();
rt.width = root["result_face_info"][i].get("details.Face.Rectangle.Width","").asInt();
rt.height = root["result_face_info"][i].get("details.Face.Rectangle.Height","").asInt();
m_rt_array.push_back(rt);
}
//
std::wstring filePath = _bstr_t(bsImage);
Gdiplus::Bitmap bmp(filePath.c_str());
//
CutImage(bmp);
return S_OK;
}
STDMETHODIMP CMPAImageCut::GetImage(LONG lIndex, CHAR** pImg, LONG* lSize)
{
// TODO: Add your implementation code here
if (lIndex >= (LONG)m_image_array.size()||lIndex<0)
return E_FAIL;
LONG len = m_image_array[lIndex].img_size_;
*pImg = m_image_array[lIndex].img_buf_;
*lSize = len;
return S_OK;
}
int CMPAImageCut::test(int t)
{
return 0;
}