windows图片转码程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/czhzasui/article/details/82356167

windows图片转码程序 通过遍历扫描文件路径下所有图片


#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <vector>
#include <cstring>        
#include <string>  
#include <io.h>

using namespace std;
using namespace cv;

#define OPENING_PICS_NUM 51
#define ENDING_PICS_NUM 80

void changealpha(const char *inPath, const char *filename)
{
    char tmp[100] = { 0 };
    Mat mRead;
    strcat(tmp, inPath);
    strcat(tmp, filename);
    //cout << "<<<<" << tmp << endl;
    mRead = imread(tmp, CV_LOAD_IMAGE_UNCHANGED);
    if (mRead.empty() != true) {
        cout << mRead.size().height * mRead.size().width * 4;
        for (int i = 0; i < mRead.size().height * mRead.size().width * 4; i+=4) {
            if (mRead.data[i + 3] == 0x00) {
                mRead.data[i + 0] = 0x00;
                mRead.data[i + 1] = 0x00;
                mRead.data[i + 2] = 0x00;
            }
        }
    }
    memset(tmp, 0, sizeof(tmp));
    strcat(tmp, inPath);
    strcat(tmp, "out\\");
    strcat(tmp, filename);
    cout << ">>>>" << tmp << endl;
    imwrite(tmp, mRead);
}

void changeImagePNG32To24(const char *inPath, const char *filename) {

    char tmp[100] = { 0 };
    Mat mRead;
    Mat png24;
    strcat(tmp, inPath);
    strcat(tmp, filename);
    //cout << "<<<<" << tmp << endl;
    vector<int> params;
    params.push_back(CV_IMWRITE_PNG_COMPRESSION);
    params.push_back(9);
    mRead = imread(tmp, CV_LOAD_IMAGE_UNCHANGED);
    if (mRead.empty() != true) {
        for (int i = 0; i < mRead.size().height * mRead.size().width * 4; i += 4) {
            float alpha = mRead.data[i + 3] / 255.000;
            if (mRead.data[i + 0] != 0x00) {
            }
                mRead.data[i + 0] *= alpha* mRead.data[i + 0];
                mRead.data[i + 1] *= alpha * mRead.data[i + 1];
                mRead.data[i + 2] *= alpha * mRead.data[i + 2];
        }
    }
    cvtColor(mRead, png24, CV_RGBA2RGB);

    memset(tmp, 0, sizeof(tmp));
    strcat(tmp, inPath);
    strcat(tmp, "png24\\");
    strcat(tmp, filename);
    cout << ">>>>" << tmp << endl;
    imwrite(tmp, png24, params);
}

void listFiles(const char * dir)
{
    intptr_t handle;
    _finddata_t findData;
    char *p;

    char tmp[100] = { 0 };
    strcat(tmp, dir);
    strcat(tmp, "*.*");
    cout << tmp  << endl;
    handle = _findfirst(tmp, &findData);    // 查找目录中的第一个文件
    if (handle == -1)
    {
        cout << "Failed to find first file!\n";
        return;
    }
    do
    {
        if (findData.attrib == _A_SUBDIR
            || strcmp(findData.name, ".") == 0
            || strcmp(findData.name, "..") == 0) {
        }
        else {
            //cout << findData.name<< endl;
            p = strchr(findData.name, '.');
            {
                if (strcmp(p, ".db") == 0)
                continue;
            }
            changeImagePNG32To24(dir, findData.name);
        }

    } while (_findnext(handle, &findData) == 0);    // 查找目录中的下一个文件
    cout << "Done!\n";
    _findclose(handle);    // 关闭搜索句柄
}

int main(int argc, char** argv)
{
    while (1)
    {
        char dir[200];
        cout << "Enter a directory : ";
        cin.getline(dir, 200);
        strcat(dir, "\\");
        listFiles(dir);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/czhzasui/article/details/82356167