今天把前兩天GJ7所拍的照片轉正,一次轉三四百張左右大約800MB,結果PhotoImpact批次處理轉正轉到memory leak....整個程式當掉系統還當機ORZ.....所以剛剛吃飯完用三十幾分鐘的時間寫一個轉正圖片的小工具...ORZ,另外因為相機都是出JPG所以只能我只讓它讀JPG,不過可以自己改wxFileDialog的wildcard增加想要的檔案格式。
photorotate.h
photorotate.h
#include <wx/wx.h>
#include <wx/spinctrl.h>
enum{
ID_ANGLE = 100,
ID_QUALITY
};
class App:public wxApp
{
public:
bool OnInit();
};
class Frame:public wxFrame
{
public:
Frame(const wxString&);
void OnOpen(wxCommandEvent&);
void OnSave(wxCommandEvent&);
void OnExit(wxCommandEvent&);
private:
wxArrayString filepaths;
wxArrayString filenames;
wxRadioBox *angle;
wxSlider *quality;
DECLARE_EVENT_TABLE()
};
photorotate.cpp
Download
#include "photorotate.h"
IMPLEMENT_APP(App)
DECLARE_APP(App)
bool App::OnInit()
{
wxFrame *frame = new Frame(wxT("PhotoRotate"));
frame->Show(true);
return true;
}
BEGIN_EVENT_TABLE(Frame,wxFrame)
EVT_BUTTON(wxID_OPEN,Frame::OnOpen)
EVT_BUTTON(wxID_SAVE,Frame::OnSave)
EVT_MENU(wxID_EXIT,Frame::OnExit)
END_EVENT_TABLE()
Frame::Frame(const wxString &title):wxFrame(NULL,wxID_ANY,title,wxDefaultPosition,wxDefaultSize,wxMINIMIZE_BOX | wxCLOSE_BOX | wxCAPTION | wxSYSTEM_MENU)
{
wxInitAllImageHandlers();
wxMenu *file = new wxMenu;
file->Append(wxID_EXIT,wxT("E&xit\tAlt-e"),wxT("Exit"));
wxMenuBar *bar = new wxMenuBar;
bar->Append(file,wxT("File"));
SetMenuBar(bar);
wxBoxSizer *top = new wxBoxSizer(wxVERTICAL);
this->SetSizer(top);
wxBoxSizer *box1 = new wxBoxSizer(wxHORIZONTAL);
top->Add(box1,0,wxALIGN_CENTER_HORIZONTAL | wxALL,5);
wxArrayString item;
item.Add(wxT("+90"));
item.Add(wxT("-90"));
angle = new wxRadioBox(this,ID_ANGLE,wxT("Rotate Angle"),wxDefaultPosition,wxDefaultSize,item,wxRA_SPECIFY_COLS);
box1->Add(angle,0,wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL,5);
wxStaticText *quality_text = new wxStaticText(this,wxID_STATIC,wxT(" Quality:"),wxDefaultPosition,wxDefaultSize,0);
box1->Add(quality_text,0,wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL,5);
quality = new wxSlider(this,ID_QUALITY,100,0,100,wxDefaultPosition,wxSize(200,-1),wxSL_HORIZONTAL | wxSL_AUTOTICKS | wxSL_LABELS);
box1->Add(quality,0,wxALIGN_CENTER_HORIZONTAL | wxALL,5);
wxBoxSizer *box2 = new wxBoxSizer(wxHORIZONTAL);
top->Add(box2,0,wxALIGN_CENTER_HORIZONTAL | wxALL,5);
wxButton *open = new wxButton(this,wxID_OPEN,wxT("&Open"),wxDefaultPosition,wxDefaultSize);
box2->Add(open,0,wxALIGN_CENTER_VERTICAL | wxALL,5);
wxButton *save = new wxButton(this,wxID_SAVE,wxT("&Save"),wxDefaultPosition,wxDefaultSize);
box2->Add(save,0,wxALIGN_CENTER_VERTICAL | wxALL,5);
CreateStatusBar(2);
SetStatusText(wxT("PhotoRotate"));
top->Fit(this);
top->SetSizeHints(this);
}
void Frame::OnOpen(wxCommandEvent &event)
{
wxFileDialog dlg(this,wxT("選擇檔案"),wxT("."),wxT(""),wxT("JPG files (*.JPG)|*.JPG"),wxOPEN | wxMULTIPLE);
if(dlg.ShowModal() == wxID_OK){
dlg.GetPaths(filepaths);
dlg.GetFilenames(filenames);
}
}
void Frame::OnSave(wxCommandEvent &event)
{
wxDirDialog dlg(this,wxT("選擇目錄"),wxT("."),wxDD_NEW_DIR_BUTTON);
if(dlg.ShowModal() == wxID_OK){
const wxString target = dlg.GetPath();
const int _angle = angle->GetSelection();
const int _quality = quality->GetValue();
for(int i = 0;i < filenames.Count();i++){
wxImage image;
if(image.LoadFile(filepaths[i])){
image = image.Rotate90(_angle);
image.SetOption(wxIMAGE_OPTION_QUALITY,_quality);
image.SaveFile(target + wxT("/") + filenames[i]);
}
}
wxMessageBox(wxT("轉換完成!"),wxT("PhotoRotate"),wxOK,this);
}
}
void Frame::OnExit(wxCommandEvent &event)
{
Close();
}