c++ string 切割_图像批量切割并输出(C语言)

/*

功能:

图像批量切割并保存到预设置目录下

文件名前缀一样为同一个文件中切割出来的,切割后的文件名有规律是为了方便以后文件恢复或者合并作准备。

切割尺寸可以手动输入(要自己添加或替换相应代码)

本示例是按200像素为单位切割的

支持纵向切割和横向切割,最后一个碎片若不足200像素会自动补齐到上一张切割碎片的后面,所以最后一张切割碎片有可能不是200像素宽/高

本程序测试用,待改进完善。

*/

#include

#include

#include

#include

#include

#include

#include "resource.h"

/*函数声明*/

/********遍历********/

extern void dirsearch(const TCHAR *path);

//横向切割

void hx(TCHAR *path);

//字符串复制到文件

void strtofile(const TCHAR *s, const char *outfile);

//全局变量

int with;

int height;

int main()

{

initgraph(938, 600);

loadimage(NULL,_T("JPG"), MAKEINTRESOURCE(IDR_JPG1), 938, 600);//makeintResource,用ID的方式引用资源 主函数中才能显示

//loadimage(NULL, _T("JPG"), _T("IDR_JPG1"));//此方法不能嵌入资源,我也不清楚

TCHAR inpath[255] = _T("C:甥敳獲AdministratorDesktop测试图片") ;

//待处理目录

//printf("输入待处理目录:");

//scanf("%s", inpath);

dirsearch(inpath);

closegraph();

return 0;

}

//横向切割

void hx(TCHAR *path)

{

IMAGE img1;

initgraph(400, 200);

loadimage(&img1, path);

with = img1.getwidth();

height = img1.getheight();

int i = 0;

srand((unsigned)time(NULL));

Sleep(1000);

int m = rand();

for (i = 0; i * 200 < height; i++)

{

initgraph(with, 200);

int n = i + 1;

TCHAR outpath[255] = { NULL };

int j = height - (200 * i);

if (j < 200 && j>0)

{

initgraph(with, j);

_stprintf_s(outpath, _T("C:甥敳獲AdministratorDesktop测试输出%d_%d.jpg"), m,n);

putimage(0, 0, with, j, &img1, 0, 200 * i);

saveimage(outpath);

}

else

{

_stprintf_s(outpath, _T("C:甥敳獲AdministratorDesktop测试输出%d_%d.jpg"), m, n);

//用wprintf()无法传入却又没有提示有问题

//_stprintf(s, _T("%d"), 1024); // 高版本 VC 推荐使用 _stprintf_s 函数

putimage(0, 0, with, 200, &img1, 0, i * 200);

saveimage(outpath);

}

closegraph();

}

closegraph();

return;

}

/********遍历********/

/*必须包含头文件io.h*/

extern void dirsearch(const TCHAR *path)

{

TCHAR dirfind[MAX_PATH];

TCHAR ffind[MAX_PATH];

struct _wfinddata_t fileinfo;

long handle;

wcscpy(dirfind, path);

wcscat(dirfind,_T("*"));

handle = _wfindfirst(dirfind, &fileinfo);

if (handle == -1)return;

do{

if (fileinfo.attrib&_A_SUBDIR)

{

if (fileinfo.name[0] != '.')

{

wcscpy(dirfind, path);

wcscat(dirfind, _T(""));

wcscat(dirfind, fileinfo.name);

dirsearch(dirfind);

}

}

else

{

wcscpy(ffind, path);

wcscat(ffind, _T(""));

wcscat(ffind, fileinfo.name);

//strtofile(ffind, "H:jpg.txt");

//outtextxy(300, 300, ffind);

//调用切割函数

hx(ffind);

}

} while (_wfindnext(handle, &fileinfo) == 0);

_findclose(handle);

}

/*************************

字符串复制到输出文件函数

string--->>output

**************************/

void strtofile(const TCHAR *s, const char *outfile)

{

FILE *output;

output = fopen(outfile, "b+");

if (output == NULL)return;

fwrite(s, wcslen(s), 1, output);

fclose(output);

return;

}

//测试示例图

//原图

1d772f2376ba952762ab6aca5dc7215a.png

//被切割为以下4张图片

c299948dfb328867bfec60fe10586cba.png
be41e8e015384d58c49aa38fd668cb88.png
78f03bb25151cdcc8ceda74a9a3d8a00.png
accf49d4247656412622c3c2ad0172d4.png
e0112f62f41f7491b14314c72f01cd85.png
8ae381b699c747d9d884499074aa6c9e.png
d252670dd16fe93be2b5cca250d7a0bd.png
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值