zoukankan      html  css  js  c++  java
  • Cstring到string

    要利用mfc,然后接受一个图片。

    imread只能读const string& filename 的东西。

    imread 原型:

    CV_EXPORTS_W Mat imread( const string& filename, int flags=1 );

    它的参数:

    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的。

    所以就遇到了问题:怎么由cstringstring

    问题学名:

    错误 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 类型的

    方式一:

    然后从cstringstring发生了一些问题。

    解决方案有这么几种:

    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)
    
    10  {
    
    11      std::string str(wstr.length(), ' ');
    
    12      std::copy(wstr.begin(), wstr.end(), str.begin());
    
    13      return str;
    
    14  }

    快捷键格式化代码

    也就是俗称的:Ctrl+K,Ctrl+F 快捷键

    方式2

    #inlcude <sstream> //专门用来进行类型转换的C++类
    
    //用法
    stringstream ss;//需要使用std namespace。
    int a = 100;
    ss<<a;
    string s;
    ss>>s; s中为"100"

    貌似语法上是可以的,但是好像依然有bug,在读取宽字符的时候,传出来的地址依然不能有效检索。说明在转码的时候还是有问题。

    还是会报错

    方式3

    Cstring acstring = Lasdf;

    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、并且在T2AW2AA2W之前加上 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;
    }

    关于cstringstring 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 = 100 * 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 = 1;
        
        for (int i = 0; i<ary_filename.GetSize(); i++)
        {
            content.Format(_T("%s"), ary_filename.GetAt(i));
            buf.Format(_T("第%d张图:%s
    "), i + 1, 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 = 100 * 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 = 0; 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
    "), i + 1, array_filename.GetAt(i));
            fileNames += buf;
        }
        selected = fileNames;
        UpdateData(false);
    
    }

    这份代码就十分简洁了。

    可以打开多幅图片。

  • 相关阅读:
    c#2005中的各个控件转换为html代码
    支付宝接口参数详谈
    IE6兼容菜单
    cookie版购物车
    火狐执行子页面方法
    easyui中datebox文本框输入非数字报错的改善
    右侧悬浮菜单
    内边距、边框和外边距
    自我超越
    DateUtils 时间工具类
  • 原文地址:https://www.cnblogs.com/letben/p/5338406.html
Copyright © 2011-2022 走看看