基于C++的图片文件路径设置及扫描规定格式文件

ImageClass.h文件

typedef struct FileMessage
{
 CString filename[100];    //文件信息
 _In_ int filenum  ;    //文件数目
}filemessage;

class ImageObject : public CObject
{
public:
ImageObject();
~ImageObject();
public:
         //设置文件所在路径
 void SetRetrievalPath(CString &strfile1);     
            //扫描当前目录中的图像个数并获得文件名            
FileMessage GetFileNameAndNumber(const CString &strfile, const CString &form);                                             
};


ImageClass.CPP文件

#include "ImageClass.h"
#include "Dib.h"
#include "atlbase.h"
#include "atlimage.h"  
#include "string"
#include "TypeConversion.h"

//功能: 设置文件所在路径
//strfile1:返回的文件路径
//:status:finfish
void ImageObject::SetRetrievalPath(CString &strfile1)
{
 CString m_strPath;    //检索库路径
 bool dir;//设置检索库标志位
 BROWSEINFO browse;
 ZeroMemory(&browse, sizeof(browse));       //fills a block of memory with zeros.
 browse.hwndOwner = NULL;
 browse.pszDisplayName = m_strPath.GetBuffer(MAX_PATH);
 browse.lpszTitle = "请选择一个目录";
//SHBrowseForFolder函数返回一个ITEMIDLIST结构的指针,包含了用户选择文件夹的信息
 LPITEMIDLIST lpItem = SHBrowseForFolder(&browse);
 if (lpItem == NULL) return;
 m_strPath.ReleaseBuffer();
//SHGetPathFromIDList把项目标志符列表转换为文档系统路径
 if (SHGetPathFromIDList(lpItem, m_strPath.GetBuffer(MAX_PATH)) == false) return;
 m_strPath.ReleaseBuffer();
 dir = true; //标志位设置为true,表示待检索文件已设置
//得到目录下文件的路径
 m_strPath.TrimRight(); m_strPath.TrimLeft(); //去除前后多余
 strfile1 = m_strPath;
}
//扫描当前目录中的图像个数并获得文件名
//strfile:文件夹路径
//form:文件格式
//FileMessage:filename[tempi]:文件名
//filenum:文件个数
//:status:finfish
FileMessage ImageObject::GetFileNameAndNumber(const CString &strfile, const CString &form)
{
 int tempi = 0;//临时的检索库图像计数器
 FileMessage temp;//检索库中图像路径
 temp.filenum = 0;
 BOOL yesno;
 CFileFind find;
 char tempFileFind[200];
 sprintf_s(tempFileFind, "%s\\*." + form, strfile);
 yesno = find.FindFile(tempFileFind); //在当前目录下查找form文件
 while (yesno)
 {
 yesno = find.FindNextFile();
 char foundFileName[200];//临时存储查找到的图像名
 strcpy_s(foundFileName, find.GetFileName().GetBuffer(200));//获取图像名
  if (!find.IsDots()) //过滤缺省目录
  {
  char tempFileName[200];
  sprintf_s(tempFileName, "%s\\%s", strfile, foundFileName);
  CString strfilepath1;
  strfilepath1.Format("%s", tempFileName);//获取图像完整路径
  temp.filenum++;
  temp.filename[tempi] = strfilepath1;//保存图像完整路径
  tempi++;
  }
 }
  find.Close();
  return temp;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个基于TSP文件最短路径生成图片C++代码示例,使用了STL库中的vector、pair和algorithm模块: ```c++ #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <cmath> #include <string> using namespace std; typedef pair<double, double> Point; typedef vector<Point> Path; double distance(Point p1, Point p2){ double dx = p1.first - p2.first; double dy = p1.second - p2.second; return sqrt(dx*dx + dy*dy); } Path read_tsp_file(string filename){ Path path; ifstream file(filename); string line; bool found = false; while (getline(file, line)){ if (found){ int id; double x, y; sscanf(line.c_str(), "%d %lf %lf", &id, &x, &y); path.push_back(make_pair(x, y)); } else if (line == "NODE_COORD_SECTION"){ found = true; } } return path; } int main(int argc, char *argv[]){ if (argc < 2){ cerr << "Usage: " << argv[0] << " <tsp_file>" << endl; return 1; } Path path = read_tsp_file(argv[1]); vector<int> order(path.size()); for (int i = 0; i < path.size(); i++){ order[i] = i; } double best_distance = -1; Path best_path; do { double distance = 0; for (int i = 0; i < order.size() - 1; i++){ distance += ::distance(path[order[i]], path[order[i+1]]); } if (best_distance < 0 || distance < best_distance){ best_distance = distance; best_path.clear(); for (int i = 0; i < order.size(); i++){ best_path.push_back(path[order[i]]); } } } while (next_permutation(order.begin(), order.end())); ofstream fout("output.ppm"); fout << "P3" << endl; fout << path.size() << " " << path.size() << endl; fout << "255" << endl; for (int i = 0; i < path.size(); i++){ for (int j = 0; j < path.size(); j++){ if (i == j){ fout << "255 255 255 "; } else { int r = (int)(255 * ::distance(path[i], path[j]) / best_distance); fout << r << " 0 0 "; } } fout << endl; } fout.close(); return 0; } ``` 该代码从TSP文件中读取点的坐标,并使用暴力枚举算法计算最短路径。然后,它将生成一个PPM格式的图像文件,其中每个像素表示两个点之间的距离。这个像素的颜色表示两个点之间的距离与最短路径距离之比。白色像素表示同一点,黑色像素表示最远距离的点对。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值