Cstring到string
要利用mfc,然后接受一个图片。
imread只能读const string& filename 的东西。
imread 原型:
CV_EXPORTS_W Mat imread( const string& filename, int flags= );
它的参数:
filename —— 文件的位置。如果只提供文件名,那么文件应该和C++文件在同一目录,否则必须提供图片的全路径。
flags —— 有5个可能的输入。
CV_LOAD_IMAGE_UNCHANGED – 在每个通道中,每个像素的位深为8 bit,通道数(颜色)保持不变。
CV_LOAD_IMAGE_GRAYSCALE – 位深=8 bit 通道数=1(颜色变灰)
CV_LOAD_IMAGE_COLOR -位深=?, 通道数=3
CV_LOAD_IMAGE_ANYDEPTH – 位深不变 ,通道数=?
CV_LOAD_IMAGE_ANYCOLOR – 位深=?, 通道数不变
上面的值还可以组合使用,比如:
CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR – 位深不变,通道数比便
CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH – 位深不变,通道数=3
如果你不确定使用哪个,就是用CV_LOAD_IMAGE_COLOR 。
复制的,我都忘了从哪里复制的了。。。笑哭
然后我在包壳的过程中发现,并不能传一个cstring。用到mfc就还是要用cstring的。
所以就遇到了问题:怎么由cstring到string。
问题学名:
错误 1 error C2664: “std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(std::initializer_list<_Elem>,const std::allocator<char> &)”: 无法将参数 1 从“wchar_t *”转换为“const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &” f:\code4vs\打开文件\打开文件\打开文件dlg.cpp 199 1 打开文件
如何将CString 的一个字符串转换成一个string 类型的
方式一:
然后从cstring到 string发生了一些问题。
解决方案有这么几种:
CString str(_T("xxxxxxx"));
#ifdef _UNICODE
wstring s = str;
#else
string s = str;
#endif
这倒是没问题。
WString s = fileNames;
不过imread依然不识别。所以可能需要把wstring转到string。
std::string WStringToString(const std::wstring &wstr)
{
std::string str(wstr.length(), ' ');
std::copy(wstr.begin(), wstr.end(), str.begin());
return str;
}
快捷键格式化代码
也就是俗称的:Ctrl+K,Ctrl+F 快捷键
方式2:
#inlcude <sstream> //专门用来进行类型转换的C++类 //用法
stringstream ss;//需要使用std namespace。
int a = ;
ss<<a;
string s;
ss>>s; s中为""
貌似语法上是可以的,但是好像依然有bug,在读取宽字符的时候,传出来的地址依然不能有效检索。说明在转码的时候还是有问题。
还是会报错
方式3:
Cstring acstring = L“asdf”;
Std::string astring = (acstring.getbuffer());
也不行。这个根本就跑不通
方式4:
如果是unicode工程
USES_CONVERSION;
CString str1;
std::string str2(W2A(str1));
如果是多字节工程
CString str1;
std::string str2(str1.Getbuffer());
str1.ReleaseBuffer();
所以发现我的是unicode工程而并非 多字节工程,所以getbuffer使不了。
所以我的方法是使用w2a方式
中途遇到:
未定义的标识符:_lpw
然后解决方案是:
1、增加头文件 #include <atlconv.h>
2、并且在T2A,W2A,A2W之前加上 USES_CONVERSION
http://blog.csdn.net/educast/article/details/11798695
方式五:
在头文件下面定义这两个方法//其实定义一个就够了,因为我只想做把wsting转成string。但是只是针对非汉字的字符有效,比如数字字母下划线仅此而已。所以为了程序的可以适配的更广,也就是包含汉字字符,就舍弃了这种方法。
std::string WStringToString(const std::wstring &wstr)
{
std::string str(wstr.length(), ' ');
std::copy(wstr.begin(), wstr.end(), str.begin());
return str;
}
std::wstring StringToWString(const std::string &str)
{
std::wstring wstr(str.length(), L' ');
std::copy(str.begin(), str.end(), wstr.begin());
return wstr;
}
关于cstring到string 的blog:
http://bbs.csdn.net/topics/320044813
代码尝试:
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv; std::string WStringToString(const std::wstring &wstr)
{
std::string str(wstr.length(), ' ');
std::copy(wstr.begin(), wstr.end(), str.begin());
return str;
}
std::wstring StringToWString(const std::string &str)
{
std::wstring wstr(str.length(), L' ');
std::copy(str.begin(), str.end(), wstr.begin());
return wstr;
} #include <sstream>
#include<atlconv.h>
using namespace std; void CCstring到stringDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
TCHAR szFilterforpic[] = _T("JPEG(*.JPG;*.JPEG;*.JPE)|*.JPG;*.JPEG;*.JPE|PNG(*.PNG;*.PNS)|*.PNG;*.PNS||");
CFileDialog dlg(true, _T(".png"), NULL, OFN_ALLOWMULTISELECT | OFN_ENABLESIZING, szFilterforpic, this);
dlg.m_ofn.nMaxFile = * MAX_PATH;
dlg.m_ofn.lpstrFile = new TCHAR[dlg.m_ofn.nMaxFile];
ZeroMemory(dlg.m_ofn.lpstrFile, sizeof(TCHAR)*dlg.m_ofn.nMaxFile);
int retval = dlg.DoModal();
if (retval == IDCANCEL)
return;
POSITION pos_file;
pos_file = dlg.GetStartPosition();
CArray<CString, CString> ary_filename;
while (pos_file != NULL){
ary_filename.Add(dlg.GetNextPathName(pos_file));
}
CString fileNames;
CString buf;
CString content;
//string s;
int fileCount = ; for (int i = ; i<ary_filename.GetSize(); i++)
{
content.Format(_T("%s"), ary_filename.GetAt(i));
buf.Format(_T("第%d张图:%s\r\n"), i + , ary_filename.GetAt(i)); fileNames += buf; }
//一:256个字符以内没有问题
//WString s = content;
//string s1 = fileNames;
//Mat img = imread(WStringToString(s));//而且只能打开英文名的图片。这也是个问题。 //二:
//转码错误,猜测。因为没有办法把这个中间过程输出
//stringstream ss;//这个也是要用到 std namespace的。
//ss << content;
//string s;
//ss >> s;
//printf("%s",s);转码错误,猜测。因为没有办法把这个中间过程输出 //三是:cstring.getbuffer(); //四:
USES_CONVERSION;
string s(W2A(content)); Mat img = imread(s);//是支持的 我转码的时候不支持。 //imread()
//MessageBox(s, ); imshow("游戏原画", img);//有办法改变图片大小么?
selected = fileNames;
UpdateData(false); }
然后新建一个:作为完整打开的范式:
#include <iostream>
using namespace std; #include <opencv2/highgui/highgui.hpp>
using namespace cv; void Cmfc完整打开Dlg::OnBnClickedButton1()
{
TCHAR szFilterForPic[] = _T("PNG(*.PNG;*.PNS)|*.PNG;*.PNS|JPEG(*.JPG;*.JPEG;*.JPE)|*.JPG;*.JPEG;*.JPE||");
CFileDialog dlg(true, _T(".png"), NULL, OFN_ALLOWMULTISELECT | OFN_ENABLESIZING, szFilterForPic, this);
dlg.m_ofn.nMaxFile = * MAX_PATH;
dlg.m_ofn.lpstrFile = new TCHAR[dlg.m_ofn.nMaxFile];
ZeroMemory(dlg.m_ofn.lpstrFile, sizeof(TCHAR)*dlg.m_ofn.nMaxFile);
int retval = dlg.DoModal();
if (retval == IDCANCEL)
return;
POSITION pos_file;
pos_file = dlg.GetStartPosition();
CArray<CString, CString> array_filename;
while (pos_file != NULL){
array_filename.Add(dlg.GetNextPathName(pos_file));
}
CString fileNames;
CString buf;
CString content;
for (int i = ; i < array_filename.GetSize(); i++){
content.Format(_T("%s"), array_filename.GetAt(i));
//先把基本的跑出来,然后再改。
USES_CONVERSION;
string s(W2A(content));//iostream;namespace
Mat img = imread(s);//imread 在 opencv2/highgui/highgui.hpp里面。Mat 需要using namespace cv f imshow(s, img);//这个地方可以改换成文章标题 buf.Format(_T("第%d张图:%s\r\n"), i + , array_filename.GetAt(i));
fileNames += buf;
}
selected = fileNames;
UpdateData(false); }
这份代码就十分简洁了。
可以打开多幅图片。
Cstring到string的更多相关文章
- CString转string
如题,找了半天... //CString转string USES_CONVERSION; CString temp; temp = _T("kjdsaflkjdlfkj"); ch ...
- CString和string
CString和string(一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中: CString(typedef CS ...
- unicode下各种类型转换CString、string
把最近用到的各种unicode下类型转换总结了一下: 1.string转CString string a=”abc”; CString str=CString(a.c_str()); 或str.for ...
- unicode下各种类型转换,CString,string,char*,int,char[]
把最近用到的各种unicode下类型转换总结了一下,今后遇到其他的再补充: 1.string转CString string a=”abc”; CString str=CString(a.c_str() ...
- stringstream转换CString为string出错
使用stringstream转换CString为string时,调试时发现是CString赋给stringstream没有问题,stringstram赋给string就不行,倒也不是没有赋成功,只是赋 ...
- C++———库函数cstring及string方法解读
1.string与cstring区别 <string>是C++标准库头文件.包含了拟容器class std::string的声明(不过class string事实上只是basic_stri ...
- CString与string、char*的区别和转换
转自:http://blog.csdn.net/luoweifu/article/details/20232379 我们在C++的开发中经常会碰到string.char*以及CString,这三种都表 ...
- Cpp读文件、CString转String、String转CString
场景 C++读取文件 技术点 读取文件 fstream提供了三个类,用来实现c++对文件的操作.(文件的创建.读.写). ifstream -- 从已有的文件读入 ofstream -- 向文件写内容 ...
- 【转】CString与string、char*的区别和转换
我们在C++的开发中经常会碰到string.char*以及CString,这三种都表示字符串类型,有很多相似又不同的地方,常常让人混淆.下面详细介绍这三者的区别.联系和转换: 各自的区别 char*: ...
随机推荐
- CodeForce---Educational Codeforces Round 3 USB Flash Drives (水题)解题报告
对于这题明显是用贪心算法来解决问题: 下面贴出笔者的代码: #include<cstdio> #include<iostream> #include<algorithm& ...
- 高质量、处于持续更新的R包
本文在Creative Commons许可证下发布 自由软件的问题是开发人员没有稳定的资金来源支持,可能更新上做不到持续.经过考证和圈内朋友的帮助,现在把R包中高质量.持续更新的跟大数据事业相关的R包 ...
- JavaScript(class0526)
什么是JavaScript? HTML只是描述网页长相的标记语言,没有计算.判断能力,如果所有计算.判断(比如判断文本框是否为空.判断两次密码是否输入一致)都放到服务器端执行的话网页的话页面会非常慢. ...
- ubuntu启用root用户
在安装Ubuntu时系统会提示你创建一个用户,但是该用户不具备ROOT权限.但是此时系统是有root用户的,root密码是随机的,如果你想使用root用户需要更改root密码.用你安装系统时创建的用户 ...
- 菜鸟学习HTML5+CSS3(一)
主要内容: 1.新的文档类型声明(DTD) 2.新增的HTML5标签 3.删除的HTML标签 4.重新定义的HTML标签 一.新的文档类型声明(DTD) HTML 5的DTD声明为:<!d ...
- Express细节探究(1)——app.use(express.static)
express相信是很多人用nodejs搭建服务器的首选框架,相关教程有很多,也教会了大家来如何使用.如果你想更深的了解他的细节,不妨和我一起来研究一下. 先来看一个每个人都用到的方法app.use( ...
- 6.2 CUDA streams
stream是什么 nivdia给出的解释是:A sequence of operations that execute in issue-order on the GPU. 可以理解成在GPU上执 ...
- mysql 处理中文乱码问题
CREATE TABLE tbl_score( `ID` INT NOT NULL, `score` DEC(,) NOT NULL, `subject` VARCHAR() NOT NULL ); ...
- Unity3D入门之JavaScript动态创建对象
接着上一篇Unity3D入门文章,这里继续使用JavaScript脚本语言. 调试:Unity集成了MonoDevelop编辑器,在代码某行的左侧点击,即可下一个断点.然后先关闭Unity编辑器,在M ...
- Java 多线程同步的五种方法
一.引言 闲话不多说,进入正题. 二.为什么要线程同步 因为当我们有多个线程要同时访问一个变量或对象时,如果这些线程中既有读又有写操作时,就会导致变量值或对象的状态出现混乱,从而导致程序异常.举个例子 ...