/*
功能:
图像批量切割并保存到预设置目录下
文件名前缀一样为同一个文件中切割出来的,切割后的文件名有规律是为了方便以后文件恢复或者合并作准备。
切割尺寸可以手动输入(要自己添加或替换相应代码)
本示例是按200像素为单位切割的
支持纵向切割和横向切割,最后一个碎片若不足200像素会自动补齐到上一张切割碎片的后面,所以最后一张切割碎片有可能不是200像素宽/高
本程序测试用,待改进完善。
*/
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<Windows.h>
#include<io.h>
#include<time.h>
#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:\\Users\\Administrator\\Desktop\\测试图片") ;
//待处理目录
//printf("输入待处理目录:\n");
//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:\\Users\\Administrator\\Desktop\\测试输出\\%d_%d.jpg"), m,n);
putimage(0, 0, with, j, &img1, 0, 200 * i);
saveimage(outpath);
}
else
{
_stprintf_s(outpath, _T("C:\\Users\\Administrator\\Desktop\\测试输出\\%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;
}
//测试示例图
//原图
//被切割为以下4张图片
本文暂时没有评论,来添加一个吧(●'◡'●)