完成Partition函式實作分割成16份;
#include "ImagePartition.h"
#include <iostream>
#include <string>
//------------------------------------------------------------------------------------------------------------------
ImagePartition::ImagePartition()
{
}
ImagePartition::~ImagePartition()
{
}
// 圖片切割函式
// in_buffer為原BMP圖片的位圖資料,OriginalW和OriginalH分別代表原BMP圖片的寬和高
// 將切割好的圖片存放至out_buffer
// wBlocks和hBlocks分別代表對原圖進行怎樣的切割,如要將原圖切割為2 * 4,則wBlocks和hBlocks分別為2,4
bool __stdcall ImagePartition::Partition(void* in_buffer, int OriginalW, int OriginalH, void** out_buffer, int wBlocks, int hBlocks)
{// 圖片陣列的首地址,
return true;
}
bool __stdcall ImagePartition::test()
{
void* buffer = new char[1366 * 768 * 3];
// BMP圖片的寬和高
int w = 1024, h = 768;
// 獲取BMP圖片的位圖資料
wchar_t base_path[260] = { 0 };
auto bmpPath = GetModuleFileNameW(nullptr, base_path, 260);
std::wstring path = base_path;
path = path.substr(0, path.rfind(L"\\") + 1);
std::wstring temp_path = (path + L"image1.bmp");
bool success = GetImageBuffer((path + L"image1.bmp").c_str(), w, h, buffer);
if (!success)
{
return success;
}
std::vector<void*> out_buffer;
int len = w * h * 3;
for(size_t i = 0; i < 100; ++i)
{
char* p = new char[len];
out_buffer.push_back(p);
}
// 對圖片進行切割
Partition(buffer, 1366, 768, &out_buffer[0], 4, 4);
//std::string prefix = "image";
//std::string num;
//std::string ext = ".bmp";
//std::string file;
////for(int i = 0; i < 18; ++i)
////{
//// char buf[8] = { 0 };
//// itoa(i + 1, buf, 10);//需要轉換的數字,轉換出來的地方,進制10進制。
//// num = buf;
//// file = prefix + num + ext;
//// SaveBuffer2Bmp(file.c_str(), out_buffer[i], 256, 192);
//// file.clear();
////}
////delete[] buffer;
////for (size_t i = 0 ; i < out_buffer.size() ; i++ )
////{
//// delete [] out_buffer[i];
////}
////
return true;
}
//------------------------------------------------------------------------------------------------------------------
// 保存圖片函式
bool __stdcall ImagePartition::SaveBuffer2Bmp(const char* pFileName, void* pBuf, int width, int height)
{
bmFileHeader.bfSize = height * width * 3 + 54;
bmFileHeader.bfReserved1 = 0;
bmFileHeader.bfReserved2 = 0;
bmFileHeader.bfType = 0x4d42;
bmFileHeader.bfOffBits = 54;
bmiHeader.biBitCount = 24;
bmiHeader.biClrImportant = 0;
bmiHeader.biClrImportant = 0;
bmiHeader.biClrUsed = 0;
bmiHeader.biCompression = 0;
bmiHeader.biHeight = height;
bmiHeader.biWidth = width;
bmiHeader.biPlanes = 1;
bmiHeader.biSize = 40;
bmiHeader.biSizeImage = height * width * 3;
bmiHeader.biXPelsPerMeter = 0;
bmiHeader.biYPelsPerMeter = 0;
if(!pFileName)
{
return false;
}
FILE* hFile = fopen(pFileName, "wb");
if(!hFile)
{
return false;
}
if(!fwrite(&bmFileHeader, sizeof(bmFileHeader), 1, hFile))
{
return false;
}
if(!fwrite(&(bmiHeader), sizeof(bmiHeader), 1, hFile))
{
return false;
}
if(!fwrite((char*)pBuf,bmiHeader.biSizeImage, 1, hFile))
{
return false;
}
fclose(hFile);
return true;
}
// 獲取BMP圖片的位圖資料
bool __stdcall ImagePartition::GetImageBuffer(const wchar_t* image_path, int &width, int &height, void* buffer)
{
BITMAPINFO info = {0};
//初始化位圖資訊頭
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = width;
info.bmiHeader.biHeight = height;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 24;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biSizeImage = 0;
info.bmiHeader.biXPelsPerMeter = 0;
info.bmiHeader.biYPelsPerMeter = 0;
info.bmiHeader.biClrUsed = 0;
info.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL, image_path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (!hBitmap)
{
return NULL;
}
HDC imageDC = GetMemDC();
SelectObject(imageDC, hBitmap);
BITMAP bm;
GetObject(hBitmap, sizeof(bm), &bm);
width = bm.bmWidth;
height = bm.bmHeight;
int r = ::GetDIBits(imageDC, hBitmap, 0, bm.bmHeight, buffer, &info, DIB_RGB_COLORS);
return true;
}
uj5u.com熱心網友回復:
類的頭檔案有沒有貼出來用c++builder什么版本
uj5u.com熱心網友回復:
420280150親方便qq聯系嗎uj5u.com熱心網友回復:
先把帖子結了,我來幫你調
uj5u.com熱心網友回復:
許久沒上來了,你的這個代碼沒看過,不過根據你的需求寫了一個函式,支持jpeg和bmp影像的任意數量分割,你試試效果:
#include <jpeg.hpp>
int ImagePartition(
// 要操作的檔案
String T_PODK
,
// 分割后的檔案的保存目錄
String T_GIVJ
,
// 分割的列數
int T_LKBT
,
// 分割的行數
int T_SPXI
){
if(
L""==T_PODK // 要操作的檔案
||
L""==T_GIVJ // 分割后的檔案的保存目錄
){
return -1;
}
if(true != FileExists(T_PODK)) // 檢查檔案是否存在
{
return -2;
}
if(true != ForceDirectories(T_GIVJ)) // 一次建立多層目錄樹,成功建立回傳true目錄已經存在也回傳true
{
return-3;
}
//if(true != DirectoryExists(T_GIVJ)) // 檢查本地或網路目錄是否存在,網路目錄要先打通路徑
//{
// 此處不需要檢查
//return -4;
//}
if(T_LKBT<=0) // 分割的列數
{
T_LKBT=1;
}
if(T_SPXI<=0) // 分割的行數
{
T_SPXI=1;
}
if(L"\\" != T_GIVJ.SubString(T_GIVJ.Length(),1))
{
T_GIVJ=T_GIVJ+L"\\"; // 如果末尾不是“\\”就加上
}
Graphics::TBitmap *bmpSrc=https://bbs.csdn.net/topics/new Graphics::TBitmap;
if(
(String(L".jpeg")).LowerCase()==ExtractFileExt(T_PODK).LowerCase()
||
(String(L".jpg")).LowerCase()==ExtractFileExt(T_PODK).LowerCase()
){
TJPEGImage *jpg=new TJPEGImage; // 宣告一個jpg物件
jpg->LoadFromFile(T_PODK); // 讀檔案進來
bmpSrc->Assign(jpg); // 在Image1中show出圖片
delete jpg; // 釋放
}
else
{
bmpSrc->LoadFromFile(T_PODK); // 載入圖片
}
Graphics::TBitmap *bmpDst=new Graphics::TBitmap;
int tCount=T_LKBT*T_SPXI; // 總圖片數
bmpDst->Width=bmpSrc->Width/T_LKBT;
bmpDst->Height=bmpSrc->Height/T_SPXI;
SetStretchBltMode(bmpDst->Canvas->Handle, STRETCH_HALFTONE);
bmpDst->Canvas->CopyMode=cmSrcCopy;
String T_FRQO;
for(int i=0;i<IntToStr(tCount).Length();i++)
{
T_FRQO=T_FRQO+L"0";
}
String tTempName;
for(int i=0;i<tCount;i++)
{
bmpDst->Canvas->CopyRect(Rect( 0, 0, bmpDst->Width, bmpDst->Height), bmpSrc->Canvas,Rect( (i-(i/T_LKBT)*T_LKBT)*bmpDst->Width, (i/T_LKBT)*bmpDst->Height, (i-(i/T_LKBT)*T_LKBT)*bmpDst->Width+bmpDst->Width, (i/T_LKBT)*bmpDst->Height+bmpDst->Height));
tTempName=Now().FormatString(L"yyyymmddhhnnss")+L"_"+FormatFloat(T_FRQO,i);
bmpDst->SaveToFile(T_GIVJ+tTempName+L".bmp");
}
delete bmpDst;
delete bmpSrc;
return 0;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
OpenPictureDialog1->Options >> ofAllowMultiSelect; // 關閉檔案多選屬性
OpenPictureDialog1->Filter=
L"全部位圖檔案 (*.jpg;*.jpeg;*.bmp)|*.jpg;*.jpeg;*.bmp"
L"| JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg"
L"| BMP檔案 (*.bmp)|*.bmp"
L"| 所有檔案 (*.*)|*.*" // 顯示的檔案型別
;
if(true != OpenPictureDialog1->Execute())
{
return;
}
String T_PODK=OpenPictureDialog1->FileName;
ImagePartition(
// 要操作的檔案
T_PODK
,
// 分割后的檔案的保存目錄
L"C:\\[TEMP]"
,
// 分割的列數
4
,
// 分割的行數
4
);
}
uj5u.com熱心網友回復:
強韌
uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/95809.html
標籤:基礎類
上一篇:【潛水】гулаг 2015/8/11 6:46:19 有個問題困擾我幾天了,群里的大牛幫忙看下。 我加載ctp的dll時,會報下面這個錯誤。
下一篇:復數矩陣求偽逆的演算法
