📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

76行目: 76行目:
<br>
<br>
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
// ImageResizer.hファイル
  #include <QImage>
  #include <QImage>
  #include <QSize>
  #include <QSize>
#include <QFileInfo>
   
   
  class ImageResizer
  int main()
  {
  {
private:
     // 入力ファイルの存在確認
     // エラーメッセージを保存する変数
     if (!QFileInfo::exists("input.jpg")) return -1;
     QString m_lastError;
    // エラーメッセージを設定するヘルパーメソッド
    void setError(const QString &error)
    {
      m_lastError = error;
    }
   
   
public:
    // 画像の読み込み
     ImageResizer() {};
     QImage image("input.jpg");
     ~ImageResizer() {};
     if (image.isNull()) return -1;
   
   
     // 画像の拡大
     // 画像の拡大
     bool resizeImage(const QString &inputPath, const QString &outputPath, const QSize &newSize)
     QImage resizedImage = image.scaled(QSize(800, 600), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    {
      // 入力ファイルの存在確認
      if (!QFileInfo::exists(inputPath)) {
          setError("入力ファイルが存在しない: " + inputPath);
          return false;
      }
   
   
      // 画像の読み込み
    // 拡大した画像の保存
      QImage image(inputPath);
    if (!resizedImage.save("output.jpg")) return -1;
      if (image.isNull()) {
          setError("画像の読み込みに失敗: " + inputPath);
          return false;
      }
   
   
      // 画像の拡大
    return 0;
      QImage resizedImage = image.scaled(newSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
</syntaxhighlight>
<br>
==== 画像の縮小 ====
以下の例では、QImageクラスを使用して、指定された最大サイズ (: 400x300ピクセル) に収まるように縮小する。<br>
元の画像が既このサイズ以下の場合は、縮小処理は行わない。<br>
<br>
<syntaxhighlight lang="c++">
#include <QImage>
#include <QSize>
#include <QFileInfo>
   
   
      // 拡大した画像の保存
QImage shrinkImage(QImage &image, int maxWidth, int maxHeight)
      if (!resizedImage.save(outputPath)) {
{
          setError("拡大した画像の保存に失敗: " + outputPath);
    QSize originalSize = image.size();
          return false;
    QSize newSize      = originalSize;
      }
   
   
       return true;
    // アスペクト比を維持しながら、指定された最大サイズに収まるように縮小
    if (originalSize.width() > maxWidth || originalSize.height() > maxHeight) {
       newSize.scale(maxWidth, maxHeight, Qt::KeepAspectRatio);
     }
     }
   
   
public:
    // 元のサイズより小さい場合のみ縮小を行う
     // エラーメッセージの取得
     // 縮小の必要がない場合は元の画像をそのまま
     QString lastError() const
     if (newSize != originalSize) {
     {
      return image.scaled(newSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
       return m_lastError;
    }
     else {
       return image;
     }
     }
  };
  }
</syntaxhighlight>
<br>
<syntaxhighlight lang="c++">
// main.cppファイル
#include "ImageResizer.h"
   
   
  int main()
  int main()
  {
  {
     ImageResizer resizer;
     // 入力ファイルの存在確認
     QString inputPath  = "input.jpg";
     if (!QFileInfo::exists("input.jpg")) return -1;
    QString outputPath = "output.jpg";
    QSize newSize(800, 600);
   
   
     if (resizer.resizeImage(inputPath, outputPath, newSize)) {
    // 画像の読み込み
      qDebug() << "画像の拡大に成功";
    QImage image("input.jpg");
     }
     if (image.isNull()) return -1;
     else {
      qDebug() << "エラー:" << resizer.lastError();
    // 画像の縮小
    }
    QImage resizedImage = shrinkImage(image, 400, 300)
     // 縮小した画像の保存
     if (!resizedImage.save("output.jpg")) return -1;
   
   
     return 0;
     return 0;
  }
  }  
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>