跳至內容
測試伺服器
開發伺服器

如何操作:以獨特的檔名儲存每一張影像#

問題#

如何在我的 API 中將每張圖片儲存為「image_1.png」、「image_2.png」等等?

解決方案#

解決這項任務的方法有很多,但以下是其中兩種最簡單的方法:

  • 請使用以下方式儲存圖片: itoa() 將圖片編號轉換為字串,以產生唯一的檔案名稱。此方法在Linux 上可能無法運作,因為 itoa() 是一個非標準的 C++ 函式。

    char buf[32];
    Pylon::String_t imageNumber;
    imageNumber = itoa(ptrGrabResult->GetImageNumber(), buf, 10);
    Pylon::String_t fileName = "image_" + imageNumber + ".png";
    CImagePersistence::Save(ImageFileFormat_Png, fileName, ptrGrabResult);
    

  • 請使用以下方式儲存圖片: sprintf() 將圖片編號轉換為字串,以產生唯一的檔案名稱。這樣做具有更好的可移植性,且能在Linux 上運作,但會多出一行程式碼。

    char buf[32];
    Pylon::String_t imageNumber;
    sprintf(buf, "%d", ptrGrabResult->GetImageNumber());
    imageNumber = buf;
    Pylon::String_t fileName = "image2_" + imageNumber + ".png";
    CImagePersistence::Save(ImageFileFormat_Png, fileName, ptrGrabResult);
    

返回知識文章