2014年2月5日水曜日

oF: 画像の読み込み

基礎的ですが、openFrameworksに画像を呼び込む方法と書き出す方法覚え書き。
まず、プロジェクトフォルダ内のbin/dataディレクトリに使用する画像を保存
そして以下入力


#pragma once

#include "ofMain.h"

class testApp : public ofBaseApp{

 public:
  void setup();
  void update();
  void draw();

  void keyPressed(int key);
  void keyReleased(int key);
  void mouseMoved(int x, int y );
  void mouseDragged(int x, int y, int button);
  void mousePressed(int x, int y, int button);
  void mouseReleased(int x, int y, int button);
  void windowResized(int w, int h);
  void dragEvent(ofDragInfo dragInfo);
  void gotMessage(ofMessage msg);
    
        ofImage image1;//読み込む画像の名前宣言
        ofImage grabbedimage;//書き出す画像の名前の宣言
    
    
};


#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
    ofBackground(0, 0, 0);
    ofEnableSmoothing();
    ofEnableAlphaBlending();
    ofEnableBlendMode(OF_BLENDMODE_ADD);
    image1.loadImage("shock.jpg");//画像をimage1として読み込む
    
}

//--------------------------------------------------------------
void testApp::update(){
    
}

//--------------------------------------------------------------
void testApp::draw(){
    ofSetColor(255, 255, 255);//画像の背景色の設定。これをいじることで表示彩度や色相が変わる。基本は白
    image1.draw(20,20);//読み込んだ画像データを画面に描写

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
    int width =image1.width;
    int height =image1.height;//画像の縦横幅を取得
    
    if(key =='x'){
        grabbedimage.grabScreen(20,20, width, height);//位置とサイズを指定して画面をキャプチャ
        grabbedimage.saveImage("grabbedimage.png");//キャプチャした画像をgrabbedimage.pngで保存
    }

}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){ 

}




これで、Xキーを押す事によって読み込んだ画像がgrabbedimage.pngとしてプロジェクトフォルダ内のbin/dataディレクトリに保存されるはずです。簡単!

0 件のコメント:

コメントを投稿