【Processing】画面の範囲を指定してキャプチャする

十字キーでカーソルのサイズを、SHIFTキーと十字キーでカーソルの位置を、
CTRLキーと十字キーでカーソルのサイズを10の倍数に調整して画面の範囲を指定し、
ENTERキーを押してキャプチャする。

int camera_px, camera_py;
int camera_w, camera_h;
int count= 0;
boolean shift, control, left, right, up, down, enter;

void setup() {
  size(1280, 720);
  camera_px = width/2;
  camera_py = height/2;
  camera_w = 100;
  camera_h = 100;
}

void draw() {
  background(127);

  textSize(50);
  text("Hello", width/2, height/2);
  
  if (enter) {
    count++;
    PImage img = createImage(width, height, RGB);
    loadPixels();
    img.pixels = pixels;
    img.updatePixels();
    img = img.get(camera_px-camera_w/2, camera_py-camera_h/2, camera_w, camera_h);
    img.save(dataPath("capture" + count + ".png"));
    delay(100);
  } else {
    if (camera_h%10 ==0) {
      stroke(255,0,0);
      line(0, camera_py, width, camera_py);
    } else {
      stroke(0,0,0);
      line(0, camera_py, width, camera_py);
    }
    if (camera_w%10 ==0) {
      stroke(255,0,0);
      line(camera_px, 0, camera_px, height);
    } else {
      stroke(0,0,0);
      line(camera_px, 0, camera_px, height);
    }
    noFill();
    rectMode(CENTER);
    if (camera_w % 10 ==0 && camera_h % 10 == 0) {
      stroke(255,0,0);
      rect(camera_px, camera_py, camera_w, camera_h);
    } else {
      stroke(0,0,0);
      rect(camera_px, camera_py, camera_w, camera_h);
    }
    rect(camera_px, camera_py, camera_w, camera_h);

    if (shift && right) {
      camera_px++;
    } else if (shift && left) {
      camera_px--;
    } else if (control && right) {
      while(camera_w%10!=0) {
        camera_w++;
      }
    } else if (control && left) {
      while(camera_w%10!=0) {
        camera_w--;
      }
    } else if (right) {
      if (camera_w<width) {
        camera_w++;
      }
    } else if (left) {
      if (camera_w>1) {
        camera_w--;
      }
    }
    if (shift && down) {
      camera_py++;
    } else if(shift && up) {
      camera_py--;
    } else if (control && down) {
      while(camera_h%10!=0) {
        camera_h++;
      }
    } else if (control && up) {
      while(camera_h%10!=0) {
        camera_h--;
      }
    } else if (down) {
      if (camera_h<height) {
        camera_h++;
      }
    } else if (up) {
      if (camera_h>1) {
        camera_h--;
      }
    }
  }
}

void keyPressed() {
  if (keyCode == SHIFT) shift = true;
  if (keyCode == CONTROL) control = true;
  if (keyCode == LEFT) left = true;
  if (keyCode == RIGHT) right = true;
  if (keyCode == UP) up = true;
  if (keyCode == DOWN) down = true;
  if (key == ENTER) enter = true;
}

void keyReleased() {
  if (keyCode == SHIFT) shift = false;
  if (keyCode == CONTROL) control = false;
  if (keyCode == LEFT) left = false;
  if (keyCode == RIGHT) right = false;
  if (keyCode == UP) up = false;
  if (keyCode == DOWN) down = false;
  if (key == ENTER) enter = false;
}
タイトルとURLをコピーしました