#pragma once // CDlgInput dialog #include #include #include #include #include // Group: 基本设置;图像处理;送纸方式;高级设置; // // Cat: 图像处理; // class CDlgZk : public CDialogEx { DECLARE_DYNAMIC(CDlgZk) CComboBox font_; CComboBox size_; CComboBox o_size_; CSliderCtrl threshold_; bool initialized_ = false; std::wstring pic_; HBITMAP bmp_ = NULL; std::wstring get_item_text(UINT id); class char_dc { HDC hdc_; HDC src_; HBITMAP bmp_; HBITMAP old_; HFONT font_ = NULL; HFONT oldf_ = NULL; SIZE size_; uint8_t mono_threshold_ = 127; bool pic_ = false; bool get_color(HDC hdc, int x, int y) // true - white; false - black { COLORREF clr = ::GetPixel(hdc, x, y); uint8_t r = GetRValue(clr), g = GetGValue(clr), b = GetBValue(clr); float mono = .3f * r + .59f * g + .11f * b + .5f; //return mono >= mono_threshold_; return pic_ ? mono >= mono_threshold_ : (clr & 1); } uint8_t* lattice(SIZE size, POINT off) { int x = 0, y = 0; uint8_t* buf = NULL, * oper = NULL, val = 0; oper = buf = new uint8_t[size.cx * size.cy / 8]; // LCD: 8 lines / unit, from bottom to top, auto increament on x-axis for (int i = 0; i < size.cx * size.cy / 8; ++i) { val = 0; x = i % size.cx; y = i / size.cx * 8; #define GET_PIXE(r) \ val <<= 1; \ if(x + off.x >= 0 && x + off.x < size_.cx && r + off.y >= 0 && r + off.y < size_.cy) \ { val |= get_color(hdc_, x + off.x, r + off.y); ::SetPixel(src_, x, r, get_color(hdc_, x + off.x, r + off.y) ? RGB(255, 255, 255) : RGB(0, 0, 0)); } GET_PIXE(y + 7); GET_PIXE(y + 6); GET_PIXE(y + 5); GET_PIXE(y + 4); GET_PIXE(y + 3); GET_PIXE(y + 2); GET_PIXE(y + 1); GET_PIXE(y + 0); *oper++ = val ^ 0x0ff; if(size.cy * 2 <= size_.cy) BitBlt(src_, 0, size.cy + 2, size.cx, size.cy, hdc_, 0, 0, SRCCOPY); else BitBlt(src_, size.cx + 2, 0, size.cx, size.cy, hdc_, 0, 0, SRCCOPY); } return buf; } public: char_dc(HDC src) : src_(src) { size_.cx = size_.cy = 256; bmp_ = CreateCompatibleBitmap(src, size_.cx, size_.cy); hdc_ = CreateCompatibleDC(src); old_ = (HBITMAP)SelectObject(hdc_, bmp_); BitBlt(hdc_, 0, 0, size_.cx, size_.cy, src, 0, 0, SRCCOPY); ::SetBkMode(hdc_, TRANSPARENT); ::SetTextColor(hdc_, RGB(0, 0, 0)); } ~char_dc() { if (oldf_) ::SelectObject(hdc_, oldf_); SelectObject(hdc_, old_); DeleteObject(bmp_); DeleteDC(hdc_); if (font_) DeleteObject(font_); } public: bool set_font(int size = 8, const wchar_t* name = L"宋体") { HFONT* old = &oldf_, tmpf = NULL; if (font_) { DeleteObject(font_); old = &tmpf; } font_ = ::CreateFontW(size, 0, 0, 0, 0, 0, 0, 0, GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, name); if (font_) *old = (HFONT)::SelectObject(hdc_, font_); return font_ != NULL; } uint8_t* lattice(wchar_t ch, SIZE size, POINT off) { for (int r = 0; r < size_.cy; ++r) { for (int c = 0; c < size_.cx; ++c) { ::SetPixel(hdc_, c, r, RGB(255, 255, 255)); } } wchar_t str[] = { ch, 0 }; mono_threshold_ = 127; pic_ = false; ::TextOutW(hdc_, 0, 0, str, 1); return lattice(size, off); } uint8_t* lattice(const wchar_t* pic, uint8_t threshold, SIZE size, POINT off, bool stretch = false) { HDC dc = CreateCompatibleDC(hdc_); HBITMAP bmp = (HBITMAP)LoadImageW(NULL, pic, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE), old = (HBITMAP)::SelectObject(dc, bmp); for (int r = 0; r < size_.cy; ++r) { for (int c = 0; c < size_.cx; ++c) { ::SetPixel(hdc_, c, r, RGB(255, 255, 255)); } } if (bmp) { pic_ = true; mono_threshold_ = threshold; if (stretch) { BITMAPINFO bi = { 0 }; GetObject(bmp, sizeof(bi), &bi); StretchBlt(hdc_, 0, 0, size.cx, size.cy, dc, 0, 0, bi.bmiHeader.biWidth, bi.bmiHeader.biWidth, SRCCOPY); } else { BitBlt(hdc_, 0, 0, size_.cx, size_.cy, dc, 0, 0, SRCCOPY); } } ::SelectObject(dc, old); DeleteObject(bmp); DeleteDC(dc); if (bmp) return lattice(size, off); else return NULL; } uint8_t* lattice(HBITMAP bmp, uint8_t threshold, SIZE size, POINT off, bool stretch = false) { HDC dc = CreateCompatibleDC(hdc_); HBITMAP old = (HBITMAP)::SelectObject(dc, bmp); for (int r = 0; r < size_.cy; ++r) { for (int c = 0; c < size_.cx; ++c) { ::SetPixel(hdc_, c, r, RGB(255, 255, 255)); } } if (bmp) { pic_ = true; mono_threshold_ = threshold; if (stretch) { BITMAPINFO bi = { 0 }; GetObject(bmp, sizeof(bi), &bi); StretchBlt(hdc_, 0, 0, size.cx, size.cy, dc, 0, 0, bi.bmiHeader.biWidth, bi.bmiHeader.biWidth, SRCCOPY); } else { BitBlt(hdc_, 0, 0, size_.cx, size_.cy, dc, 0, 0, SRCCOPY); } } ::SelectObject(dc, old); DeleteDC(dc); if (bmp) return lattice(size, off); else return NULL; } }; void regen(void); public: CDlgZk(CWnd* pParent = NULL); // standard constructor virtual ~CDlgZk(); // Dialog Data enum { IDD = IDD_ZK }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support virtual BOOL OnInitDialog(); DECLARE_MESSAGE_MAP() public: afx_msg void OnEnChangeEditData(); afx_msg void OnCbnSelchangeComboFont(); afx_msg void OnCbnSelchangeComboSize(); afx_msg void OnCbnSelchangeComboSize2(); afx_msg void OnBnClickedCheck1(); afx_msg void OnBnClickedCheck2(); afx_msg void OnEnChangeEditPos(); afx_msg void OnNMReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult); afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor); afx_msg void OnBnClickedButton1(); };